Daily energy usage plus costs

Hi,

I have a hassio running with node-red with an usb cable attached to my kamstrup 162 smart meter.
This is configured in home assistant and I can see the data and display it on the ui.
What I want to do is make a function:

Start measuring gas usage on 0:00 and end at 23:59, and display this on the ui on daily basis.
Calculate the costs from the start at 0:00 and also display that.

e.g.
0:00 gas meter: 10000m3
0:20 gas meter: 10030m3
calc delta, multiply by costs per m3, display usage plus costs

This should be fairly simple, but storing vars with time, calc delta and displaying it, really i have no clue on how to do this. I tried to put something together but I am missing just crucial knowledge to build this kind of function.
So hopefully someone is able to help me.

My end goal is to have a similar display as "TOON" (dutch smart meter) but ofcourse better :slight_smile:

Thank you in advance.

This thread might be a good starting point...

1 Like

That was very quick, thank you.
I imported the flow and it looks very promising, I think I can change it to my needs.
When it is working I will post the flow into this topic.

Hi,

I promised to return the favor and share the adapted code.
Some parts are in Dutch and the dashboard could be made better, but it is working.
So if you are using home assistant with an usb smart meter cable then this is for you.

Import this code

[{"id":"88fa0739.2e94a8","type":"tab","label":"Energie verbruik","disabled":false,"info":""},{"id":"9ca1a2c.bafe76","type":"inject","z":"88fa0739.2e94a8","name":"Start of the day","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":150,"y":100,"wires":[["90799776.afa048","de17266b.9b4b18","9cc1f9a1.27cf18"]]},{"id":"90799776.afa048","type":"change","z":"88fa0739.2e94a8","name":"Gas","rules":[{"t":"set","p":"payload","pt":"msg","to":"dailyusage","tot":"global"},{"t":"set","p":"dailyusage","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":80,"wires":[["159ea88.8d6ec58"]]},{"id":"cb270f6d.24d6e","type":"debug","z":"88fa0739.2e94a8","name":"Last Days","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1180,"y":80,"wires":[]},{"id":"675fb147.46d4c","type":"comment","z":"88fa0739.2e94a8","name":"Dagelijks verbruik en reset","info":"","x":150,"y":20,"wires":[]},{"id":"159ea88.8d6ec58","type":"function","z":"88fa0739.2e94a8","name":"msg.payload = "Total usage today is "+msg.payload","func":"msg.payload = "Total usage today is "+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":80,"wires":[["cb270f6d.24d6e"]]},{"id":"2d264d5.9fe0bb2","type":"poll-state","z":"88fa0739.2e94a8","name":"Dal Tarief","server":"4463f6c4.b567b8","updateinterval":"","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.power_consumption_low","x":120,"y":360,"wires":[["47f912f1.565eec"]]},{"id":"47f912f1.565eec","type":"change","z":"88fa0739.2e94a8","name":"Convert","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":360,"wires":[["4351a607.4d2808"]]},{"id":"4351a607.4d2808","type":"function","z":"88fa0739.2e94a8","name":"Power usage","func":"//Grab vorig dal from global variable for total usage\nvorigdal = global.get("totaaldal")||0;\n\n//How much usage since the last update\nperiodedal = msg.payload - vorigdal;\n\n//Grab vorig dal from global variable for daily usage\ndagelijksdal = global.get("dagelijksdal")||0;\n\ndagelijksdal = dagelijksdal + periodedal;\n\n//Update the global variables\nglobal.set("dagelijksdal",dagelijksdal );\nglobal.set("totaaldal", msg.payload);\n\nmsg.payload = parseFloat(dagelijksdal).toFixed(3)\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":360,"wires":[["ef7ea479.50ec18"]]},{"id":"d5c007ef.eb55b8","type":"ui_gauge","z":"88fa0739.2e94a8","name":"VerbruikMeter","group":"88cbb72.df21948","order":1,"width":"3","height":"3","gtype":"donut","title":"Verbruik live","label":"W","format":"{{value}}","min":0,"max":"10000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1500","seg2":"2500","x":520,"y":260,"wires":[]},{"id":"ef7ea479.50ec18","type":"function","z":"88fa0739.2e94a8","name":"KWh naar Euro","func":"kwhprijs = 0.23 //23 cent\ncalc = parseFloat(msg.payload * kwhprijs).toFixed(2);\nvar msg1 = {payload:calc};\nreturn [msg1];","outputs":1,"noerr":0,"x":760,"y":340,"wires":[["244e1b79.28b1a4"]]},{"id":"244e1b79.28b1a4","type":"ui_text","z":"88fa0739.2e94a8","group":"88cbb72.df21948","order":5,"width":0,"height":0,"name":"Dal Euro","label":"Dal","format":"{{msg.payload}} Euro","layout":"row-center","x":740,"y":380,"wires":[]},{"id":"939b58c5.b66018","type":"ui_chart","z":"88fa0739.2e94a8","name":"VerbruikGrafiek","group":"88cbb72.df21948","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"4000","removeOlder":"4","removeOlderPoints":"10000","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":520,"y":300,"wires":[[],[]]},{"id":"de17266b.9b4b18","type":"change","z":"88fa0739.2e94a8","name":"Dal","rules":[{"t":"set","p":"payload","pt":"msg","to":"dagelijksdal","tot":"global"},{"t":"set","p":"dagelijksdal","pt":"global","to":"0","tot":"num"},{"t":"set","p":"vorigdal","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":120,"wires":[["ed2ef7e.b41ce08"]]},{"id":"4d94f71a.934e88","type":"debug","z":"88fa0739.2e94a8","name":"Last Days","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1180,"y":120,"wires":[]},{"id":"ed2ef7e.b41ce08","type":"function","z":"88fa0739.2e94a8","name":"msg.payload = "Total usage today is "+msg.payload","func":"msg.payload = "Total usage today is "+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":120,"wires":[["4d94f71a.934e88"]]},{"id":"1094f192.43f43e","type":"change","z":"88fa0739.2e94a8","name":"Convert","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":280,"wires":[["d5c007ef.eb55b8","939b58c5.b66018"]]},{"id":"f6258372.f7858","type":"poll-state","z":"88fa0739.2e94a8","name":"Gas","server":"4463f6c4.b567b8","updateinterval":"","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.gas_consumption","x":110,"y":720,"wires":[["94be3317.721d1"]]},{"id":"94be3317.721d1","type":"change","z":"88fa0739.2e94a8","name":"Convert","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":720,"wires":[["a43aa836.e30f78"]]},{"id":"a43aa836.e30f78","type":"function","z":"88fa0739.2e94a8","name":"Gas usage","func":"//Grab previous value from global variable for total usage\npreviousvalue = global.get("totalvalue")||0;\n\n//How much usage since the last update\nperiodvalue = msg.payload - previousvalue;\n\n//Grab previous value from global variable for daily usage\ndailyvalue = global.get("dailyusage")||0;\n\ndailyvalue = dailyvalue + periodvalue;\n\n//Update the global variables.\nglobal.set("dailyusage",dailyvalue );\nglobal.set("totalvalue", msg.payload);\n\nmsg.payload = parseFloat(dailyvalue).toFixed(3)\n\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":720,"wires":[["6bcc3e1a.d41b9","c8e1c340.52433","450350a7.1bd2e","6cae5772.5418d8"]]},{"id":"450350a7.1bd2e","type":"ui_gauge","z":"88fa0739.2e94a8","name":"VerbruikMeter","group":"5dd90078.dcc25","order":1,"width":"3","height":"3","gtype":"donut","title":"Verbruik","label":"m3","format":"{{value}}","min":0,"max":"3","colors":["#00b500","#e6e600","#ca3838"],"seg1":"1000","seg2":"2000","x":780,"y":660,"wires":[]},{"id":"6cae5772.5418d8","type":"ui_chart","z":"88fa0739.2e94a8","name":"VerbruikGrafiek","group":"5dd90078.dcc25","order":2,"width":0,"height":0,"label":"","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"3","removeOlder":"4","removeOlderPoints":"10000","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":780,"y":700,"wires":[[],[]]},{"id":"c8e1c340.52433","type":"ui_text","z":"88fa0739.2e94a8","group":"5dd90078.dcc25","order":0,"width":0,"height":0,"name":"Verbruik Gas m3","label":"Verbr. vandaag","format":"{{msg.payload}} m3","layout":"row-spread","x":790,"y":740,"wires":[]},{"id":"6bcc3e1a.d41b9","type":"function","z":"88fa0739.2e94a8","name":"m3 naar Euro","func":"gasprijs = 0.71 //0.7060 cent\ncalc = parseFloat(msg.payload * gasprijs).toFixed(2);\nvar msg1 = {payload:calc};\nreturn [msg1];","outputs":1,"noerr":0,"x":780,"y":780,"wires":[["f380c562.31b788"]]},{"id":"f380c562.31b788","type":"ui_text","z":"88fa0739.2e94a8","group":"5dd90078.dcc25","order":0,"width":0,"height":0,"name":"Verbruik Euro","label":"Kosten vandaag","format":"{{msg.payload}} Euro","layout":"row-spread","x":780,"y":840,"wires":[]},{"id":"fcf6f7a3.5eb198","type":"poll-state","z":"88fa0739.2e94a8","name":"Elektra verbruik","server":"4463f6c4.b567b8","updateinterval":"","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.power_consumption","x":140,"y":280,"wires":[["1094f192.43f43e"]]},{"id":"5e57a412.1e905c","type":"poll-state","z":"88fa0739.2e94a8","name":"Normaal Tarief","server":"4463f6c4.b567b8","updateinterval":"","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.power_consumption_normal","x":130,"y":500,"wires":[["8a920a62.d4b098"]]},{"id":"8a920a62.d4b098","type":"change","z":"88fa0739.2e94a8","name":"Convert","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":500,"wires":[["c5ace9b1.f967d8"]]},{"id":"c5ace9b1.f967d8","type":"function","z":"88fa0739.2e94a8","name":"Power usage","func":"//Grab vorig normaal from global variable for total usage\nvorignormaal = global.get("totaalnormaal")||0;\n\n//How much usage since the last update\nperiodenormaal = msg.payload - vorignormaal;\n\n//Grab vorig normaal from global variable for daily usage\ndagelijksnormaal = global.get("dagelijksnormaal")||0;\n\ndagelijksnormaal = dagelijksnormaal + periodenormaal;\n\n//Update the global variables\nglobal.set("dagelijksnormaal",dagelijksnormaal );\nglobal.set("totaalnormaal", msg.payload);\n\nmsg.payload = parseFloat(dagelijksnormaal).toFixed(3)\n\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":500,"wires":[["c3bc14e5.1a6888"]]},{"id":"c3bc14e5.1a6888","type":"function","z":"88fa0739.2e94a8","name":"KWh naar Euro","func":"kwhprijs = 0.23 //23 cent\ncalc = parseFloat(msg.payload * kwhprijs).toFixed(2);\nvar msg1 = {payload:calc};\nreturn [msg1];","outputs":1,"noerr":0,"x":760,"y":500,"wires":[["397c1ee5.ddbed2"]]},{"id":"397c1ee5.ddbed2","type":"ui_text","z":"88fa0739.2e94a8","group":"88cbb72.df21948","order":7,"width":0,"height":0,"name":"Normaal Euro","label":"Normaal","format":"{{msg.payload}} Euro","layout":"row-center","x":760,"y":540,"wires":[]},{"id":"9cc1f9a1.27cf18","type":"change","z":"88fa0739.2e94a8","name":"Normaal","rules":[{"t":"set","p":"payload","pt":"msg","to":"dagelijksnormaal","tot":"global"},{"t":"set","p":"dagelijksnormaal","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":160,"wires":[["399201e2.91d64e"]]},{"id":"399201e2.91d64e","type":"function","z":"88fa0739.2e94a8","name":"msg.payload = "Total usage today is "+msg.payload","func":"msg.payload = "Total usage today is "+msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":880,"y":160,"wires":[["49c330da.d8856"]]},{"id":"49c330da.d8856","type":"debug","z":"88fa0739.2e94a8","name":"Last Days","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":1180,"y":160,"wires":[]},{"id":"536c5c6a.56c044","type":"inject","z":"88fa0739.2e94a8","name":"Start of the day","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"00 00 * * *","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["de17266b.9b4b18"]]},{"id":"4463f6c4.b567b8","type":"server","z":"","name":"Home Assistant","legacy":true},{"id":"88cbb72.df21948","type":"ui_group","z":"","name":"Elektra","tab":"bfc64ecd.f4499","order":8,"disp":true,"width":"6","collapse":false},{"id":"5dd90078.dcc25","type":"ui_group","z":"","name":"Gas","tab":"bfc64ecd.f4499","order":9,"disp":true,"width":"6","collapse":false},{"id":"bfc64ecd.f4499","type":"ui_tab","z":"","name":"Energie","icon":"dashboard","order":1}]

1 Like

:slight_smile: You could add the flow to http://flows.node-red.org

1 Like

Ok, I will do that :slight_smile:

Check this out:
https://flows.nodered.org/flow/09f81780458d23156b74bf3f1168bbd8

1 Like