Need help to use if function in Node Red

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