Need help to use if function in Node Red

Good Day ,

I’m trying to use Node Red to design a simple Traffic lights with external switch, what I did is shown in fig 1 attached, but it seem it’s not stable and it will stop running at some point .
I need to use (if function ) to enable me to use the second out of the first countdown timer to trigger the second timer as shown in Fig 2 when the output reach the zero value , I am unable to use this program as a real logic programing .

Any help with any idea and the required code will highly appreciated .

Welcome to the forum!

It's helpful to explain what the flow is suppose in detail. i.e. what should happen when the input is a 1 and what should happen when it is a 0?

What do you mean by it seem it’s not stable and it will stop running at some point?

You could use a pair of inject buttons for testing and I'd suggest adding in several debug nodes so you can see what the data is that is passed thru the flow.

If you want someone to help, make sure to answer all questions that are asked and it's also a good idea to export the flow and attach it to a reply (read this thread first)

Here's a flow for a UK-style traffic light coded using a Finite State Machine (FSM) I built for my IoT students.


The classic coding style using a Switch and Case construct was used for the FSM.

Although the LEDs were located on a breadboard, driven by a Wemos D1 Mini via MQTT from Node-RED running on a RPi, I'm sure your could 'rip-out' that part and replace it with your RPi GPIO pins.

[{"id":"425ee0b.7ea6ba","type":"function","z":"56ceee45.9a659","name":"Index the State Counter","func":"// In this particular situation the state_counter increments by unity on each clock pulse\n// so you could code it as below\n\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nif (fsm_state > 2)\n   {fsm_state = 0;}\nelse\n   {fsm_state = fsm_state + 1;}\n\nflow.set(\"state_counter\", fsm_state);\nmsg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\nreturn msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":450,"y":400,"wires":[[]]},{"id":"c3b14f5b.7113b8","type":"function","z":"56ceee45.9a659","name":"Decode RED light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state === 0 || fsm_state == 1)\n   {msg.payload = 1;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Red ON\"});\n   }\n   \nelse\n   {msg.payload = 0;\n    node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":730,"y":180,"wires":[["8d90b811.f0f118"]]},{"id":"c385a9ef.be9378","type":"function","z":"56ceee45.9a659","name":"Decode YELLOW light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 1 || fsm_state == 3)\n   {msg.payload = 1;\n   node.status({fill:\"yellow\",shape:\"dot\",text:\"Yellow ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});}\n\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":240,"wires":[["e835800e.edbc78"]]},{"id":"5a6a9553.df0cb4","type":"function","z":"56ceee45.9a659","name":"Decode GREEN light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 2)\n   {msg.payload = 1;\n   node.status({fill:\"green\",shape:\"dot\",text:\"Green ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":300,"wires":[["2c6640cf.74028"]]},{"id":"8d90b811.f0f118","type":"mqtt out","z":"56ceee45.9a659","name":"","topic":"node45/gpio/14","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1010,"y":180,"wires":[]},{"id":"e835800e.edbc78","type":"mqtt out","z":"56ceee45.9a659","name":"","topic":"node45/gpio/12","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1010,"y":240,"wires":[]},{"id":"2c6640cf.74028","type":"mqtt out","z":"56ceee45.9a659","name":"","topic":"node45/gpio/13","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1010,"y":300,"wires":[]},{"id":"aaac1f2.80f33e","type":"inject","z":"56ceee45.9a659","name":"Trigger every 3 seconds","props":[{"p":"payload"}],"repeat":"3","crontab":"","once":true,"onceDelay":"","topic":"","payload":"1","payloadType":"num","x":190,"y":240,"wires":[["cd2a1c3e.320b8"]]},{"id":"cd2a1c3e.320b8","type":"function","z":"56ceee45.9a659","name":"Index the State Counter","func":"// Here is the classical way of coding a state machine.\n// It uses a 'case construct' to check the current state and then set the next state.\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nswitch (fsm_state)\n    {\n        case 0:\n            fsm_state = 1;\n            break;\n            \n        case 1:\n            fsm_state = 2;\n            break;\n        \n        case 2:\n            fsm_state = 3;\n            break;\n            \n        case 3:\n            fsm_state = 0;\n            break;\n    }\n    \nflow.set(\"state_counter\", fsm_state);\nmsg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\nreturn msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":450,"y":240,"wires":[["c3b14f5b.7113b8","c385a9ef.be9378","5a6a9553.df0cb4"]]},{"id":"efa4c220.ba809","type":"comment","z":"56ceee45.9a659","name":"Simple way to code the state machine","info":"","x":450,"y":360,"wires":[]},{"id":"e54be0b4.20e2d","type":"comment","z":"56ceee45.9a659","name":"Coding a state machine using a Switch/Case construct","info":"","x":360,"y":180,"wires":[]},{"id":"e3b52ac5.f48f28","type":"mqtt-broker","broker":"192.168.1.138","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
1 Like

I am building a traffic light, and I have to use a button or switch to start it and the same switch to stop it, it should not stop until I toggle the switch. In my design the problem is ; I can have it work , but at some point the countdown that I am using will mess up and the traffic will stop by itself. other times it's just working perfectly, so I am not what is the problem and if I need to use a function block to fix it.

Thanks for the reply, my problem with your design is when I replace the trigger with a button (rpi GPIO in) , I cannot get the traffic to work! if you try it you would see just the red LED will work and latch. I am looking for something that make the traffic to start as soon as I press the (button or switch) and keep on working till I press it again to stop it. Any help will highly appreciated.

so where is the flow?

In my flow the Inject node is acting as a clock-pulse generator.

What you need to do is alter the flow to make a 'gated flow'.
e.g. Have your GPIO pin enable or inhibit the clock-pulse being passed-on.

Try this version which uses a toggle action (as you mentioned you needed in your reply).

[{"id":"743e732.c12228c","type":"function","z":"be3ab1ef.3f0a68","name":"Decode RED light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state === 0 || fsm_state == 1)\n   {msg.payload = 1;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Red ON\"});\n   }\n   \nelse\n   {msg.payload = 0;\n    node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":810,"y":220,"wires":[["f508540f.461658"]]},{"id":"778d9f53.8e5f7","type":"function","z":"be3ab1ef.3f0a68","name":"Decode YELLOW light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 1 || fsm_state == 3)\n   {msg.payload = 1;\n   node.status({fill:\"yellow\",shape:\"dot\",text:\"Yellow ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});}\n\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":280,"wires":[["20ee32d4.59580e"]]},{"id":"c8a94827.acb49","type":"function","z":"be3ab1ef.3f0a68","name":"Decode GREEN light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 2)\n   {msg.payload = 1;\n   node.status({fill:\"green\",shape:\"dot\",text:\"Green ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":820,"y":340,"wires":[["2769607f.eb8618"]]},{"id":"f508540f.461658","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/14","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1090,"y":220,"wires":[]},{"id":"20ee32d4.59580e","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/12","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1090,"y":280,"wires":[]},{"id":"2769607f.eb8618","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/13","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1090,"y":340,"wires":[]},{"id":"8fe8aaf9.4bc99","type":"inject","z":"be3ab1ef.3f0a68","name":"Trigger every 3 seconds","props":[{"p":"payload"}],"repeat":"3","crontab":"","once":true,"onceDelay":"","topic":"","payload":"1","payloadType":"num","x":230,"y":120,"wires":[["3361daf1.f94576"]]},{"id":"28cc6bcb.920094","type":"function","z":"be3ab1ef.3f0a68","name":"Index the State Counter","func":"// Here is the classical way of coding a state machine.\n// It uses a 'case construct' to check the current state and then set the next state.\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nswitch (fsm_state)\n    {\n        case 0:\n            fsm_state = 1;\n            break;\n            \n        case 1:\n            fsm_state = 2;\n            break;\n        \n        case 2:\n            fsm_state = 3;\n            break;\n            \n        case 3:\n            fsm_state = 0;\n            break;\n    }\n    \nflow.set(\"state_counter\", fsm_state);\nmsg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\nreturn msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":530,"y":280,"wires":[["743e732.c12228c","778d9f53.8e5f7","c8a94827.acb49"]]},{"id":"f9440b3d.324378","type":"comment","z":"be3ab1ef.3f0a68","name":"Coding a state machine using a Switch/Case construct","info":"","x":640,"y":100,"wires":[]},{"id":"634074c5.d267d4","type":"inject","z":"be3ab1ef.3f0a68","name":"Toggle button","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":340,"wires":[["ee02a850.8eee8"]]},{"id":"ee02a850.8eee8","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\n\nif (status == \"stopped\") {\n    flow.set(\"status\", \"running\");\n    node.status({text:\"State = running\"});\n}\nelse {\n    flow.set(\"status\", \"stopped\");\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":320,"y":340,"wires":[[]]},{"id":"3361daf1.f94576","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\nif (status == \"running\") {\n     node.status({text:\"State = running\"});\n    return msg;\n}\nelse {\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":390,"y":200,"wires":[["28cc6bcb.920094"]]},{"id":"e3b52ac5.f48f28","type":"mqtt-broker","broker":"192.168.1.138","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

Please go back and reread my original post.

It looks like this design is working as I need it, I just changed it to the US traffic light. I just have 2 other questions; the first one is I want to stop the traffic completely (not just pause it) when I turn off the switch. And the 2nd question is : Is there a way to control the timing of each LED color , for example if I want the RED and Green to work for 6 seconds and the yellow to work for 3 second?

Just had a few spare minutes to take a look at having different delays between transitions.

[{"id":"743e732.c12228c","type":"function","z":"be3ab1ef.3f0a68","name":"Decode RED light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state === 0 || fsm_state == 1)\n   {msg.payload = 1;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Red ON\"});\n   }\n   \nelse\n   {msg.payload = 0;\n    node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":120,"wires":[["f508540f.461658"]]},{"id":"778d9f53.8e5f7","type":"function","z":"be3ab1ef.3f0a68","name":"Decode YELLOW light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 1 || fsm_state == 3)\n   {msg.payload = 1;\n   node.status({fill:\"yellow\",shape:\"dot\",text:\"Yellow ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});}\n\nreturn msg;","outputs":1,"noerr":0,"x":1020,"y":180,"wires":[["20ee32d4.59580e"]]},{"id":"c8a94827.acb49","type":"function","z":"be3ab1ef.3f0a68","name":"Decode GREEN light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 2)\n   {msg.payload = 1;\n   node.status({fill:\"green\",shape:\"dot\",text:\"Green ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":1020,"y":240,"wires":[["2769607f.eb8618"]]},{"id":"f508540f.461658","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/14","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":120,"wires":[]},{"id":"20ee32d4.59580e","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/12","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":180,"wires":[]},{"id":"2769607f.eb8618","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/13","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":240,"wires":[]},{"id":"28cc6bcb.920094","type":"function","z":"be3ab1ef.3f0a68","name":"Finite State Machine","func":"// Change the values of delay to suit your application\n\n\n// Here is the classical way of coding a state machine.\n// It uses a 'case construct' to check the current state and then set the next state.\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nswitch (fsm_state)\n    {\n        case 0:\n            fsm_state = 1; // Next state\n            node.send( {payload:fsm_state, delay:5000});  // delay is in milliseconds\n            break;\n            \n        case 1:\n            fsm_state = 2;\n            node.send( {payload:fsm_state, delay:1000});\n            break;\n        \n        case 2:\n            fsm_state = 3;\n            node.send( {payload:fsm_state, delay:1000});\n            break;\n            \n        case 3:\n            fsm_state = 0;\n            node.send( {payload:fsm_state, delay:1000});\n            break;\n    }\n    \nflow.set(\"state_counter\", fsm_state);\n// msg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\n// return msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":720,"y":180,"wires":[["743e732.c12228c","778d9f53.8e5f7","c8a94827.acb49","82ab245c.6aa488","bb9e3b99.c35f78"]]},{"id":"77e8a5e.82598dc","type":"comment","z":"be3ab1ef.3f0a68","name":"Checkout the new code in here","info":"","x":730,"y":140,"wires":[]},{"id":"634074c5.d267d4","type":"inject","z":"be3ab1ef.3f0a68","name":"Toggle button","props":[{"p":"payload"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":180,"wires":[["ee02a850.8eee8","82ab245c.6aa488"]]},{"id":"ee02a850.8eee8","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\n\nif (status == \"stopped\") {\n    flow.set(\"status\", \"running\");\n    node.status({text:\"State = running\"});\n}\nelse {\n    flow.set(\"status\", \"stopped\");\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":320,"wires":[[]]},{"id":"3361daf1.f94576","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\nif (status == \"running\") {\n     node.status({text:\"State = running\"});\n    return msg;\n}\nelse {\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":180,"wires":[["28cc6bcb.920094"]]},{"id":"82ab245c.6aa488","type":"delay","z":"be3ab1ef.3f0a68","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":180,"wires":[["3361daf1.f94576"]]},{"id":"bb9e3b99.c35f78","type":"debug","z":"be3ab1ef.3f0a68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":60,"wires":[]},{"id":"98ee2ab4.5b3e38","type":"comment","z":"be3ab1ef.3f0a68","name":"Override delay with msg.delay","info":"","x":420,"y":240,"wires":[]},{"id":"e3b52ac5.f48f28","type":"mqtt-broker","broker":"192.168.1.138","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]

The code has been modified in the FSM node so it outputs msg.delay which is used to set the variable delay in the delay node. You need to change the option in the Delay node to... "Override delay with msg.delay"

Please note:
The delay values in the FSM are in milliseconds.

If I had more spare time I would introduce a variable called 'defaultDelay' so you could use that in the paths of the FSM where you didn't need a different delay. Hope that makes sense.

Well that was easier than I thought, so here's a flow with a 'defaultDelay' of 1 second.

[{"id":"743e732.c12228c","type":"function","z":"be3ab1ef.3f0a68","name":"Decode RED light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state === 0 || fsm_state == 1)\n   {msg.payload = 1;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Red ON\"});\n   }\n   \nelse\n   {msg.payload = 0;\n    node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1010,"y":120,"wires":[["f508540f.461658"]]},{"id":"778d9f53.8e5f7","type":"function","z":"be3ab1ef.3f0a68","name":"Decode YELLOW light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 1 || fsm_state == 3)\n   {msg.payload = 1;\n   node.status({fill:\"yellow\",shape:\"dot\",text:\"Yellow ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});}\n\nreturn msg;","outputs":1,"noerr":0,"x":1020,"y":180,"wires":[["20ee32d4.59580e"]]},{"id":"c8a94827.acb49","type":"function","z":"be3ab1ef.3f0a68","name":"Decode GREEN light","func":"var fsm_state = flow.get(\"state_counter\");\n\nif (fsm_state == 2)\n   {msg.payload = 1;\n   node.status({fill:\"green\",shape:\"dot\",text:\"Green ON\"});\n   }\nelse\n   {msg.payload = 0;\n   node.status({});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":1020,"y":240,"wires":[["2769607f.eb8618"]]},{"id":"f508540f.461658","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/14","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":120,"wires":[]},{"id":"20ee32d4.59580e","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/12","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":180,"wires":[]},{"id":"2769607f.eb8618","type":"mqtt out","z":"be3ab1ef.3f0a68","name":"","topic":"node45/gpio/13","qos":"","retain":"","broker":"e3b52ac5.f48f28","x":1240,"y":240,"wires":[]},{"id":"28cc6bcb.920094","type":"function","z":"be3ab1ef.3f0a68","name":"Finite State Machine","func":"// Change the values of delay to suit your application\n\n\n// Here is the classical way of coding a state machine.\n// It uses a 'case construct' to check the current state and then set the next state.\n\nvar fsm_state = flow.get(\"state_counter\") || 0;\n\nvar defaultDelay = flow.get(\"defaultDelay\");\n\nswitch (fsm_state)\n    {\n        case 0:\n            fsm_state = 1; // Next state\n            node.send( {payload:fsm_state, delay:5000});  // delay is in milliseconds\n            break;\n            \n        case 1:\n            fsm_state = 2;\n            node.send( {payload:fsm_state, delay:defaultDelay});\n            break;\n        \n        case 2:\n            fsm_state = 3;\n            node.send( {payload:fsm_state, delay:defaultDelay});\n            break;\n            \n        case 3:\n            fsm_state = 0;\n            node.send( {payload:fsm_state, delay:defaultDelay});\n            break;\n    }\n    \nflow.set(\"state_counter\", fsm_state);\n// msg.payload = fsm_state;\nnode.status({text:\"State counter = \" + fsm_state});\n\n// return msg;","outputs":"1","noerr":0,"initialize":"","finalize":"","x":720,"y":180,"wires":[["743e732.c12228c","778d9f53.8e5f7","c8a94827.acb49","82ab245c.6aa488","bb9e3b99.c35f78"]]},{"id":"77e8a5e.82598dc","type":"comment","z":"be3ab1ef.3f0a68","name":"Checkout the new code in here","info":"","x":730,"y":140,"wires":[]},{"id":"634074c5.d267d4","type":"inject","z":"be3ab1ef.3f0a68","name":"Toggle button","props":[{"p":"payload"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":180,"wires":[["ee02a850.8eee8","82ab245c.6aa488"]]},{"id":"ee02a850.8eee8","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\n\nif (status == \"stopped\") {\n    flow.set(\"status\", \"running\");\n    node.status({text:\"State = running\"});\n}\nelse {\n    flow.set(\"status\", \"stopped\");\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":360,"y":320,"wires":[[]]},{"id":"3361daf1.f94576","type":"function","z":"be3ab1ef.3f0a68","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\nif (status == \"running\") {\n     node.status({text:\"State = running\"});\n    return msg;\n}\nelse {\n     node.status({text:\"State = STOPPED\"});\n}\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":520,"y":180,"wires":[["28cc6bcb.920094"]]},{"id":"82ab245c.6aa488","type":"delay","z":"be3ab1ef.3f0a68","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":360,"y":180,"wires":[["3361daf1.f94576"]]},{"id":"bb9e3b99.c35f78","type":"debug","z":"be3ab1ef.3f0a68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":990,"y":60,"wires":[]},{"id":"98ee2ab4.5b3e38","type":"comment","z":"be3ab1ef.3f0a68","name":"Override delay with msg.delay","info":"","x":420,"y":240,"wires":[]},{"id":"203dae68.e2be8a","type":"inject","z":"be3ab1ef.3f0a68","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":150,"y":80,"wires":[["fd179166.5703b"]]},{"id":"fd179166.5703b","type":"function","z":"be3ab1ef.3f0a68","name":"Set default delay to 1 second","func":"flow.set(\"defaultDelay\", 1000);\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":370,"y":80,"wires":[[]]},{"id":"e3b52ac5.f48f28","type":"mqtt-broker","broker":"192.168.1.138","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
4 Likes

Thank you, this is awesome :slightly_smiling_face:

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