# How to sum 2 saved context values

**URL:** <https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430>\
**Category:** General\
**Created:** [22 October 2022 14:35 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430 "2022-10-22T14:35:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [22 October 2022 14:35 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/1 "2022-10-22T14:35:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [22 October 2022 18:20 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/2 "2022-10-22T18:20:52Z")

</div>

> [@Walt\_Dod](#):
>
> ...the best way to add dynamically the 2 stored kWh flow values ?

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...

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

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2022 19:50 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/3 "2022-10-22T19:50:11Z")

</div>

> [@Walt\_Dod](#):
>
> add dynamically the 2 stored kWh flow values

My preference is to use a Join node. See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [22 October 2022 19:55 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/4 "2022-10-22T19:55:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [23 October 2022 00:02 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/5 "2022-10-23T00:02:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![greengolfer](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/greengolfer/32/28276_2.png) [@greengolfer](https://discourse.nodered.org/u/greengolfer)\
**Post date:** [23 October 2022 08:59 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/6 "2022-10-23T08:59:25Z")

</div>

This kind of code goes into a `function` node.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [23 October 2022 09:33 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/7 "2022-10-23T09:33:18Z")

</div>

@Walt_Dod You would learn a lot about how to use node-RED by spending some time reading the [node-RED documentation, including tutorials](https://nodered.org/docs/).

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;

```auto
[{"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"]]}]

```

 ![kwh](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e5a4a8c16f52b87c56034271c14cdab930a32815.jpeg)

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [23 October 2022 09:38 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/8 "2022-10-23T09:38:45Z")

</div>

Thank you, Paul !

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [24 October 2022 21:38 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/9 "2022-10-24T21:38:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [24 October 2022 22:19 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/10 "2022-10-24T22:19:43Z")

</div>

Have a look at post - [Persistent chart node](https://discourse.nodered.org/t/persistent-chart-node/45552)

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [25 October 2022 04:23 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/11 "2022-10-25T04:23:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [25 October 2022 08:12 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/12 "2022-10-25T08:12:30Z")

</div>

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😉

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [25 October 2022 13:56 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/13 "2022-10-25T13:56:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [27 October 2022 09:03 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/14 "2022-10-27T09:03:40Z")

</div>

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

I had a look on

> [@Fun With Persistence?](https://discourse.nodered.org/t/fun-with-persistence/33189/2):
>
> Hi @Nodi.Rubrum that is strange. When I use the same contextStorage setting as you have, then the editor knows I have multiple context stores and the UI reflects that. For example, the Change node (and all other users of the TypedInput widget) offers an option on which store to use: The Context sidebar includes labels for the store a value is in: In the developer console of your browser, can you enter RED.settings.context and see what its value is?

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 October 2022 09:48 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/15 "2022-10-27T09:48:48Z")

</div>

> [@Walt\_Dod](#):
>
> and found i had the same issue as described about the store use

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;

```auto
      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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/06a13216fff73f756dedae82be228d007f867e24.jpeg)

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [27 October 2022 10:14 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/16 "2022-10-27T10:14:14Z")

</div>

many thanks !  
enjoy your day !  
W.

**Envoyé:** jeudi 27 octobre 2022 à 11:59

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [27 October 2022 10:26 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/17 "2022-10-27T10:26:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [27 October 2022 10:31 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/18 "2022-10-27T10:31:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Walt\_Dod](https://avatars.discourse-cdn.com/v4/letter/w/e9a140/32.png) [@Walt\_Dod](https://discourse.nodered.org/u/Walt_Dod)\
**Post date:** [27 October 2022 16:04 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/19 "2022-10-27T16:04:24Z")

</div>

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...

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [27 October 2022 16:35 UTC](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430/20 "2022-10-27T16:35:37Z")

</div>

@Walt_Dod are you the person that opened an issue about this on the node's [GitHub issues page](https://github.com/DeanCording/node-red-contrib-persist/issues/17)?

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.

[Next page](https://discourse.nodered.org/t/how-to-sum-2-saved-context-values/69430.md?page=2)
