# Need help to use if function in Node Red

**URL:** https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091
**Category:** Developing Nodes
**Created:** [17 November 2020 19:31 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091 "2020-11-17T19:31:15Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [17 November 2020 19:31 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/1 "2020-11-17T19:31:15Z")

</div>

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 .

 ![Traffic Light Fig 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03ef4af416eb601cc1195ae280b30cdce68fb261.jpeg)

 ![Traffic Light Fig 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7323dca17bd1611c4d396a8af2ddf548566cb9a.jpeg)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [18 November 2020 10:49 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/2 "2020-11-18T10:49:27Z")

</div>

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](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) first)

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [18 November 2020 11:50 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/3 "2020-11-18T11:50:21Z")

</div>

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

 ![Screen Shot 11-18-20 at 11.41 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/0/e0f8a686e469222cad4b83c457e60f861dbf010a.jpeg)  
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.

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

```

---

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [18 November 2020 14:45 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/4 "2020-11-18T14:45:40Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [18 November 2020 14:57 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/5 "2020-11-18T14:57:53Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [18 November 2020 15:14 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/6 "2020-11-18T15:14:13Z")

</div>

so where is the flow?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [18 November 2020 15:15 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/7 "2020-11-18T15:15:04Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [18 November 2020 15:31 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/8 "2020-11-18T15:31:20Z")

</div>

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

 ![Screen Shot 11-18-20 at 03.29 PM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/9/99be1ba01fbece39d33ccb72de6e22e0b11d4b56.jpeg)

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

```

---

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [18 November 2020 15:44 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/9 "2020-11-18T15:44:30Z")

</div>

![Traffic Light Fig 2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/7/f7323dca17bd1611c4d396a8af2ddf548566cb9a.jpeg)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [18 November 2020 16:11 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/10 "2020-11-18T16:11:37Z")

</div>

Please go back and reread my original post.

---

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [18 November 2020 16:24 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/11 "2020-11-18T16:24:09Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dynamicdave](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dynamicdave/32/96_2.png) [@dynamicdave](https://discourse.nodered.org/u/dynamicdave)
#### Post date: [19 November 2020 09:19 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/12 "2020-11-19T09:19:23Z")

</div>

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

 ![Screen Shot 11-19-20 at 09.10 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/571afe6fc0b7c2ef1c0f00d37c380fcd39b6a927.jpeg)

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

 ![Screen Shot 11-19-20 at 09.29 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/d/ad4fd1b8b6dec9ea577e55babe48e43266f43c7a.jpeg)

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

```

---

<div class="post-metadata">

### Author: ![hadeel1985](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@hadeel1985](https://discourse.nodered.org/u/hadeel1985)
#### Post date: [19 November 2020 18:32 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/13 "2020-11-19T18:32:12Z")

</div>

Thank you, this is awesome 🙂

---

<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: [3 December 2020 18:32 UTC](https://discourse.nodered.org/t/need-help-to-use-if-function-in-node-red/36091/14 "2020-12-03T18:32:16Z")

</div>

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