Plant Factory for agriculture

Good evening, I'm safrizal. I am having problems in making a program on node-red. I am asking for help from friends and masters here if you can help my problems. So I made the program to command in a sequence more precisely for the arrangement of 7 valves which will be opened and closed alternately or simultaneously. simply I want to do command A after that to command B and so on. I want when command B is done then command A stops to neutral and does not affect command B. I ask for help if anyone understands this? Thank you for your help, may God Bless you, amen

This is my code :

[{"id":"500b7da7.1772d4","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"c6baab38.f37698","type":"ui_switch","z":"500b7da7.1772d4","name":"","label":"Alirkan","group":"9a986029.f4a13","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","style":"","onvalue":"buka","onvalueType":"str","onicon":"","oncolor":"","offvalue":"tutup","offvalueType":"str","officon":"","offcolor":"","x":796.6666259765625,"y":430,"wires":[["8a7668a3.45e4b","a54a9e88.2c5418","3774b0b5.e4e908","8805d3f1.dbdc1"]]},{"id":"f4821b4.fc58a68","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve Pengaduk 40","pin":"40","set":"","level":"0","freq":"","out":"out","x":1233.6666259765625,"y":390,"wires":},{"id":"ca5cf503.2451b8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve Keatas 38","pin":"38","set":"","level":"0","freq":"","out":"out","x":1203.9998779296875,"y":266.6666488647461,"wires":},{"id":"8805d3f1.dbdc1","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve buka/',payload:'true'}; \n } \nreturn null;\n\n","outputs":1,"noerr":0,"x":1002.6666259765625,"y":317,"wires":[["ca5cf503.2451b8"]]},{"id":"3774b0b5.e4e908","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;\n","outputs":1,"noerr":0,"x":1020.6666259765625,"y":357,"wires":[["f4821b4.fc58a68","a669cce2.5d89a8","89c5e5e7.87cff","eacbd777.12e028","93c65eb4.855748"]]},{"id":"a54a9e88.2c5418","type":"delay","z":"500b7da7.1772d4","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1022.6666259765625,"y":454,"wires":[["3521a5fe.f05bca","2b302558.b9526a","f923495d.c3bb5"]]},{"id":"87c0d93c.5a1098","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 2 . 32","pin":"32","set":"","level":"0","freq":"","out":"out","x":1380.3333740234375,"y":471.66668701171875,"wires":},{"id":"1eae8e24.1542f2","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 1. 36","pin":"36","set":"","level":"0","freq":"","out":"out","x":1340.3333740234375,"y":434.3333435058594,"wires":},{"id":"3521a5fe.f05bca","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve buka/',payload:'true'}; \n } \nreturn null;\n\n","outputs":1,"noerr":0,"x":1189.6666259765625,"y":468.0000305175781,"wires":[["1eae8e24.1542f2"]]},{"id":"2b302558.b9526a","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;\n","outputs":1,"noerr":0,"x":1165.6666259765625,"y":526,"wires":[["87c0d93c.5a1098","74f1ed36.0bb0b4","731eee1d.7b08e8"]]},{"id":"f923495d.c3bb5","type":"delay","z":"500b7da7.1772d4","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1080.6666259765625,"y":639,"wires":[["b6762b76.9e146","5f33e06f.abd868","824e836f.f894a8"]]},{"id":"328abc7b.ef8694","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 2 . 32","pin":"32","set":"","level":"0","freq":"","out":"out","x":1499.9998779296875,"y":582,"wires":},{"id":"a6e2e09c.18f35","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 3. 22","pin":"22","set":"","level":"0","freq":"","out":"out","x":1535.9998779296875,"y":640.9999694824219,"wires":},{"id":"b6762b76.9e146","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve buka/',payload:'true'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1266.6666259765625,"y":649,"wires":[["328abc7b.ef8694"]]},{"id":"5f33e06f.abd868","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1273.6666259765625,"y":696,"wires":[["a6e2e09c.18f35","ea9e35f6.7dadc8","8154a7fd.641708"]]},{"id":"ea9e35f6.7dadc8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 1. 36","pin":"36","set":"","level":"0","freq":"","out":"out","x":1546.9998779296875,"y":689.5,"wires":},{"id":"824e836f.f894a8","type":"delay","z":"500b7da7.1772d4","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1091.6666259765625,"y":781,"wires":[["84efa09d.9749a","29656942.aa8196","bb9f208d.56c868"]]},{"id":"a28c78f7.dd7d6","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 2 . 32","pin":"32","set":"","level":"0","freq":"","out":"out","x":1539.6666259765625,"y":852,"wires":},{"id":"a6b21886.2c0ae8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 3. 22","pin":"22","set":"","level":"0","freq":"","out":"out","x":1558.6666259765625,"y":787,"wires":},{"id":"84efa09d.9749a","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve buka/',payload:'true'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1290.6666259765625,"y":769,"wires":[["a6b21886.2c0ae8"]]},{"id":"29656942.aa8196","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1294.6666259765625,"y":860,"wires":[["32233256.bd93fe","a28c78f7.dd7d6","e55f57b6.4e0588"]]},{"id":"32233256.bd93fe","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 1. 36","pin":"36","set":"","level":"0","freq":"","out":"out","x":1547.6666259765625,"y":891,"wires":},{"id":"e55f57b6.4e0588","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 4. 18","pin":"18","set":"","level":"0","freq":"","out":"out","x":1544.6666259765625,"y":933,"wires":},{"id":"bb9f208d.56c868","type":"delay","z":"500b7da7.1772d4","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1078.6666259765625,"y":1026,"wires":[["e073ebea.ce9b68","2bd0d9af.ed5506","2afd518d.5deec6"]]},{"id":"fcebbfcf.7f1ee","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 2 . 32","pin":"32","set":"","level":"0","freq":"","out":"out","x":1526.6666259765625,"y":1097,"wires":},{"id":"f46d0a82.44bea8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 3. 22","pin":"22","set":"","level":"0","freq":"","out":"out","x":1528.6666259765625,"y":1183,"wires":},{"id":"e073ebea.ce9b68","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve buka/',payload:'true'}; \n } \nreturn null;\n","outputs":1,"noerr":0,"x":1277.6666259765625,"y":1014,"wires":[["79d8c910.9fa02"]]},{"id":"2bd0d9af.ed5506","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1320.6666259765625,"y":1084,"wires":[["7f4f7edd.c8daa","fcebbfcf.7f1ee","f46d0a82.44bea8"]]},{"id":"7f4f7edd.c8daa","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 1. 36","pin":"36","set":"","level":"0","freq":"","out":"out","x":1519.6666259765625,"y":1138,"wires":},{"id":"79d8c910.9fa02","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 4. 18","pin":"18","set":"","level":"0","freq":"","out":"out","x":1513,"y":1021.9999389648438,"wires":},{"id":"89c5e5e7.87cff","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 7 .35","pin":"35","set":"","level":"0","freq":"","out":"out","x":1449.6666259765625,"y":332.58331298828125,"wires":},{"id":"a669cce2.5d89a8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 8. 37","pin":"37","set":"","level":"0","freq":"","out":"out","x":1450.3333129882812,"y":270.99999237060547,"wires":},{"id":"eacbd777.12e028","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 6. 33","pin":"33","set":"","level":"0","freq":"","out":"out","x":1448.9998168945312,"y":372.8333282470703,"wires":},{"id":"93c65eb4.855748","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 5 . 29","pin":"29","set":"","level":"0","freq":"","out":"out","x":1464.4165649414062,"y":400.0833435058594,"wires":},{"id":"74fdbe23.55cb68","type":"ui_audio","z":"500b7da7.1772d4","name":"","group":"7dfeba72.29a71c","voice":"en-US","always":true,"x":961.9999084472656,"y":677.3333435058594,"wires":},{"id":"8a7668a3.45e4b","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'Mengalirkan larutan'}; \n } \nreturn null;\n","outputs":1,"noerr":0,"x":954.6665954589844,"y":545.6666870117188,"wires":[["74fdbe23.55cb68"]]},{"id":"4bcbf5a.9a1300c","type":"ui_audio","z":"500b7da7.1772d4","name":"","group":"7dfeba72.29a71c","voice":"en-US","always":true,"x":1835.6666259765625,"y":1546,"wires":},{"id":"2afd518d.5deec6","type":"delay","z":"500b7da7.1772d4","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1373.6666259765625,"y":1287,"wires":[["3277f613.902dd2","5a25796b.15bf8"]]},{"id":"95561dcf.7ff128","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Pompa Utama. 7","pin":"7","set":"","level":"0","freq":"","out":"out","x":1823.333251953125,"y":1305.3333129882812,"wires":},{"id":"3277f613.902dd2","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'false'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1561.6666259765625,"y":1308,"wires":[["95561dcf.7ff128","3abdfc70.1b3004","3cce0e24.db46aa","ec2e63e2.6c68b8","7470a923.dba0a"]]},{"id":"7470a923.dba0a","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 4. 18","pin":"18","set":"","level":"0","freq":"","out":"out","x":1829.6666259765625,"y":1481,"wires":},{"id":"3cce0e24.db46aa","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 2 . 32","pin":"32","set":"","level":"0","freq":"","out":"out","x":1826.6666259765625,"y":1397,"wires":},{"id":"3abdfc70.1b3004","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 1. 36","pin":"36","set":"","level":"0","freq":"","out":"out","x":1823.6666259765625,"y":1360,"wires":},{"id":"ec2e63e2.6c68b8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 3. 22","pin":"22","set":"","level":"0","freq":"","out":"out","x":1829.6666259765625,"y":1438,"wires":},{"id":"5a25796b.15bf8","type":"function","z":"500b7da7.1772d4","name":"Function","func":"if (msg.payload == 'buka') \n {\n return {topic:'valve tutup/',payload:'Tanki Terisi Penuh'}; \n } \nreturn null;","outputs":1,"noerr":0,"x":1567.6666259765625,"y":1422,"wires":[["4bcbf5a.9a1300c"]]},{"id":"74f1ed36.0bb0b4","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 3. 22","pin":"22","set":"","level":"0","freq":"","out":"out","x":1392.9999389648438,"y":530.3332824707031,"wires":},{"id":"731eee1d.7b08e8","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 4. 18","pin":"18","set":"","level":"0","freq":"","out":"out","x":1385.6666259765625,"y":504.3333435058594,"wires":},{"id":"8154a7fd.641708","type":"rpi-gpio out","z":"500b7da7.1772d4","name":"Valve 4. 18","pin":"18","set":"","level":"0","freq":"","out":"out","x":1554,"y":733.4999389648438,"wires":},{"id":"9a986029.f4a13","type":"ui_group","z":"","name":"Fase II","tab":"14b42a7f.a25596","disp":true,"width":"6","collapse":false},{"id":"7dfeba72.29a71c","type":"ui_group","z":"","name":"Fase I","tab":"14b42a7f.a25596","disp":true,"width":"10","collapse":false},{"id":"14b42a7f.a25596","type":"ui_tab","z":"","name":"Kontrol","icon":"dashboard"}]

