How can I store a current value

I am reading the output of my kWh meter (via P1).

This concerns the current kWh value, but it is increasing slowly. How can I freeze the current value when I press a button? I want to place this in the context data, as I want to know my monthly energy consumption.

Hi.

I'm not quite understanding the question.

How good are you with programming/using Node-red?

Alas the only way I can see you doing it is with a function node and a bit of code.

Idea:

You have the normal input to the node and have a second input to cause the value to be captured.

I'm not sure if you hare using a dashboard or not. But I'm guessing you are.

But for the sake of keeping it simple I will only talk at the editor level.

The inject node would send a payload of HOLD. Given the other payloads are numbers.

Doing this here/now.

con

No, hang on.
I'll make the code in the editor and post.

const message = msg.payload
let hold = context.get("hold") || 0

//  If `HOLD`
if (message == "HOLD")
{
    context.set("hold",1)
    return  //  Nothing more to do be hone here/now
}

if (hold == 1)
{
    //  `hold` is set.
    context.set("store",msg.payload)
    context.set("hold",0)   //  stop next message being storred too.
}

return msg

Thoughts.
Rather than context (nodal) make it flow context.
That way you can see the stored value from elsewhere.

Replace: context.set("store" with flow.set("store" for the value.
and context.set("hold" with flow.set("hold"
and context.get("hold" with flow.get("hold"

Then in another part of the flow, you can see the value and wipe the hold condition.

Example flow:

[{"id":"509552482ce62bd8","type":"function","z":"6cf51e98651df17b","name":"The magic happens here.","func":"const message = msg.payload\nlet hold = flow.get(\"hold\") || 0\n\n//  If `HOLD`\nif (message == \"HOLD\")\n{\n    flow.set(\"hold\",1)\n    return  //  Nothing more to do be hone here/now\n}\n\nif (hold == 1)\n{\n    //  `hold` is set.\n    flow.set(\"store\",msg.payload)\n    flow.set(\"hold\",0)   //  stop next message being storred too.\n}\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2600,"y":4210,"wires":[["8c11f2103322c9cd"]]},{"id":"4f1ae4a73d6db203","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2100,"y":4210,"wires":[["4f6b16abf4ef6dc4"]]},{"id":"4f6b16abf4ef6dc4","type":"function","z":"6cf51e98651df17b","name":"random vlue for testing","func":"const randomNumber = Math.floor(Math.random() * 10) + 1\nmsg.payload = randomNumber\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2290,"y":4210,"wires":[["509552482ce62bd8"]]},{"id":"c80e384d4cde4c9e","type":"inject","z":"6cf51e98651df17b","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"HOLD","payloadType":"str","x":2340,"y":4150,"wires":[["509552482ce62bd8"]]},{"id":"f08a667949b8a286","type":"inject","z":"6cf51e98651df17b","name":"Read stored value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":2130,"y":4330,"wires":[["5433167c824dd54f"]]},{"id":"5433167c824dd54f","type":"change","z":"6cf51e98651df17b","name":"get value","rules":[{"t":"set","p":"payload","pt":"msg","to":"store","tot":"flow"},{"t":"set","p":"hold","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":2330,"y":4330,"wires":[["eb35e83ef43ee6e6"]]},{"id":"eb35e83ef43ee6e6","type":"debug","z":"6cf51e98651df17b","name":"Look","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2540,"y":4330,"wires":[]},{"id":"8c11f2103322c9cd","type":"debug","z":"6cf51e98651df17b","name":"to Where ever","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":2870,"y":4210,"wires":[]}]

Here is a SIMILAR node I made that does something like what you described.

[{"id":"acbd4dca.cd318","type":"function","z":"78d17e5b.9e6ff","g":"7e49ed3d.35f9cc","name":"Avg/Max/Min readings","func":"let max = context.get(\"max\") || 0\nlet counter = context.get(\"counter\") || 0\nlet min = context.get(\"min\") || null\nlet avg = context.get(\"avg\") || null\nlet total = context.get(\"total\") || 0\n\n\n//node.warn(\"Initial total reading is \" + total);\n//total = total;\n\nlet new_value = parseInt(msg.payload)\n\nlet msg1 = {}\nlet msg2 = {}\n\nif (msg.reset == true)\n{\n    //  Reset all things\n    node.warn(\"RESET\")\n    context.set(\"max\",0)\n    context.set(\"counter\",0)\n    context.set(\"min\",0)\n    context.set(\"avg\",0)\n    context.set(\"total\",0)\n    return\n}\n\nif (counter === 0)\n{\n    //  First pass\n    context.set(\"max\",new_value)\n    context.set(\"min\",new_value)\n    context.set(\"avg\",new_value)\n    min = new_value\n    max = new_value\n    avg = new_value\n    total = new_value\n}\n\n//new_value = msg.payload;\n\nif (new_value > max)\n{\n    max = new_value\n    context.set(\"max\",new_value)\n}\n\nif (new_value < min)\n{\n\tmin = new_value\n\tcontext.set(\"min\",new_value)\n}\n\ncounter = counter + 1\ncontext.set(\"counter\",counter)\n\ntotal = context.get(\"total\") || 0\n//node.warn(\"context TOTAL is \" + total)\ntotal = total + new_value\n//node.warn(\"TOTAL IS NOW \" + total)\ncontext.set(\"total\",total)\navg = Math.round((total / counter))\n\nmsg.payload = Math.round(avg)\nmsg1.payload = Math.round(max)\nmsg2.payload = Math.round(min)\nreturn [msg,msg1,msg2]","outputs":3,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":3930,"y":1790,"wires":[["b6b37cab.88821","267b42e0.270b16"],["e631bde7.36527","1b588bd.547a2f4"],["e8ec38d8.a3c568","89fd7801.01c23"]],"outputLabels":["AVG","MAX","MIN"]}]

It is a function node that looks at changes and gives the average, max and min values received.

You will have to play with the message to reset/give the values.

But just offering another option.

Thanks,

I have implemented your solution in my situation and it works fine, thanks a lot.

Mark the one you used as the solution.

Helps others.

Indicated with yellow.
I have tested with "Vermogen" (power) what I easely can change.
The solution is simpler than I thought, now I can place that nicely on my dashboard.

Gr. Johannes

Just saying

a problem with this is - as you are wanting to see if there are any changes during the day - if you catch a value of..... (say) 5.5 and it changes during the day and goes back to 5.5 and you look at the captured value: you won't see a change.

For me no problem because I use it for the kWh value and I deliver no energy back to the grid so the value only increase

Ok.

Sorry. I wasn't sure and saw that as a possible problem.

All good.

It was a good point! thanks


My result :slightly_smiling_face: