Power consumption of a heatpump

Hello

My heat pump sends once a day the accumulated power consumption. I want to write the power consumption of the last three days in a bar chart.
So first of all I have to subtract the value from two days ago from the value from yesterday to get the daily consumption and store this fro three days. I don`t need to store all values in node red because I send them to KNX and a TWS also.

Can you help me how to subract the value 2 days ago from the value yesterday?

Best Regards
Erwin

Hello,
first I would use the Parser "csv" and the "write file" node to store the data in a csv file.
From there I would write a function to process the data.

PSEUDO CODE
length = getNumberOfRowsInCSVFile()
result = []
for i=0 to length do
   diff = valueOfLine[i]-valueOfLine[i-1]
   result.append({
      day: dateOfLine[i],
      value: diff}
    )

msg.payload = result
return msg

You can store the last 3 days in context storage with the last accumulated reading, then every day you get a new reading create a chart .

The inject simulates an input of accumulated value for testing, just connect to your daily power consumption input. Will take 3 inputs to fully build chart.

Change 1st rule in change node to increase number of days to display.

[{"id":"bb6ff273.d3e588","type":"inject","z":"9790f611.6dd06","name":"random rise","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"$sum([$flowContext(\"heatpump.previous\")]) + $ceil($random()*20) + 5","payloadType":"jsonata","x":140,"y":640,"wires":[["5fb3fdbb.e5daec","d339ef33.7e1088"]]},{"id":"5fb3fdbb.e5daec","type":"debug","z":"9790f611.6dd06","name":"","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":260,"y":580,"wires":[]},{"id":"d339ef33.7e1088","type":"change","z":"9790f611.6dd06","name":"","rules":[{"t":"set","p":"display","pt":"msg","to":"3","tot":"num"},{"t":"set","p":"now","pt":"msg","to":"payload","tot":"msg"},{"t":"set","p":"payload","pt":"msg","to":"[$append(\t   [$flowContext(\"heatpump.last3\")],\t   [\t       {\t           \"timestamp\": $now(),\t           \"value\": $$.payload - $sum([$flowContext(\"heatpump.previous\")])\t       }\t   ]\t)[[-$$.display..-1]]]","tot":"jsonata"},{"t":"set","p":"heatpump.last3","pt":"flow","to":"payload","tot":"msg"},{"t":"move","p":"now","pt":"msg","to":"heatpump.previous","tot":"flow"},{"t":"set","p":"payload","pt":"msg","to":"[\t    {\t        \"labels\": [$$.payload.timestamp.$split($, \"T\")[0]],\t        \"data\": [\t            [\t                $$.payload.value\t            ]\t        ],\t        \"series\":[\"Power consumption\"]\t    }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":410,"y":640,"wires":[["363b4c50.e9ceec","7ce5a93d.95db28"]]},{"id":"363b4c50.e9ceec","type":"debug","z":"9790f611.6dd06","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":640,"wires":[]},{"id":"7ce5a93d.95db28","type":"ui_chart","z":"9790f611.6dd06","name":"","group":"2d4fe667.28f8ba","order":11,"width":0,"height":0,"label":"chart","chartType":"bar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"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":590,"y":680,"wires":[[]]},{"id":"2d4fe667.28f8ba","type":"ui_group","name":"demo","tab":"1caa8458.b17814","order":1,"disp":true,"width":"12","collapse":false},{"id":"1caa8458.b17814","type":"ui_tab","name":"Demo","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Hello E1cid

thanks a lot. With the random rise it ist working fine and does exactly what I need. Doesnot matter when I cannot understand the expressions.

Still one problem exists: I don`t know where to connect the KNX device node from which the data will be recvd.

The KNX device should connect where random rise connects, it should feed in the accumulated power consumption once a day and needs to be an integer.

[edit]
The first expression takes the stored array (last3) and adds the new object, containing the time and value., it then removes the first array item if greater than 3 or msg.display.

The second expression creates the payload array for the bar chart.