I want to do command A after that to command B and so on. I want when command B is done then command A stops to neutral and does not affect command B.

Could you explain this more, I don't understand what you want.

Some things I noticed, since every function is testing if (msg.payload == 'buka') why not put a switch after the button to do that test once.

Next I would change all the function nodes to change nodes to set the topic and payload.

Finally aai would use link nodes - have one set of gpio nodes with a link-in omn each and when you want to connect to it, use a link-out node to connect to it. That will reduce the line clutter

Thanks for your answer . I have a main 4 valve (A, B, C, D). when I press the switch I want the first second A ON, BCD OFF. The next 10 seconds B is ON, ACD is OFF. 10 seconds later C is ON, ABD is OFF. and the last 10 seconds D ON, ABC OFF. and it takes place only on one switch command and the code runs sequentially. I had a problem when I made the code but it didn't work properly, so when the initial code was running it affected the code 10 seconds later he didn't return to the initial state, like when B was ON, ACD was OFF but what happened A was still ON. can you fix my code?

Have you thought about using a Finite State Machine (FSM) ?
It might be a bit of an overkill for what you are trying to do, but would seem to answer your requirement.
I created a write-up about FSMs on 'Share Your Projects' earlier this year.
Here's the link.

There's also a Node-RED node written by @cflurin that implements dynamic FSM.

