# Add new value to last value

**URL:** <https://discourse.nodered.org/t/add-new-value-to-last-value/49698>\
**Category:** Developing Nodes\
**Created:** [13 August 2021 20:30 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698 "2021-08-13T20:30:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gjones84](https://avatars.discourse-cdn.com/v4/letter/g/7cd45c/32.png) [@gjones84](https://discourse.nodered.org/u/gjones84)\
**Post date:** [13 August 2021 20:30 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/1 "2021-08-13T20:30:38Z")

</div>

Hi All,

I wonder if someone can help me with a simple solution to my control loop. I have a series on values coming into my control loop and I want add the newest value to the last value. My idea was to set new value as a global but I can figure out how to add it to the previous?

I have created a simple flow of what I am trying to achieve. So basically if inject the value 5 into 'some kind of node' I want the value 5 to pass through it. Then say the next new value is 10 I want the message payload to be 15. Then if next new value was 100 I want the msg payload to bee 115 for example. Bit like a counter relay. Add new value to old.

[{"id":"66a95929.4357b8","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"67e87232.3e560c","type":"inject","z":"66a95929.4357b8","name":"","topic":"","payload":"5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":100,"wires":[["832c7dbb.7020d"]]},{"id":"19025231.6df0ce","type":"inject","z":"66a95929.4357b8","name":"","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":180,"wires":[["832c7dbb.7020d"]]},{"id":"c0beaf7c.e8a16","type":"inject","z":"66a95929.4357b8","name":"","topic":"","payload":"100","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":260,"wires":[["832c7dbb.7020d"]]},{"id":"90e8f252.80601","type":"debug","z":"66a95929.4357b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":970,"y":180,"wires":},{"id":"832c7dbb.7020d","type":"change","z":"66a95929.4357b8","name":"","rules":[{"t":"set","p":"1stnewpayloadnumber","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":180,"wires":[["90e8f252.80601"]]},{"id":"3018bcdd.ced2b4","type":"inject","z":"66a95929.4357b8","name":"","topic":"","payload":"newpayloadnumber","payloadType":"global","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":380,"wires":[["9f0152a3.b7883"]]},{"id":"9f0152a3.b7883","type":"debug","z":"66a95929.4357b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":750,"y":380,"wires":}]

Any help would be much appreciated 🙂

Cheers

Gareth

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [13 August 2021 21:20 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/2 "2021-08-13T21:20:50Z")

</div>

The code you posted is incomplete, and it would have been better posted within code tabs, like this (which I hope does what you are trying to achieve)

```auto
[{"id":"7bb9dc47f0e0ca71","type":"inject","z":"117fb44a.f7cc54","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":150,"y":80,"wires":[["4d13faa716d9d957"]]},{"id":"400c8f2ec5d7faf1","type":"inject","z":"117fb44a.f7cc54","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":150,"y":120,"wires":[["4d13faa716d9d957"]]},{"id":"4ac06b012cfeeb3f","type":"inject","z":"117fb44a.f7cc54","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":150,"y":160,"wires":[["4d13faa716d9d957"]]},{"id":"26d281030d25319b","type":"inject","z":"117fb44a.f7cc54","name":"Reset","props":[{"p":"payload"},{"p":"reset","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":150,"y":40,"wires":[["4d13faa716d9d957"]]},{"id":"4d13faa716d9d957","type":"function","z":"117fb44a.f7cc54","name":"","func":"var total = context.get('total');\nif (msg.reset) {\n total = 0;\n}\nelse {\n total = total + msg.payload\n}\ncontext.set('total', total);\nmsg.payload = total;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":100,"wires":[["a9fa20649bc65dfb"]]},{"id":"a9fa20649bc65dfb","type":"debug","z":"117fb44a.f7cc54","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":600,"y":100,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [13 August 2021 21:45 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/3 "2021-08-13T21:45:52Z")

</div>

And here is how to access and set global context storage using a change node.

```auto
[{"id":"b6d070d5.e5a44","type":"inject","z":"66a95929.4357b8","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":40,"wires":[["56fb9882.1327f"]]},{"id":"56fb9882.1327f","type":"change","z":"66a95929.4357b8","name":"","rules":[{"t":"set","p":"total","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":60,"wires":[[]]},{"id":"67e87232.3e560c","type":"inject","z":"66a95929.4357b8","name":"","props":[{"p":"payload","v":"5","vt":"num"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":130,"y":80,"wires":[["832c7dbb.7020d"]]},{"id":"19025231.6df0ce","type":"inject","z":"66a95929.4357b8","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":130,"y":180,"wires":[["832c7dbb.7020d"]]},{"id":"c0beaf7c.e8a16","type":"inject","z":"66a95929.4357b8","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":130,"y":260,"wires":[["832c7dbb.7020d"]]},{"id":"832c7dbb.7020d","type":"change","z":"66a95929.4357b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum([$$.payload , $globalContext(\"total\")])","tot":"jsonata"},{"t":"set","p":"total","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":160,"wires":[["90e8f252.80601"]]},{"id":"3018bcdd.ced2b4","type":"inject","z":"66a95929.4357b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"total","payloadType":"global","x":130,"y":380,"wires":[["9f0152a3.b7883"]]},{"id":"9f0152a3.b7883","type":"debug","z":"66a95929.4357b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":380,"wires":[]}]

```

To add them you need to use JSONata, and $sum(), as when you first start the flow the global will be undefined. If you try to add an undefined variable to a number the result would be undefined. $sum() will ignore undefined variable.

---

<div class="post-metadata">

**Author:** ![gjones84](https://avatars.discourse-cdn.com/v4/letter/g/7cd45c/32.png) [@gjones84](https://discourse.nodered.org/u/gjones84)\
**Post date:** [14 August 2021 06:15 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/4 "2021-08-14T06:15:53Z")

</div>

> [@E1cid](#):
>
> ```auto
> [{"id":"b6d070d5.e5a44","type":"inject","z":"66a95929.4357b8","name":"reset","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":40,"wires":[["56fb9882.1327f"]]},{"id":"56fb9882.1327f","type":"change","z":"66a95929.4357b8","name":"","rules":[{"t":"set","p":"total","pt":"global","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":60,"wires":[[]]},{"id":"67e87232.3e560c","type":"inject","z":"66a95929.4357b8","name":"","props":[{"p":"payload","v":"5","vt":"num"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":130,"y":80,"wires":[["832c7dbb.7020d"]]},{"id":"19025231.6df0ce","type":"inject","z":"66a95929.4357b8","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":130,"y":180,"wires":[["832c7dbb.7020d"]]},{"id":"c0beaf7c.e8a16","type":"inject","z":"66a95929.4357b8","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":130,"y":260,"wires":[["832c7dbb.7020d"]]},{"id":"832c7dbb.7020d","type":"change","z":"66a95929.4357b8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$sum([$.payload , $globalContext(\"total\")])","tot":"jsonata"},{"t":"set","p":"total","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":160,"wires":[["90e8f252.80601"]]},{"id":"3018bcdd.ced2b4","type":"inject","z":"66a95929.4357b8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"total","payloadType":"global","x":130,"y":380,"wires":[["9f0152a3.b7883"]]},{"id":"9f0152a3.b7883","type":"debug","z":"66a95929.4357b8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":490,"y":380,"wires":[]}]
> 
> ```

Hi,

Thanks for replying. Apologies for the incorrect format will make sure its in that format if I post again. I just tried your code and yes that's exactly what I wanted.

Thank you very much!

Cheers

Gareth

---

<div class="post-metadata">

**Author:** ![gjones84](https://avatars.discourse-cdn.com/v4/letter/g/7cd45c/32.png) [@gjones84](https://discourse.nodered.org/u/gjones84)\
**Post date:** [14 August 2021 06:17 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/5 "2021-08-14T06:17:07Z")

</div>

Hi,

Great stuff.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![gjones84](https://avatars.discourse-cdn.com/v4/letter/g/7cd45c/32.png) [@gjones84](https://discourse.nodered.org/u/gjones84)\
**Post date:** [14 August 2021 17:04 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/6 "2021-08-14T17:04:58Z")

</div>

@jbudd how hard would it be to limit or range this function node script so that the counter or total value limits are between -500 and +2000?

If the input values still try to add more numbers to counter in the + direction it still outputs 2000. Same with -500.

I've been trying to figure it out but again struggling. 😩

Thanks

Gareth

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [14 August 2021 17:22 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/7 "2021-08-14T17:22:08Z")

</div>

Like this? Untested.

var max = 2000;  
var min = -500;  
var total = context.get('total');  
if (msg.reset) {  
total = 0;  
}  
else {  
total = total + msg.payload  
}  
if (total \> max) total = max;  
if (total \< min) total = min;  
context.set('total', total);  
msg.payload = total;  
return msg;

---

<div class="post-metadata">

**Author:** ![gjones84](https://avatars.discourse-cdn.com/v4/letter/g/7cd45c/32.png) [@gjones84](https://discourse.nodered.org/u/gjones84)\
**Post date:** [14 August 2021 17:29 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/8 "2021-08-14T17:29:27Z")

</div>

Thank you so much!

I owe you a beer 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [28 August 2021 17:29 UTC](https://discourse.nodered.org/t/add-new-value-to-last-value/49698/9 "2021-08-28T17:29:56Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
