How to sum 2 saved context values

Hi Paul,
If I may ask, can you give me some hint or a link about the best way to add dynamically the 2 stored kWh flow values ?
Regards,
W.

Envoyé: samedi 22 octobre 2022 à 10:29

Numerous ways to do that... but if you use two differently named flow context stores with your kWh flows - energyValData & energyValStore, something like this should work...

var a = flow.get('energyValData') || 0;
var b = flow.get('energyValStore') || 0;
msg.payload = a+b;
return msg

My preference is to use a Join node. See this article in the cookbook for an example of how to join messages into one object.

@Walt_Dod @Colin
These posts moved to their own topic, as they are general node-RED questions and off-topic from original topic.

Hi Paul,
Sorry to ask, but it is only 2 days I am in Node-Red,
In what kind of node do I include the code you mentioned ?
W.

This kind of code goes into a function node.

@Walt_Dod You would learn a lot about how to use node-RED by spending some time reading the node-RED documentation, including tutorials.

If you wanted to add together the 2 stored values, you would need to add the code that I posted above in a function node to retrieve the values, and add them together.
However, if you wanted to add the payload values together instead, read the cookbook link and use a Join node instead, as Colin has said above

Something like this;

[{"id":"47b769c5.cb0e28","type":"join","z":"8ec88bd9c36c3f6b","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":535,"y":690,"wires":[["79c640dfcddc2f07"]]},{"id":"f9afb265.b11b7","type":"debug","z":"8ec88bd9c36c3f6b","name":"Sum","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":780,"y":690,"wires":[]},{"id":"72985628cd69b236","type":"function","z":"8ec88bd9c36c3f6b","name":"Store kwh","func":"var inputVal = msg.payload;\nif (inputVal == \"reset\"){\n context.set(\"energyValA\",0); \n } else {\nvar savedVal = context.get('energyValA')||0;\nsavedVal += inputVal;\ncontext.set(\"energyValA\",savedVal);\nmsg.payload = savedVal;\nmsg.topic = \"topicA\"\nreturn msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":385,"y":650,"wires":[["2f7c403dc696f005","47b769c5.cb0e28"]]},{"id":"f0342efc1df3ec36","type":"watt2kwh","z":"8ec88bd9c36c3f6b","format":"kwh","maximum":"60","maximumunit":"mins","name":"","x":235,"y":650,"wires":[["72985628cd69b236"]]},{"id":"8a6d0f53c496353c","type":"inject","z":"8ec88bd9c36c3f6b","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1000","payloadType":"num","x":100,"y":650,"wires":[["f0342efc1df3ec36"]]},{"id":"2f7c403dc696f005","type":"debug","z":"8ec88bd9c36c3f6b","name":"energyValA","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":650,"wires":[]},{"id":"400ccdd38c87a58a","type":"function","z":"8ec88bd9c36c3f6b","name":"Store kwh","func":"var inputVal = msg.payload;\nif (inputVal == \"reset\") {\n    context.set(\"energyValB\", 0);\n} else {\n    var savedVal = context.get('energyValB') || 0;\n    savedVal += inputVal;\n    context.set(\"energyValB\", savedVal);\n    msg.payload = savedVal;\n    msg.topic = \"topicB\"\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":385,"y":730,"wires":[["6c9caf2dc266bf9a","47b769c5.cb0e28"]]},{"id":"38d80f0408df092e","type":"watt2kwh","z":"8ec88bd9c36c3f6b","format":"kwh","maximum":"60","maximumunit":"mins","name":"","x":235,"y":730,"wires":[["400ccdd38c87a58a"]]},{"id":"3fd908d1c1f43cda","type":"inject","z":"8ec88bd9c36c3f6b","name":"","props":[{"p":"payload"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2000","payloadType":"num","x":100,"y":730,"wires":[["38d80f0408df092e"]]},{"id":"6c9caf2dc266bf9a","type":"debug","z":"8ec88bd9c36c3f6b","name":"energyValB","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":800,"y":730,"wires":[]},{"id":"79c640dfcddc2f07","type":"function","z":"8ec88bd9c36c3f6b","name":"add","func":"let result = msg.payload.topicA + msg.payload.topicB\nmsg.payload = result\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":655,"y":690,"wires":[["f9afb265.b11b7"]]},{"id":"cd800c1383fdf78e","type":"inject","z":"8ec88bd9c36c3f6b","name":"RESET","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"reset","payloadType":"str","x":225,"y":690,"wires":[["72985628cd69b236","400ccdd38c87a58a"]]}]

Thank you, Paul !

Hi Paul,
Maybe you can help with the following : when redeploying the flows, the watt2kWh values are lost on the dashboard and back to zero.
Is there a way to store them so that the dashboard shall not be altered ?
Rgds,
W.

Have a look at post - Persistent chart node

Thank you Paul,
Would you mind sending the Jason code for both inject & save context nodes ?

It appears that there are many different kind of configurations for the cache used
and I cannot figure out how to manage it.
Regards,
W.

Envoyé: mardi 25 octobre 2022 à 00:29

Why not try and work it out yourself.

Have a look at the output from your chart node, you need to save it, and then when you restart the flow or reboot, inject the 'saved' file back into the chart node, to restore the chart.
The screenshot in my last link of the flow should give you a clue

It's the best way to learn😉

1 Like

Hi Paul,
As the watt2kwh node was set with your help in context mode to avoid data influence from one node to the next,
it seems that I only had to uncomment

contextStorage: {
default: {
module:"localfilesystem"
},
},

in settings.js to have the stored kwh data saved in the dashboard when redeploying...

W.

Envoyé: mardi 25 octobre 2022 à 10:22

1 Like

Hi Paul,
The uncommenting of my last mail (see below) did work for a while but not any more...

I had a look on

and found i had the same issue as described about the store use, whether default or persistent, which do not appear in the change node settings on my screen. I tried deleting the coma in the settings.json file as shown, whith no avail...

Maybe you can help ?

Regards,
Walt

Hi Paul,
As the watt2kwh node was set with your help in context mode to avoid data influence from one node to the next,
it seems that I only had to uncomment

contextStorage: {
default: {
module:"localfilesystem"
},
},

in settings.js to have the stored kwh data saved in the dashboard when redeploying...

W.

Envoyé: mardi 25 octobre 2022 à 10:22

It's not the same, as you have only described one store in settings.js.
If you want to have 2 stores, one in memory, and another on disk (persistent) then change your settings.js file like this;

      contextStorage: {
      default: "RAM",
                RAM: { module: 'memory' },
                Disk: { module: 'localfilesystem'
                },
        },

...and restart node-RED.

Then whenever you set or get context, you specify which you want to use -

flow.set('Energy', energy, 'Disk') or flow.set('Energy', energy, 'RAM')

flow.get('Energy', 'Disk') or flow.get('Energy', 'RAM')

You will then also see the option in your change nodes -

ted

1 Like

many thanks !
enjoy your day !
W.

Envoyé: jeudi 27 octobre 2022 à 11:59

Hi Paul,
I followed your advices but the RAM/Disk options doesn't appear in the change node...
W.

Envoyé: jeudi 27 octobre 2022 à 11:59

@Walt_Dod - Have you restarted node-RED?
...and possibly refreshed your browser (F5)

When I try to set the change node, I get this message :
slight_smile: 27/10/2022 17:58:09 msg : string[174]
"[RED.events] Deprecated use of "nodes-started" event from "/data/home/nodered/.node-red/node_modules/node-red-contrib-persist/persist.js:164:20". Use "flows:started" instead."

I cannot find this path on my computer...

@Walt_Dod are you the person that opened an issue about this on the node's GitHub issues page?

Note that this node (node-red-contrib-persist) hasn't been updated in over two years and that is no what @Paul-Reed was showing you. If you want to try Paul's suggestions, I suggest you remove that node first.