Add new value to last value

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 :slight_smile:

Cheers

Gareth

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)

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

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

[{"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.

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

Hi,

Great stuff.

Thank you very much.

@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. :weary:

Thanks

Gareth

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;

Thank you so much!

I owe you a beer :slight_smile:

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