Creating a function with a loop with input from Dashboard

Hello everyone,

at the moment I'm trying to build a loop function which will allow me to do the following:

  • Input of 2 variables via dashboard (ON and Off time in minutes)
  • a permanent loop which first outputs a payload ON for the duration of the time of input ON, and an OFF payload for the length of the OFF time.

Currently, I have already searched several nodes but unfortunately found nothing suitable. Therefore I thought that it should be possible to write this simple function myself. Unfortunately I only have experience with a little c++ and have absolutely no idea about javascript.

Does anyone have a good hint, an idea or a node?

Kind regards

Hello Driss, welcome to the forum.

What do you mean output a payload ON for the duration?

  • Send a message ON, wait for n minutes then send OFF?
  • Send a message ON, then send it again, again, again... (How often, every millisecond?)

The first is very simply achieved with a Trigger node.

Hello jbudd,

my plan ist to : Send a message ON, wait for x minutes then send OFF and wait for y minutes and then restart.

Here is a simple flow using two trigger nodes, one for x minutes delay, one for y minutes.

[{"id":"f77cbf8b5446a20e","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fb5d4a613b960f55","type":"group","z":"f77cbf8b5446a20e","name":"Just Once","style":{"stroke":"#999999","label":true,"color":"#a4a4a4"},"nodes":["b712ab380b1e1ec3","c8fb78c55d77c905","c2103d113a91feae","4cec78c51d2d16f9","b137edd48063a624"],"x":74,"y":79,"w":812,"h":122},{"id":"0150e151a1d46934","type":"group","z":"f77cbf8b5446a20e","name":"Repeat after a suitable delay","style":{"label":true,"stroke":"#a4a4a4","label-position":"sw","color":"#a4a4a4"},"nodes":["3a05f55eec451eec","82c8e8b68376d985"],"x":234,"y":239,"w":472,"h":90},{"id":"b712ab380b1e1ec3","type":"inject","z":"f77cbf8b5446a20e","g":"fb5d4a613b960f55","name":"Input","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":160,"wires":[["b137edd48063a624"]]},{"id":"c8fb78c55d77c905","type":"trigger","z":"f77cbf8b5446a20e","g":"fb5d4a613b960f55","name":"ON, wait, OFF","op1":"ON","op2":"OFF","op1type":"str","op2type":"str","duration":"250","extend":false,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":580,"y":160,"wires":[["4cec78c51d2d16f9","82c8e8b68376d985"]]},{"id":"c2103d113a91feae","type":"comment","z":"f77cbf8b5446a20e","g":"fb5d4a613b960f55","name":"msg.delay specifies time to OFF in milliseconds","info":"","x":460,"y":120,"wires":[]},{"id":"4cec78c51d2d16f9","type":"debug","z":"f77cbf8b5446a20e","g":"fb5d4a613b960f55","name":"Output","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":160,"wires":[]},{"id":"3a05f55eec451eec","type":"trigger","z":"f77cbf8b5446a20e","g":"0150e151a1d46934","name":"Send nothing, 5min, something","op1":"","op2":"0","op1type":"nul","op2type":"str","duration":"5","extend":false,"overrideDelay":false,"units":"min","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":550,"y":280,"wires":[["b137edd48063a624"]]},{"id":"b137edd48063a624","type":"change","z":"f77cbf8b5446a20e","g":"fb5d4a613b960f55","name":"msg.delay = 2 min","rules":[{"t":"set","p":"delay","pt":"msg","to":"120000","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":160,"wires":[["c8fb78c55d77c905"]]},{"id":"82c8e8b68376d985","type":"switch","z":"f77cbf8b5446a20e","g":"0150e151a1d46934","name":"== OFF?","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":320,"y":280,"wires":[["3a05f55eec451eec"]]}]

For simplicity it uses an inject node rather than dashboard input to start, you would have to work out how to get from the dashboard input to the appropriate msg.delay.

I put the restart loop bit in a seperate group on screen. I don't really think a loop like this is a good idea. Instead you could use an inject node set to repeat every y minutes.

1 Like

Can't test at the moment, but I think you can do it with two trigger nodes in series, the first one set just to repeat the On message at the required frequency and the second to pass through the On and then send Off after the timeout.

[{"id":"cec7984dfb188cd3","type":"inject","z":"2fbaad6f2b46c829","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":1880,"wires":[["023c418c79dcb64c"]]},{"id":"023c418c79dcb64c","type":"trigger","z":"2fbaad6f2b46c829","name":"On every ten seconds","op1":"On","op2":"0","op1type":"str","op2type":"str","duration":"-10","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":280,"y":1880,"wires":[["1525569001487fd6"]]},{"id":"1525569001487fd6","type":"trigger","z":"2fbaad6f2b46c829","name":"Off after three seconds","op1":"","op2":"Off","op1type":"pay","op2type":"str","duration":"3","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":500,"y":1880,"wires":[["32ccd16b94ea8b51"]]},{"id":"32ccd16b94ea8b51","type":"debug","z":"2fbaad6f2b46c829","name":"debug 64","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":680,"y":1880,"wires":[]}]
1 Like

Yes, when you first suggested that approach I made a flow to test it. I knew about the "Resend every n seconds" option but have never used it.

It certainly looks less "wrong" in the editor than mine with a visible feedback loop but it's still an infinite loop and should be avoided if possible.
Not much different from an inject node firing repeatedly I guess.
You don't need my switch node though :heavy_check_mark:
Either way, if the process is initiated more often than the delay period (OP's y > x) the queue can build and build until it runs out of memory.

I don't think there is any looping involved, as you say it is like a repeating Inject, but the period can be changed by sending it the delay time.

I don't think that is correct. The second Trigger node ignores inputs that arrive before it has sent its Off message.

Hey Collin and jbudd,

thank you very much for your help.
first I have tried to modify the solution from jbudd but had problems with the delay input and a weird behavior.
Now I've modified the Idea from Colin....but somehow the the delay is not really changing.

Can anyone see/find the mistake ?

[{"id":"5a25675a96444b82","type":"tab","label":"Fog-control ","disabled":false,"info":""},{"id":"22c71edbf6b6d6f8","type":"inject","z":"5a25675a96444b82","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":290,"y":200,"wires":[["4d29f3423e490f63"]]},{"id":"04eb8d8991a65f7f","type":"trigger","z":"5a25675a96444b82","name":"Off Loop","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"10","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":840,"y":200,"wires":[["076c3fccc0a2a353"]]},{"id":"076c3fccc0a2a353","type":"debug","z":"5a25675a96444b82","name":"debug 64","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1080,"y":200,"wires":[]},{"id":"cc6be63065ccbb8e","type":"ui_text_input","z":"5a25675a96444b82","name":"","label":"On Time [s]","tooltip":"","group":"5b1b349e.248d7c","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"delay1","sendOnBlur":false,"className":"","topicType":"msg","x":170,"y":60,"wires":[["39a7d06d88ef9093"]]},{"id":"c6f65e99bc950b9a","type":"debug","z":"5a25675a96444b82","name":"debug 65","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":640,"y":60,"wires":[]},{"id":"39a7d06d88ef9093","type":"change","z":"5a25675a96444b82","name":"","rules":[{"t":"set","p":"delay1","pt":"flow","to":"payload*1000","tot":"jsonata"},{"t":"set","p":"delay1","pt":"msg","to":"delay ","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":60,"wires":[["c6f65e99bc950b9a","4d29f3423e490f63"]]},{"id":"4d29f3423e490f63","type":"trigger","z":"5a25675a96444b82","name":"ON Loop","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-5","extend":false,"overrideDelay":true,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":600,"y":200,"wires":[["04eb8d8991a65f7f"]]},{"id":"eb62d275f0ef5870","type":"ui_text_input","z":"5a25675a96444b82","name":"","label":"Off Time [s]","tooltip":"","group":"5b1b349e.248d7c","order":2,"width":0,"height":0,"passthru":true,"mode":"number","delay":300,"topic":"delay2","sendOnBlur":false,"className":"","topicType":"msg","x":170,"y":320,"wires":[["0385d318d1537aa9"]]},{"id":"0385d318d1537aa9","type":"change","z":"5a25675a96444b82","name":"","rules":[{"t":"set","p":"delay2","pt":"flow","to":"payload*1000","tot":"jsonata"},{"t":"set","p":"delay2","pt":"msg","to":"delay ","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":320,"wires":[["04eb8d8991a65f7f"]]},{"id":"5b1b349e.248d7c","type":"ui_group","name":"Interval Fogger","tab":"5398f064f922494a","order":3,"disp":true,"width":"6","collapse":false,"className":""},{"id":"5398f064f922494a","type":"ui_tab","name":"Control","icon":"dashboard","disabled":false,"hidden":false}]

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