Skip to content

Instantly share code, notes, and snippets.

@AndreasGrip
Created January 10, 2023 09:35
Show Gist options
  • Save AndreasGrip/bed862075206ea531a8ff2f044228729 to your computer and use it in GitHub Desktop.
Save AndreasGrip/bed862075206ea531a8ff2f044228729 to your computer and use it in GitHub Desktop.
Home Assistant Node js. Get priceses from tibber, and calculate when it's cheapest based on hours it takes.
[
{
"id": "f5409b74bf753a05",
"type": "tab",
"label": "Tibber",
"disabled": false,
"info": "",
"env": []
},
{
"id": "f7e275da2c86c70a",
"type": "group",
"z": "f5409b74bf753a05",
"style": {
"stroke": "#999999",
"stroke-opacity": "1",
"fill": "none",
"fill-opacity": "1",
"label": true,
"label-position": "nw",
"color": "#a4a4a4"
},
"nodes": [
"189854435faf9cef",
"bcb76ab300317dd5",
"567d6dcc2470668a",
"c2405c25d294865c"
],
"x": 54,
"y": 259,
"w": 492,
"h": 162
},
{
"id": "c8a1e191bff08995",
"type": "junction",
"z": "f5409b74bf753a05",
"x": 740,
"y": 100,
"wires": [["27858193c3ce30f7", "01dd4186fef32a02", "4c249f7be74e9a7f"]]
},
{
"id": "b54d015f0cb58729",
"type": "inject",
"z": "f5409b74bf753a05",
"name": "",
"props": [{ "p": "payload" }, { "p": "topic", "vt": "str" }],
"repeat": "3600",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "{ viewer { homes { currentSubscription{ priceInfo{ today { total startsAt } tomorrow { total startsAt } } } } } }",
"payloadType": "str",
"x": 110,
"y": 100,
"wires": [["e1cf23514a793cac", "cbe7f1ce4407b866"]]
},
{
"id": "5fb85c0e768e0165",
"type": "debug",
"z": "f5409b74bf753a05",
"name": "Tibber: retruned raw data",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload.viewer.homes[0].currentSubscription.priceInfo",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 570,
"y": 20,
"wires": []
},
{
"id": "4c249f7be74e9a7f",
"type": "debug",
"z": "f5409b74bf753a05",
"name": "Tibber: extractPrices_BestPrices",
"active": false,
"tosidebar": true,
"console": true,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 960,
"y": 160,
"wires": []
},
{
"id": "eb5169126f5e4d50",
"type": "function",
"z": "f5409b74bf753a05",
"name": "extractPrices_BestPrices",
"func": "const priceInfo = msg.payload.viewer.homes[0].currentSubscription.priceInfo;\nconst priceInfoNew = { prices: [...priceInfo.today.filter(p => new Date(p.startsAt) > new Date()), ...priceInfo.tomorrow] }\nconst allPrices = priceInfoNew.prices\n// create and combine all the Hour prices\nfor (let i = allPrices.length - 1; i >= 0; i--) {\n for (let i2 = i; i2 >= 0; i2--) {\n if (typeof allPrices[i2]['h' + (i - i2 + 1)] === 'undefined') allPrices[i2]['h' + (i - i2 + 1)] = 0;\n allPrices[i2]['h' + (i - i2 + 1)] += allPrices[i].total;\n\n let i3 = 2;\n while (allPrices[i2]['h' + (i - i2 + i3)]) {\n allPrices[i2]['h' + (i - i2 + i3)] += allPrices[i2]['h' + (i - i2 + 1)]\n i3++;\n }\n }\n console.log(i);\n}\n// divide the hourprices with the number of hours it represent.\nallPrices.forEach(e => {\n let i = 2;\n while (e['h' + i]) {\n e['h' + i] = e['h' + i] / i\n i++\n }\n})\n\nconst bestPrices = {}\nlet i = 1;\n// for each timeXXh sort contents\nwhile (allPrices[0]['h' + i]) {\n bestPrices['h' + i] = [...allPrices].filter(e => typeof e['h' + i] !== 'undefined').sort((a, b) => a['h' + i] - b['h' + i])\n i++\n}\npriceInfoNew.bestPrices = bestPrices\n\nreturn {payload: priceInfoNew};",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 570,
"y": 100,
"wires": [["c8a1e191bff08995"]]
},
{
"id": "27858193c3ce30f7",
"type": "change",
"z": "f5409b74bf753a05",
"name": "tibberPrices",
"rules": [
{
"t": "set",
"p": "tibberPrices",
"pt": "global",
"to": "payload.prices",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 890,
"y": 100,
"wires": [["fe830e62e2dedc3a"]]
},
{
"id": "01dd4186fef32a02",
"type": "change",
"z": "f5409b74bf753a05",
"name": "tibberBestPrices",
"rules": [
{
"t": "set",
"p": "tibberBestPrices",
"pt": "global",
"to": "payload.bestPrices",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 900,
"y": 40,
"wires": [["0b604f3fd795c857"]]
},
{
"id": "e1cf23514a793cac",
"type": "debug",
"z": "f5409b74bf753a05",
"name": "Tibber: graphQl code",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 340,
"y": 160,
"wires": []
},
{
"id": "cbe7f1ce4407b866",
"type": "tibber-query",
"z": "f5409b74bf753a05",
"name": "Tibber Endpoint",
"active": true,
"apiEndpointRef": "95e3c9a60ef39f13",
"x": 320,
"y": 100,
"wires": [["5fb85c0e768e0165", "eb5169126f5e4d50"]]
},
{
"id": "fe830e62e2dedc3a",
"type": "debug",
"z": "f5409b74bf753a05",
"name": "Tibber: tibberPrices",
"active": true,
"tosidebar": true,
"console": true,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1120,
"y": 100,
"wires": []
},
{
"id": "0b604f3fd795c857",
"type": "debug",
"z": "f5409b74bf753a05",
"name": "Tibber: tibberBestPrices",
"active": true,
"tosidebar": true,
"console": true,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1140,
"y": 40,
"wires": []
},
{
"id": "189854435faf9cef",
"type": "debug",
"z": "f5409b74bf753a05",
"g": "f7e275da2c86c70a",
"name": "tibberBestPrices",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "tibberBestPrices",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 420,
"y": 340,
"wires": []
},
{
"id": "bcb76ab300317dd5",
"type": "inject",
"z": "f5409b74bf753a05",
"g": "f7e275da2c86c70a",
"name": "",
"props": [
{ "p": "tibberBestPrices", "v": "tibberBestPrices", "vt": "global" },
{ "p": "tibberPrices", "v": "tibberPrices", "vt": "global" }
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 150,
"y": 340,
"wires": [["189854435faf9cef", "567d6dcc2470668a"]]
},
{
"id": "567d6dcc2470668a",
"type": "debug",
"z": "f5409b74bf753a05",
"g": "f7e275da2c86c70a",
"name": "tibberPrices",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "tibberPrices",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 410,
"y": 380,
"wires": []
},
{
"id": "c2405c25d294865c",
"type": "comment",
"z": "f5409b74bf753a05",
"g": "f7e275da2c86c70a",
"name": "Debug",
"info": "",
"x": 130,
"y": 300,
"wires": []
},
{
"id": "95e3c9a60ef39f13",
"type": "tibber-api-endpoint",
"feedUrl": "wss://api.tibber.com/v1-beta/gql/subscriptions",
"queryUrl": "https://api.tibber.com/v1-beta/gql",
"feedTimeout": "60",
"name": "Correct"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment