Incrementing from a base value

Kind of embarrassed to ask this...but I'm a beginner, so....

I want a node to take a start value from the UI and an increment value from the UI.
The first time the node runs, it outputs the start value.
Every subsequent time the node triggers it increments the output my the increment value.

So, for example, start value 5000, increment value 10
First run - the node outputs 5000, second 5010, next 5020 etc..... Really simple I'm sure, but I'm actually struggling with the concept. Any suggestions?

Hello and welcome to the Node-RED forum.

This might help to get you started. A very simple flow using UI components.
Enter your values (start and increment) in the form in the UI, then click submit.
Each time you click 'Trigger_the_node' it should increment after showing the initial value.
i.e. It outputs the current value, then increments the value as per your stated requirement.

There are probably lots of other ways to implement this - as people will probably tell you.

[{"id":"3f5d9f56.573d8","type":"tab","label":"Form_Values","disabled":false,"info":""},{"id":"f9d88234.a8ee58","type":"ui_form","z":"3f5d9f56.573d8","name":"","label":"","group":"4bd2080b.20314","order":3,"width":0,"height":0,"options":[{"label":"Start value","value":"start","type":"number","required":true,"rows":null},{"label":"Increment","value":"inc","type":"number","required":true,"rows":null}],"formValue":{"start":"","inc":""},"payload":"","submit":"submit","cancel":"cancel","topic":"topic","topicType":"msg","splitLayout":"","className":"","x":230,"y":180,"wires":[["fe6d43c8.4e02f"]]},{"id":"10e0fa0a.d6cae6","type":"ui_text","z":"3f5d9f56.573d8","group":"4bd2080b.20314","order":1,"width":0,"height":0,"name":"","label":"Current value","format":"{{msg.payload}}","layout":"row-spread","className":"","x":640,"y":240,"wires":[]},{"id":"66a073af.018764","type":"ui_button","z":"3f5d9f56.573d8","name":"","group":"4bd2080b.20314","order":2,"width":0,"height":0,"passthru":false,"label":"Trigger_the_node","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"","payloadType":"str","topic":"topic","topicType":"msg","x":270,"y":240,"wires":[["2008ef73.c2d818"]]},{"id":"fe6d43c8.4e02f","type":"function","z":"3f5d9f56.573d8","name":"","func":"flow.set(\"start_value\",msg.payload.start);\nflow.set(\"increment\",msg.payload.inc);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":180,"wires":[[]]},{"id":"2008ef73.c2d818","type":"function","z":"3f5d9f56.573d8","name":"","func":"let start = flow.get(\"start_value\");\nlet increment = flow.get(\"increment\");\nmsg.payload = start;\nstart = start + increment;\nflow.set(\"start_value\",start); \n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":240,"wires":[["10e0fa0a.d6cae6"]]},{"id":"4bd2080b.20314","type":"ui_group","name":"Controls","tab":"ff2c1506.6f0d9","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"ff2c1506.6f0d9","type":"ui_tab","name":"Form_Inc_Display","icon":"dashboard","order":25,"disabled":false,"hidden":false}]

That is absolutely perfect - thanks, I'd started learning how the context.set and get worked and figured out a clunky way to make that work but your approach is much neater.
I'd tried setting the initial variables in the "setup" tab of the function node but these don't get transferred to the code in the "Function" tab which resulted in me having to design some cludge to get around that.
Separating them into 2 separate flows and using the flow.set and flow.get avoids the problem I was having and is neat. Thanks again.

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