1 Like

State machine is powerful but if you want to do it in the way that you are in control and to educate yourself, then just take Node-RED basic nodes and you are good to go. Something like this.

[{"id":"fc307d7f.99d3d","type":"change","z":"e1898a32.4e08e8","name":"","rules":[{"t":"set","p":"valves","pt":"flow","to":"[{\"name\":\"Valve A\",\"id\":0,\"state\":false},{\"name\":\"Valve B\",\"id\":1,\"state\":false},{\"name\":\"Valve C\",\"id\":2,\"state\":false},{\"name\":\"Valve D\",\"id\":3,\"state\":false}]","tot":"json"},{"t":"set","p":"phase","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":110,"wires":[[]]},{"id":"1b9a4265.fc637e","type":"inject","z":"e1898a32.4e08e8","name":"init","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":890,"y":110,"wires":[["fc307d7f.99d3d"]]},{"id":"b1b33bd9.b923a8","type":"inject","z":"e1898a32.4e08e8","name":"begin cycle","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":850,"y":240,"wires":[["f32c4242.2865c"]]},{"id":"7f3d96c5.e99238","type":"delay","z":"e1898a32.4e08e8","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1080,"y":200,"wires":[["71d3fe7b.39fb1"]]},{"id":"bb204049.75d95","type":"switch","z":"e1898a32.4e08e8","name":"","property":"phase","propertyType":"flow","rules":[{"t":"lt","v":"valves.length","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":290,"wires":[["dd11b4eb.d4a2f8"],["cfaa90d8.48ecf"]]},{"id":"f32c4242.2865c","type":"change","z":"e1898a32.4e08e8","name":"zero","rules":[{"t":"set","p":"phase","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":290,"wires":[["bb204049.75d95"]]},{"id":"dd11b4eb.d4a2f8","type":"function","z":"e1898a32.4e08e8","name":"on by one","func":"var phase = flow.get(\"phase\") || 0;\nvar valves = flow.get(\"valves\") || [];\nif(valves.length === 0){\n    //valves not initialized;\n    return;\n}\nvar valve;\nfor(var i=0;i<valves.length;i++){\n    valve = valves[i];\n    valve.state = phase === i ? true : false;\n}\nflow.set(\"valves\",valves);\nmsg.payload = valves;\nmsg.phase = phase;\nreturn msg;","outputs":1,"noerr":0,"x":1210,"y":280,"wires":[["2ec0d607.6e784a","7f3d96c5.e99238"]]},{"id":"cfaa90d8.48ecf","type":"function","z":"e1898a32.4e08e8","name":"all off","func":"var valves = flow.get(\"valves\") || [];\nif(valves.lenght === 0){\n    //valves not initialized;\n    return;\n}\nvar valve;\nfor(var i=0;i<valves.length;i++){\n    valve = valves[i];\n    valve.state = false;\n}\nflow.set(\"valves\",valves);\nmsg.payload = valves;\nmsg.phase = flow.get(\"phase\");\nreturn msg;","outputs":1,"noerr":0,"x":1210,"y":330,"wires":[["2ec0d607.6e784a"]]},{"id":"2ec0d607.6e784a","type":"split","z":"e1898a32.4e08e8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1360,"y":300,"wires":[["4668589a.4af678"]]},{"id":"4668589a.4af678","type":"switch","z":"e1898a32.4e08e8","name":"","property":"payload.id","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":1220,"y":440,"wires":[["e36d4b90.57d0a8"],["8db87ef.09bcd8"],["7bb02f42.f8252"],["7c501ec8.cb253"]]},{"id":"71d3fe7b.39fb1","type":"function","z":"e1898a32.4e08e8","name":"increase phase","func":"var phase = flow.get(\"phase\") || 0;\nphase ++;\nflow.set(\"phase\",phase);\nmsg.payload = phase\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":200,"wires":[["bb204049.75d95"]]},{"id":"64bd924a.1d7e9c","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":380,"wires":[]},{"id":"8ae736da.7e35b8","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":420,"wires":[]},{"id":"431e8f74.a4086","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":460,"wires":[]},{"id":"8fc6384f.4ee508","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":500,"wires":[]},{"id":"7348e446.73c2dc","type":"inject","z":"e1898a32.4e08e8","name":"cancel","topic":"cancel","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":450,"wires":[["3081aa07.027c56"]]},{"id":"3081aa07.027c56","type":"change","z":"e1898a32.4e08e8","name":"last step","rules":[{"t":"set","p":"phase","pt":"flow","to":"valves.length","tot":"flow"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":330,"wires":[["cfaa90d8.48ecf","7f3d96c5.e99238"]]},{"id":"e36d4b90.57d0a8","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":380,"wires":[["64bd924a.1d7e9c"]]},{"id":"7c501ec8.cb253","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":500,"wires":[["8fc6384f.4ee508"]]},{"id":"7bb02f42.f8252","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":460,"wires":[["431e8f74.a4086"]]},{"id":"8db87ef.09bcd8","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":420,"wires":[["8ae736da.7e35b8"]]}]

@dynamicdave,
That's really an user case where a state machine can help simplifying code.

Flow:

[{"id":"f2a3fa4b.3e78f8","type":"dsm","z":"66646ca8.e97654","name":"sequence","sm_config":"{\n    \"currentState\": \"idle\",\n    \"states\": {\n        \"idle\": {\n            \"start\": \"A\"\n        },\n        \"A\": {\n            \"A>B\": \"B\"\n        },\n        \"B\": {\n            \"B>C\": \"C\"\n        },\n        \"C\": {\n            \"C>D\": \"D\"\n        },\n        \"D\": {\n            \"stop\": \"idle\"\n        }\n    },\n    \"data\": {\n        \"period\": 10\n    },\n    \"methods\": {\n\t\t\"init\": [\n\t\t    \"sm.delay = function(tran, msg) {\",\n\t\t    \"   timeout.interval = setTimeout(function() {\",\n            \"       resume(tran, msg)\",\n            \"   },sm.data.period*1000);\",\n            \"}\"\n\t\t],\n\t\t\"onBeforeTransition\": [\n\t\t    \"msg.payload = {'A': 'off', 'B': 'off', 'C': 'off', 'D': 'off'};\"\n\t\t],\n        \"start\": [\n            \"msg.payload.A = 'on';\",\n            \"sm.delay('A>B', msg);\"\n        ],\n        \"A>B\": [\n            \"msg.payload.B = 'on';\",\n            \"sm.delay('B>C', msg);\"\n        ],\n        \"B>C\": [\n            \"msg.payload.C = 'on';\",\n            \"sm.delay('C>D', msg);\"\n        ],\n        \"C>D\": [\n            \"msg.payload.D = 'on';\",\n            \"sm.delay('stop', msg);\"\n        ],\n        \"status\": {\n            \"fill\": {\n                \"get\": \"sm.currentState === 'idle' ? 'green' : 'yellow';\"\n            },\n            \"shape\": \"dot\",\n            \"text\": {\n                \"get\": \"sm.currentState;\"\n            }\n        }\n    }\n}\n","x":290,"y":1580,"wires":[["487f8430.22e05c"]]},{"id":"487f8430.22e05c","type":"debug","z":"66646ca8.e97654","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":450,"y":1580,"wires":[]},{"id":"472dc3ea.dc802c","type":"inject","z":"66646ca8.e97654","name":"","topic":"start","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":1580,"wires":[["f2a3fa4b.3e78f8"]]}]

Note: however a state machine is for advanced users, that means basics about node-red, javascript, function, object are required.

Just had a few spare minutes to knock-up a quick (possible) solution using some of the basic nodes.

Please note...
"No FSMs were used or harmed in the making of this flow."

I'm sure there are parts of the flow that can be simplified.
For example, some of the functionality in the function blocks could be combined.

Hope you find this useful.

[{"id":"33da7582.0d26e2","type":"inject","z":"935fc4c0.e49458","name":"","topic":"","payload":"","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":150,"y":80,"wires":[["298df848.2f5568"]]},{"id":"298df848.2f5568","type":"change","z":"935fc4c0.e49458","name":"Set initial conditions","rules":[{"t":"set","p":"status","pt":"flow","to":"stopped","tot":"str"},{"t":"set","p":"counter","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":80,"wires":[[]]},{"id":"b644747a.430e8","type":"inject","z":"935fc4c0.e49458","name":"","topic":"start","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":160,"wires":[["6fe89848.fae838"]]},{"id":"69c6af4c.b03bf","type":"inject","z":"935fc4c0.e49458","name":"Trigger every 3 seconds","topic":"","payload":"1","payloadType":"num","repeat":"3","crontab":"","once":true,"onceDelay":"1","x":210,"y":240,"wires":[["cc76bee1.6f76c"]]},{"id":"cc76bee1.6f76c","type":"function","z":"935fc4c0.e49458","name":"","func":"var status = flow.get(\"status\") || \"stopped\";\n\nif (status == \"run\")\n{\n    var counter = flow.get(\"counter\") || 0;\n    if (counter < 5)\n    {\n        counter = counter + 1;\n        flow.set(\"counter\",counter);\n        return msg;\n    }\n    else\n    {\n        counter = 0;\n        flow.set(\"counter\",counter);\n        flow.set(\"status\",\"stopped\");\n    }\n}\n\n","outputs":1,"noerr":0,"x":410,"y":240,"wires":[["97677c38.0aad5","598b4e34.52e978","ed68e8f7.b2d658","9af74977.f4993"]]},{"id":"97677c38.0aad5","type":"function","z":"935fc4c0.e49458","name":"Decode Valve-A","func":"var counter = flow.get(\"counter\") || 0;\n\nif (counter == 1)\n   {\n    msg.payload = 1;\n    node.status({fill:\"green\",shape:\"dot\",text:\"Valve-A is ON\"});\n   }\n   \nelse\n   {\n    msg.payload = 0;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Valve-A is OFF\"});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":140,"wires":[["4553c239.9c9ff4"]]},{"id":"598b4e34.52e978","type":"function","z":"935fc4c0.e49458","name":"Decode Valve-B","func":"var counter = flow.get(\"counter\") || 0;\n\nif (counter == 2)\n   {\n    msg.payload = 1;\n    node.status({fill:\"green\",shape:\"dot\",text:\"Valve-B is ON\"});\n   }\n   \nelse\n   {\n    msg.payload = 0;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Valve-B is OFF\"});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":200,"wires":[["e6d2c777.306ec"]]},{"id":"ed68e8f7.b2d658","type":"function","z":"935fc4c0.e49458","name":"Decode Valve-C","func":"var counter = flow.get(\"counter\") || 0;\n\nif (counter == 3)\n   {\n    msg.payload = 1;\n    node.status({fill:\"green\",shape:\"dot\",text:\"Valve-C is ON\"});\n   }\n   \nelse\n   {\n    msg.payload = 0;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Valve-C is OFF\"});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":280,"wires":[["bb773c1e.ac3818"]]},{"id":"9af74977.f4993","type":"function","z":"935fc4c0.e49458","name":"Decode Valve-A","func":"var counter = flow.get(\"counter\") || 0;\n\nif (counter == 4)\n   {\n    msg.payload = 1;\n    node.status({fill:\"green\",shape:\"dot\",text:\"Valve-D is ON\"});\n   }\n   \nelse\n   {\n    msg.payload = 0;\n    node.status({fill:\"red\",shape:\"dot\",text:\"Valve-D is OFF\"});\n   }\n\nreturn msg;","outputs":1,"noerr":0,"x":600,"y":340,"wires":[["a1766a9b.789518"]]},{"id":"6fe89848.fae838","type":"change","z":"935fc4c0.e49458","name":"Start the sequence","rules":[{"t":"set","p":"status","pt":"flow","to":"run","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":330,"y":160,"wires":[[]]},{"id":"7db6094d.069648","type":"comment","z":"935fc4c0.e49458","name":"Testing only - change this to 10-seconds ","info":"","x":240,"y":300,"wires":[]},{"id":"4553c239.9c9ff4","type":"debug","z":"935fc4c0.e49458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":140,"wires":[]},{"id":"e6d2c777.306ec","type":"debug","z":"935fc4c0.e49458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":200,"wires":[]},{"id":"bb773c1e.ac3818","type":"debug","z":"935fc4c0.e49458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":280,"wires":[]},{"id":"a1766a9b.789518","type":"debug","z":"935fc4c0.e49458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":340,"wires":[]},{"id":"61d3256a.2583dc","type":"comment","z":"935fc4c0.e49458","name":"Color scheme: Red = OFF, Green = ON ","info":"","x":670,"y":400,"wires":[]}]

thanks dave :slight_smile:

thanks . it works

thanks nipi

@rizalderck, @dynamicdave,

I didn't try but it seems to me that when you start the sequence it may take up to 10 (3) seconds before valve A is turned on.

I think you are correct about the time before valve-A is turned on.
I could probably adjust the flow to accommodate it - but thought it probably wasn't that critical.

Happy Christmas from Dynamic Dave in the UK.

Thanks, the same to you and the community.

3 Likes

yess ,me too

Hello Rizal.

I have a set of node modules that can do that automatically.
Please check


The sequencer module was designed to poll devices sequentially.
Use the automatically start tab in the config settings.
If you send a "true/False" or "1/0" message to the input pin it will pause the sequence for a defined amount of time.
You can add as many output pins as you want, the program module works in a round robin type way.

Hi,

Need your help here, working on a similar problem need the FSM to get valve open time and interval between valves opening from flow or global context variables. Kindly assist,

[{"id":"fc307d7f.99d3d","type":"change","z":"e1898a32.4e08e8","name":"","rules":[{"t":"set","p":"valves","pt":"flow","to":"[{"name":"Valve A","id":0,"state":false},{"name":"Valve B","id":1,"state":false},{"name":"Valve C","id":2,"state":false},{"name":"Valve D","id":3,"state":false}]","tot":"json"},{"t":"set","p":"phase","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1060,"y":110,"wires":[]},{"id":"1b9a4265.fc637e","type":"inject","z":"e1898a32.4e08e8","name":"init","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":890,"y":110,"wires":[["fc307d7f.99d3d"]]},{"id":"b1b33bd9.b923a8","type":"inject","z":"e1898a32.4e08e8","name":"begin cycle","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":850,"y":240,"wires":[["f32c4242.2865c"]]},{"id":"7f3d96c5.e99238","type":"delay","z":"e1898a32.4e08e8","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1080,"y":200,"wires":[["71d3fe7b.39fb1"]]},{"id":"bb204049.75d95","type":"switch","z":"e1898a32.4e08e8","name":"","property":"phase","propertyType":"flow","rules":[{"t":"lt","v":"valves.length","vt":"flow"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1070,"y":290,"wires":[["dd11b4eb.d4a2f8"],["cfaa90d8.48ecf"]]},{"id":"f32c4242.2865c","type":"change","z":"e1898a32.4e08e8","name":"zero","rules":[{"t":"set","p":"phase","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":290,"wires":[["bb204049.75d95"]]},{"id":"dd11b4eb.d4a2f8","type":"function","z":"e1898a32.4e08e8","name":"on by one","func":"var phase = flow.get("phase") || 0;\nvar valves = flow.get("valves") || ;\nif(valves.length === 0){\n //valves not initialized;\n return;\n}\nvar valve;\nfor(var i=0;i<valves.length;i++){\n valve = valves[i];\n valve.state = phase === i ? true : false;\n}\nflow.set("valves",valves);\nmsg.payload = valves;\nmsg.phase = phase;\nreturn msg;","outputs":1,"noerr":0,"x":1210,"y":280,"wires":[["2ec0d607.6e784a","7f3d96c5.e99238"]]},{"id":"cfaa90d8.48ecf","type":"function","z":"e1898a32.4e08e8","name":"all off","func":"var valves = flow.get("valves") || ;\nif(valves.lenght === 0){\n //valves not initialized;\n return;\n}\nvar valve;\nfor(var i=0;i<valves.length;i++){\n valve = valves[i];\n valve.state = false;\n}\nflow.set("valves",valves);\nmsg.payload = valves;\nmsg.phase = flow.get("phase");\nreturn msg;","outputs":1,"noerr":0,"x":1210,"y":330,"wires":[["2ec0d607.6e784a"]]},{"id":"2ec0d607.6e784a","type":"split","z":"e1898a32.4e08e8","name":"","splt":"\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1360,"y":300,"wires":[["4668589a.4af678"]]},{"id":"4668589a.4af678","type":"switch","z":"e1898a32.4e08e8","name":"","property":"payload.id","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"}],"checkall":"true","repair":false,"outputs":4,"x":1220,"y":440,"wires":[["e36d4b90.57d0a8"],["8db87ef.09bcd8"],["7bb02f42.f8252"],["7c501ec8.cb253"]]},{"id":"71d3fe7b.39fb1","type":"function","z":"e1898a32.4e08e8","name":"increase phase","func":"var phase = flow.get("phase") || 0;\nphase ++;\nflow.set("phase",phase);\nmsg.payload = phase\nreturn msg;","outputs":1,"noerr":0,"x":1240,"y":200,"wires":[["bb204049.75d95"]]},{"id":"64bd924a.1d7e9c","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":380,"wires":},{"id":"8ae736da.7e35b8","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":420,"wires":},{"id":"431e8f74.a4086","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":460,"wires":},{"id":"8fc6384f.4ee508","type":"debug","z":"e1898a32.4e08e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1510,"y":500,"wires":},{"id":"7348e446.73c2dc","type":"inject","z":"e1898a32.4e08e8","name":"cancel","topic":"cancel","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":830,"y":450,"wires":[["3081aa07.027c56"]]},{"id":"3081aa07.027c56","type":"change","z":"e1898a32.4e08e8","name":"last step","rules":[{"t":"set","p":"phase","pt":"flow","to":"valves.length","tot":"flow"},{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":330,"wires":[["cfaa90d8.48ecf","7f3d96c5.e99238"]]},{"id":"e36d4b90.57d0a8","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":380,"wires":[["64bd924a.1d7e9c"]]},{"id":"7c501ec8.cb253","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":500,"wires":[["8fc6384f.4ee508"]]},{"id":"7bb02f42.f8252","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":460,"wires":[["431e8f74.a4086"]]},{"id":"8db87ef.09bcd8","type":"rbe","z":"e1898a32.4e08e8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload.state","x":1370,"y":420,"wires":[["8ae736da.7e35b8"]]}]