Toggle flow with different timers

I had the need to create an asyncronous toggle function with different timers for ON and OFF (true and false)

I did not found any node capable of this, so I create mine...

You have a dashboard also where settings the parameters and see the status of the toggle and the remaining times.

[{"id":"7805ff82.17aba","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"7f1d04e2.d70bcc","type":"ui_numeric","z":"7805ff82.17aba","name":"","label":"Scan Time","tooltip":"","group":"e27a69a4.4de348","order":0,"width":"0","height":"0","passthru":true,"topic":"scanTime","format":"{{value}}","min":0,"max":"900","step":1,"x":490.69889068603516,"y":47.664772033691406,"wires":[["7978e7dc.8adcc8"]]},{"id":"60f28a4a.6f6d34","type":"ui_numeric","z":"7805ff82.17aba","name":"","label":"Idle Time","tooltip":"","group":"e27a69a4.4de348","order":0,"width":"0","height":"0","passthru":true,"topic":"idleTime","format":"{{value}}","min":0,"max":"900","step":1,"x":484.0142593383789,"y":107.00567436218262,"wires":[["7978e7dc.8adcc8"]]},{"id":"e0d01da6.303bc","type":"ui_text","z":"7805ff82.17aba","group":"e27a69a4.4de348","order":2,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"row-spread","x":483.6959915161133,"y":167.67609786987305,"wires":[]},{"id":"2c36e3ef.a2cc1c","type":"inject","z":"7805ff82.17aba","name":"","topic":"","payload":"Clock","payloadType":"str","repeat":"1","crontab":"","once":true,"onceDelay":"1","x":152.69032287597656,"y":72.97443389892578,"wires":[["ca10048b.d7c628"]]},{"id":"ca10048b.d7c628","type":"function","z":"7805ff82.17aba","name":"","func":"var msg = {};\nvar scanTime = flow.get('scanTime')||60;\nvar idleTime = flow.get('idleTime')||60;\nvar scanStatus = flow.get('scanStatus')||false;\nvar scanClock = flow.get('scanClock')||0;\n\nscanClock += 1;\nflow.set('scanClock',scanClock);\nif (scanStatus === false) {\n    var triggerClock = idleTime;\n} else {\n    var triggerClock = scanTime;\n} \n\nif (scanClock === triggerClock) {\n   flow.set('scanStatus',!scanStatus);\n   flow.set('scanClock',0);\n}\nmsg.payload = 'SCAN: '+scanStatus.toString()+' TIME: '+scanClock+'/'+triggerClock;\nreturn msg;","outputs":1,"noerr":0,"x":297.6903877258301,"y":72.7982816696167,"wires":[["e0d01da6.303bc"]]},{"id":"7978e7dc.8adcc8","type":"function","z":"7805ff82.17aba","name":"","func":"flow.set(msg.topic, msg.payload)\nreturn msg;","outputs":1,"noerr":0,"x":651.014217376709,"y":78.00568389892578,"wires":[[]]},{"id":"e27a69a4.4de348","type":"ui_group","z":"","name":"dbg","tab":"ec4c7e0f.6e5ef","order":1,"disp":true,"width":"6","collapse":true},{"id":"ec4c7e0f.6e5ef","type":"ui_tab","z":"","name":"DBG","icon":"fa-quora","order":5}]

Flow Info

Created 5 years, 9 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • function (x2)
  • inject (x1)
Other
  • tab (x1)
  • ui_group (x1)
  • ui_numeric (x2)
  • ui_tab (x1)
  • ui_text (x1)

Tags

  • toggle
  • async
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option