Here is a simple flow for traffic lights tested sucessfully. Its using a loop without parameter passing. Edit the state machine line 30 from “return null” to “return msg” will screw up node.js runtime. Never try at a used system as NodeRed crashs. Port 1881 never comes back. Also had the case, that complete Venus did a reboot what shouldnt be true.
[{"id":"be3ab1ef.3f0a68","type":"tab","label":"Traffic Lights using delays and events","disabled":false,"info":"This is a state machine example for Node Red. Graphical FSM representation is done using Mermaid code what what is supported up from NodeRed V3.1. To be recognized, the mermaid code needs to be included in three tilde characters followed by the keyword mermaid. This tilde character is only available at the key beside backspace and can be accessed by pressing the shift key only. \n\nTo get a good FSM design, never use composite states, nor fork and join. \n\n```mermaid\nstateDiagram-v2\n [*] --> red\n red --> red_yellow : time\n red_yellow --> green : time\n green --> yellow : time\n yellow --> red : time\n```\n","env":[]},{"id":"743e732.c12228c","type":"function","z":"be3ab1ef.3f0a68","name":"Decode RED light","func":"var LocalState = flow.get(\"GlobalState\");\n\nif (LocalState === 0 || LocalState == 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 }\nreturn msg;\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":120,"wires":[[]]},{"id":"778d9f53.8e5f7","type":"function","z":"be3ab1ef.3f0a68","name":"Decode YELLOW light","func":"var LocalState = flow.get(\"GlobalState\");\n\nif (LocalState == 1 || LocalState == 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,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":180,"wires":[[]]},{"id":"c8a94827.acb49","type":"function","z":"be3ab1ef.3f0a68","name":"Decode GREEN light","func":"var LocalState = flow.get(\"GlobalState\");\n\nif (LocalState == 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,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":980,"y":240,"wires":[[]]},{"id":"28cc6bcb.920094","type":"function","z":"be3ab1ef.3f0a68","name":"Finite State Machine","func":"// FSM implemented by switch / case \n\nvar LocalState = flow.get(\"GlobalState\");\n\nswitch (LocalState)\n {\n case 0: // red\n node.send( {payload:LocalState, delay:1000}); // start red_yellow time\n LocalState = 1; // next state red_yellow \n break;\n \n case 1:\n LocalState = 2; // red_yellow\n node.send( {payload:LocalState, delay:3000}); // green time\n break;\n \n case 2:\n LocalState = 3; // green\n node.send( {payload:LocalState, delay:1000}); // yellow time\n break;\n \n case 3: // yellow\n LocalState = 0; // red time\n node.send( {payload:LocalState, delay:5000});\n break;\n }\n \nflow.set(\"GlobalState\", LocalState); // save state information\nnode.status({text:\"State = \" + LocalState}); // display state information\nreturn null;\n","outputs":"1","timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":700,"y":180,"wires":[["743e732.c12228c","778d9f53.8e5f7","c8a94827.acb49","82ab245c.6aa488"]]},{"id":"82ab245c.6aa488","type":"delay","z":"be3ab1ef.3f0a68","name":"Timer","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":470,"y":180,"wires":[["28cc6bcb.920094"]]},{"id":"43e78726a5c704c9","type":"inject","z":"be3ab1ef.3f0a68","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":"0","topic":"","payload":"true","payloadType":"bool","x":330,"y":120,"wires":[["9f03fd51549f6c27"]]},{"id":"9f03fd51549f6c27","type":"function","z":"be3ab1ef.3f0a68","name":"Init","func":"// Initialisation of State Maschine\n// State information needs to be global scope as the state needs to be\n// non volatile until next execution cycle of FSM function\n// Initialisation using the || character inside FSM ist not working for whatever reason\n// Therefore we use explicit initialisation function executed once at startup\n// FSM always starts after deploy and runs in a loop forever\n// For safety reasons, traffic lights should start at red what is state 0\n// First red time needs to\n\nvar LocalState = flow.get(\"GlobalState\");\nLocalState = 3;\nflow.set(\"GlobalState\", LocalState);\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":470,"y":120,"wires":[["28cc6bcb.920094"]]}]