Skip to content

Instantly share code, notes, and snippets.

@MatsA
Created August 12, 2023 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MatsA/f61e0d6404b0932734b47c4abe300d84 to your computer and use it in GitHub Desktop.
Save MatsA/f61e0d6404b0932734b47c4abe300d84 to your computer and use it in GitHub Desktop.
NodeRed node getting hourly Nordpool prices and calculate energy consumption at home
[
{
"id": "cf84e2f1f062c5fd",
"type": "tab",
"label": "Energy price/consumption",
"disabled": false,
"info": "",
"env": []
},
{
"id": "4210ad2e94dfc7f3",
"type": "nordpool-api-plus",
"z": "cf84e2f1f062c5fd",
"name": "",
"area": "SE4",
"currency": "SEK",
"action": "dayAhead",
"x": 380,
"y": 80,
"wires": [
[
"4686f4ee61df58c9"
]
]
},
{
"id": "66cee571cfba05ca",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "00 23 * * *",
"once": false,
"onceDelay": "10",
"topic": "",
"payload": "",
"payloadType": "date",
"x": 110,
"y": 80,
"wires": [
[
"4210ad2e94dfc7f3",
"a6f282a79e554ae2"
]
]
},
{
"id": "4686f4ee61df58c9",
"type": "function",
"z": "cf84e2f1f062c5fd",
"name": "Extract data from arry",
"func": "\nlet maxPrice = -100;\nlet minPrice = 100;\nlet price = 0\nlet ackPrice = 0\n\nlet msg1 = {};\nfor (var i = 0; i < msg.payload.length; i++) {\n\n price = parseFloat((msg.payload[i].price / 1000).toFixed(3))\n ackPrice = ackPrice + price\n\n msg1 = {\n topic:msg.payload[i].currency,\n payload:price,\n timestamp:msg.payload[i].timestamp\n }\n node.send(msg1);\n\n if (price > maxPrice) {\n maxPrice = price;\n }\n if (price < minPrice) {\n minPrice = price;\n }\n}\n\nnode.done;\n\nlet priceDate = new Date(msg.payload[0].timestamp).toLocaleDateString(\"se\")\nlet averagePrice = (ackPrice/i).toFixed(3)\n\nmsg.payload = \"Date: \" + priceDate + \"<br />Average price: \" + averagePrice + \" SEK/kW, Max price: \" + maxPrice + \" SEK/kW, Min price: \" + minPrice + \"SEK/kW \";\n\nreturn msg",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 680,
"y": 80,
"wires": [
[
"d441e48a7a3e2a96",
"aa09114f67b272a2",
"e2adb51a9ce23cb8"
]
]
},
{
"id": "d441e48a7a3e2a96",
"type": "ui_chart",
"z": "cf84e2f1f062c5fd",
"name": "",
"group": "f8a38be7242dbf0c",
"order": 7,
"width": 0,
"height": 0,
"label": "SE4 kW prices, SEK",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm",
"interpolate": "step",
"nodata": "",
"dot": false,
"ymin": "",
"ymax": "",
"removeOlder": 1,
"removeOlderPoints": "",
"removeOlderUnit": "86400",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#ff2600",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 680,
"y": 120,
"wires": [
[]
]
},
{
"id": "a6f282a79e554ae2",
"type": "change",
"z": "cf84e2f1f062c5fd",
"name": "Clear chart",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "[]",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 370,
"y": 140,
"wires": [
[
"d441e48a7a3e2a96",
"aa09114f67b272a2"
]
]
},
{
"id": "daf45df0dce4e5d0",
"type": "comment",
"z": "cf84e2f1f062c5fd",
"name": "Getting kW prices for this day",
"info": "\n",
"x": 170,
"y": 40,
"wires": []
},
{
"id": "caf04626bd5e9d02",
"type": "debug",
"z": "cf84e2f1f062c5fd",
"name": "debug 97",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 780,
"y": 400,
"wires": []
},
{
"id": "962fb775772dbece",
"type": "function",
"z": "cf84e2f1f062c5fd",
"name": "Acc. current every 5 seconds",
"func": "\nlet totCurrent = msg.payload;\nlet ackCurrent = flow.get(\"ackCurrent\") + totCurrent;\n\n// For the hourly ackumulated energy consumption \nflow.set(\"ackCurrent\", ackCurrent);\n\n// For the graph\nmsg.payload = ((totCurrent) * 230)/1000; // Energy in kW\nmsg.topic = \"kW\";\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 160,
"y": 280,
"wires": [
[
"31e85e5ac2ad949a"
]
]
},
{
"id": "47de92c9ce1c3422",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "Every full hour",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "0 0-23 * * *",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 120,
"y": 320,
"wires": [
[
"a44c3cbd262d1987"
]
]
},
{
"id": "a44c3cbd262d1987",
"type": "function",
"z": "cf84e2f1f062c5fd",
"name": "Calc last hour kW consumption",
"func": "\nlet ackCurrent = flow.get(\"ackCurrent\");\nflow.set(\"ackCurrent\", 0);\n\n// Average current per measured hour => ackCurrent/12(12 times per minute, 5 seconds) * 60. Energy kW => (Avg current * 230 Volt)/1000\nlet kWLastHour = (((ackCurrent/720)*230)/1000).toFixed(3);\n\nmsg.payload = kWLastHour;\nmsg.topic = \"kW\"\n\nreturn msg;\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 430,
"y": 320,
"wires": [
[
"fcde38ddb3730e3e"
]
]
},
{
"id": "fcde38ddb3730e3e",
"type": "ui_chart",
"z": "cf84e2f1f062c5fd",
"name": "",
"group": "f8a38be7242dbf0c",
"order": 12,
"width": 0,
"height": 0,
"label": "Energy consumption per hour",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "step",
"nodata": "",
"dot": false,
"ymin": "",
"ymax": "",
"removeOlder": "24",
"removeOlderPoints": "",
"removeOlderUnit": "3600",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#1f77b4",
"#aec7e8",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 710,
"y": 320,
"wires": [
[
"caf04626bd5e9d02",
"f2075e584fe47304"
]
]
},
{
"id": "1c5e94b359a1b3fc",
"type": "comment",
"z": "cf84e2f1f062c5fd",
"name": "Calculating power consumption using VP data",
"info": "",
"x": 210,
"y": 180,
"wires": []
},
{
"id": "f2075e584fe47304",
"type": "change",
"z": "cf84e2f1f062c5fd",
"name": "Store chartArray as flow context",
"rules": [
{
"t": "set",
"p": "chartArray",
"pt": "flow",
"to": "payload[0].data[0]",
"tot": "msg"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 170,
"y": 380,
"wires": [
[]
]
},
{
"id": "b7d0d0bc57062a91",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "Every day at 00:05",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "05 00 * * *",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 130,
"y": 420,
"wires": [
[
"bf2ee8e8fe60ab0f"
]
]
},
{
"id": "bf2ee8e8fe60ab0f",
"type": "function",
"z": "cf84e2f1f062c5fd",
"name": "Store consumption array just after midnight",
"func": "\nflow.set(\"chartArrayMidnight\",flow.get(\"chartArray\")); \n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 470,
"y": 420,
"wires": [
[
"7bcda922b2d1594f"
]
]
},
{
"id": "e2adb51a9ce23cb8",
"type": "debug",
"z": "cf84e2f1f062c5fd",
"name": "debug 98",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "false",
"statusVal": "",
"statusType": "auto",
"x": 880,
"y": 80,
"wires": []
},
{
"id": "aa09114f67b272a2",
"type": "ui_text",
"z": "cf84e2f1f062c5fd",
"group": "f8a38be7242dbf0c",
"order": 8,
"width": 0,
"height": 0,
"name": "",
"label": "",
"format": "{{msg.payload}}",
"layout": "row-left",
"className": "",
"x": 870,
"y": 140,
"wires": []
},
{
"id": "31e85e5ac2ad949a",
"type": "ui_chart",
"z": "cf84e2f1f062c5fd",
"name": "",
"group": "f8a38be7242dbf0c",
"order": 11,
"width": 0,
"height": 0,
"label": "Energy consumption, every 5 seconds",
"chartType": "line",
"legend": "false",
"xformat": "HH:mm:ss",
"interpolate": "linear",
"nodata": "",
"dot": false,
"ymin": "",
"ymax": "",
"removeOlder": "24",
"removeOlderPoints": "",
"removeOlderUnit": "3600",
"cutout": 0,
"useOneColor": false,
"useUTC": false,
"colors": [
"#ff2600",
"#ff2600",
"#ff7f0e",
"#2ca02c",
"#98df8a",
"#d62728",
"#ff9896",
"#9467bd",
"#c5b0d5"
],
"outputs": 1,
"useDifferentColor": false,
"className": "",
"x": 730,
"y": 280,
"wires": [
[]
]
},
{
"id": "4e0743d61d2f470d",
"type": "change",
"z": "cf84e2f1f062c5fd",
"name": "Reset chart",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "[]",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 790,
"y": 220,
"wires": [
[
"31e85e5ac2ad949a"
]
]
},
{
"id": "cc843489bf797298",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "Reset",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 650,
"y": 220,
"wires": [
[
"4e0743d61d2f470d"
]
]
},
{
"id": "2989ecd9778bae64",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "Just run against the stored \"chartArrayMidnight\"",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 220,
"y": 540,
"wires": [
[
"7bcda922b2d1594f"
]
]
},
{
"id": "7bcda922b2d1594f",
"type": "function",
"z": "cf84e2f1f062c5fd",
"name": "Create a \"csv\" string with yesterdays energy consumption",
"func": "\nlet ackConsumption = 0\nlet consumptionArray = flow.get(\"chartArrayMidnight\")\n\nfor (var i = 0; i < consumptionArray.length; i++) {\n \n ackConsumption = ackConsumption + consumptionArray[i].y\n \n // Output for test purposes\n // msg.payload = i + \";\" + new Date(consumptionArray[i].x).toLocaleString(\"sv\") + \";\" + consumptionArray[i].y;\n // node.send(msg);\n\n}\n\n// Output for test purposes\n// msg.payload = ackConsumption\n// msg.topic = new Date(consumptionArray[1].x).toLocaleDateString(\"sv\")\n\nmsg.payload = new Date(consumptionArray[1].x).toLocaleDateString(\"sv\") + \";\" + ackConsumption\n\nreturn msg;",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 250,
"y": 480,
"wires": [
[
"4331530c531070f6",
"b7342fe3220ca362"
]
]
},
{
"id": "4331530c531070f6",
"type": "debug",
"z": "cf84e2f1f062c5fd",
"name": "debug 99",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 640,
"y": 540,
"wires": []
},
{
"id": "b7342fe3220ca362",
"type": "file",
"z": "cf84e2f1f062c5fd",
"name": "Store yesterdays energy consumption in file",
"filename": "/home/pi/energyConsumption.csv",
"filenameType": "str",
"appendNewline": true,
"createDir": false,
"overwriteFile": "false",
"encoding": "none",
"x": 750,
"y": 480,
"wires": [
[]
]
},
{
"id": "9cb8b7162ced34ef",
"type": "inject",
"z": "cf84e2f1f062c5fd",
"name": "Get current every 5 seconds",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "5",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 160,
"y": 220,
"wires": [
[
"26aead4921bfba9c"
]
]
},
{
"id": "26aead4921bfba9c",
"type": "random",
"z": "cf84e2f1f062c5fd",
"name": "Simulate measured current",
"low": 1,
"high": 10,
"inte": "true",
"property": "payload",
"x": 420,
"y": 220,
"wires": [
[
"962fb775772dbece"
]
]
},
{
"id": "f8a38be7242dbf0c",
"type": "ui_group",
"name": "Energy prices/consumption",
"tab": "09b8121dc5481d72",
"order": 1,
"disp": true,
"width": "15",
"collapse": false,
"className": ""
},
{
"id": "09b8121dc5481d72",
"type": "ui_tab",
"name": "Energy prices/consumption",
"icon": "dashboard",
"order": 1,
"disabled": false,
"hidden": false
}
]
@MatsA
Copy link
Author

MatsA commented Aug 12, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment