New to Node red variables

Hello all!

I am new to using variables in Node Red, what i am trying to achieve in from the flow is that if msg.payload == 1 it will trigger a stopwatch function, but it needs flow.count = "start" to run the stopwatch function and i have difficulty using functions to achieve this. Below has a picture of what the change node looks like to activate the stopwatch.

Thanks all!

[{"id":"bbef0e1c0398f259","type":"tab","label":"timer flow","disabled":false,"info":"","env":[]},{"id":"98e93f5391583a13","type":"change","z":"bbef0e1c0398f259","name":"Set msg.topic to time create global: \"time\"","rules":[{"t":"set","p":"topic","pt":"msg","to":"time","tot":"str"},{"t":"set","p":"time","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1080,"y":560,"wires":[["d05db47648a35594"]]},{"id":"8ccc6c672fd7b94c","type":"ui_text","z":"bbef0e1c0398f259","group":"ab52afafd221cb44","order":18,"width":"0","height":"0","name":"timer ","label":"timer ","format":"{{msg.payload}}","layout":"row-spread","x":930,"y":760,"wires":[]},{"id":"e6e04031a25e9d08","type":"inject","z":"bbef0e1c0398f259","name":"every 1 second","props":[{"p":"payload"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":560,"wires":[["257272cce4977fd1"]]},{"id":"257272cce4977fd1","type":"switch","z":"bbef0e1c0398f259","name":"flow.clock_state == \"start\" ?","property":"count","propertyType":"flow","rules":[{"t":"eq","v":"start","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":580,"y":560,"wires":[["6c247e742d6f3ebd"]]},{"id":"6c247e742d6f3ebd","type":"function","z":"bbef0e1c0398f259","name":"run timer","func":"let clock_value = flow.get(\"clock_value\") || 0;\n\nclock_value++; \n\nif (clock_value < 0) clock_value = 0;\n\nflow.set(\"clock_value\", clock_value);\n\nlet mins = Math.floor(clock_value/60);\n\nlet secs = clock_value % 60;\n\nmsg.payload = (mins + (secs / 100.0)).toFixed(2)\n\nmsg.payload = Number(msg.payload);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":560,"wires":[["98e93f5391583a13","8ccc6c672fd7b94c"]]},{"id":"a58365feb0200449","type":"change","z":"bbef0e1c0398f259","name":"count stop pause timer","rules":[{"t":"set","p":"count","pt":"flow","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":640,"wires":[[]]},{"id":"87d3b392e47cdac0","type":"inject","z":"bbef0e1c0398f259","name":"pause","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":640,"wires":[["680b023a52c6ee1c"]]},{"id":"d836af656bff1338","type":"change","z":"bbef0e1c0398f259","name":"clock_state run // start / resume","rules":[{"t":"set","p":"count","pt":"flow","to":"start","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":600,"wires":[[]]},{"id":"b1fe52212c8fd269","type":"inject","z":"bbef0e1c0398f259","name":"start / resume","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":110,"y":600,"wires":[["e1275d7c1aaba0a9"]]},{"id":"7a8a515e5a31c857","type":"change","z":"bbef0e1c0398f259","name":"clear and count reset","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"},{"t":"set","p":"count","pt":"flow","to":"stop","tot":"str"},{"t":"set","p":"clock_value","pt":"flow","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":760,"wires":[["8ccc6c672fd7b94c"]]},{"id":"758acb44348da998","type":"change","z":"bbef0e1c0398f259","name":"reset count value","rules":[{"t":"set","p":"clock_value","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":680,"wires":[[]]},{"id":"5269217d1f0e56f1","type":"inject","z":"bbef0e1c0398f259","name":"reset count value","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":680,"wires":[["bc16bf379be45477"]]},{"id":"43d97c5f93cb9347","type":"change","z":"bbef0e1c0398f259","name":" stop and count reset ","rules":[{"t":"set","p":"clock_value","pt":"flow","to":"0","tot":"num"},{"t":"set","p":"count","pt":"flow","to":"stop","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":720,"wires":[[]]},{"id":"8e9a0a37a19129fc","type":"inject","z":"bbef0e1c0398f259","name":"0 and stop","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":720,"wires":[["cbe4ac5e7719713b"]]},{"id":"5aeb1eec0f5a4436","type":"inject","z":"bbef0e1c0398f259","name":"hard reset","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":760,"wires":[["a3598e79ed33312f"]]},{"id":"a3598e79ed33312f","type":"ui_button","z":"bbef0e1c0398f259","name":"clear timer","group":"ab52afafd221cb44","order":24,"width":2,"height":1,"passthru":true,"label":"clear timer","tooltip":"","color":"","bgcolor":"","icon":"","payload":"restart","payloadType":"str","topic":"control","topicType":"str","x":310,"y":760,"wires":[["7a8a515e5a31c857"]]},{"id":"680b023a52c6ee1c","type":"ui_button","z":"bbef0e1c0398f259","name":"pause timer","group":"ab52afafd221cb44","order":24,"width":2,"height":1,"passthru":true,"label":"pause timer","tooltip":"","color":"","bgcolor":"","icon":"","payload":"restart","payloadType":"str","topic":"control","topicType":"str","x":310,"y":640,"wires":[["a58365feb0200449"]]},{"id":"e1275d7c1aaba0a9","type":"ui_button","z":"bbef0e1c0398f259","name":"start / resume timer","group":"ab52afafd221cb44","order":24,"width":2,"height":1,"passthru":true,"label":"resume timer","tooltip":"","color":"","bgcolor":"","icon":"","payload":"restart","payloadType":"str","topic":"control","topicType":"str","x":330,"y":600,"wires":[["d836af656bff1338"]]},{"id":"bc16bf379be45477","type":"ui_button","z":"bbef0e1c0398f259","name":"reset count value","group":"ab52afafd221cb44","order":24,"width":2,"height":1,"passthru":true,"label":"reset count value","tooltip":"","color":"","bgcolor":"","icon":"","payload":"restart","payloadType":"str","topic":"control","topicType":"str","x":330,"y":680,"wires":[["758acb44348da998"]]},{"id":"cbe4ac5e7719713b","type":"ui_button","z":"bbef0e1c0398f259","name":"soft counter reset","group":"ab52afafd221cb44","order":24,"width":2,"height":1,"passthru":true,"label":"soft counter reset","tooltip":"","color":"","bgcolor":"","icon":"","payload":"restart","payloadType":"str","topic":"control","topicType":"str","x":330,"y":720,"wires":[["43d97c5f93cb9347"]]},{"id":"d05db47648a35594","type":"debug","z":"bbef0e1c0398f259","name":"debug 2","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1280,"y":480,"wires":[]},{"id":"c843cc41274ca89a","type":"function","z":"bbef0e1c0398f259","name":"function 3","func":"\nif(msg.payload == 1){\n    flow.set((flow.count), 'start')\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":420,"wires":[["f2e66d10f9023d4e"]]},{"id":"3a75065d20664913","type":"inject","z":"bbef0e1c0398f259","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":530,"y":420,"wires":[["c843cc41274ca89a"]]},{"id":"f2e66d10f9023d4e","type":"debug","z":"bbef0e1c0398f259","name":"debug 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":420,"wires":[]},{"id":"ab52afafd221cb44","type":"ui_group","name":"Timer","tab":"061e64803467b898","order":1,"disp":true,"width":"6","collapse":false},{"id":"061e64803467b898","type":"ui_tab","name":"timer","icon":"dashboard","disabled":false,"hidden":false}]

image

In a function use

// flow.set("count", "start")
// flow.get("count")

1 Like

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