User Variable Timer

I am looking for some help with a user variable timer. (Application): I am building a drink maker using dosing pumps. The time the pump need to run depends on the drink that is being made and the calibration value of the pump (flow rate). The time the pump needs to run can be calculated (That part I can do). What I can't figure out is a timer that can change its timeout value based on a flow.get (value). I feel like this should be a common thing. I have tried understanding the setTimeout syntax, however I am not a code writer, and have found no example that dumbs it down for me.

A trigger node, using msg.delay to determine the 'off' time.

[{"id":"dd9edfd759fabc58","type":"trigger","z":"f8cf7ea5344d6287","name":"Delay","op1":"on","op2":"off","op1type":"str","op2type":"str","duration":"250","extend":false,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":460,"y":1655,"wires":[["7c8e892a468d461f"]]},{"id":"7c8e892a468d461f","type":"debug","z":"f8cf7ea5344d6287","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":625,"y":1655,"wires":[]},{"id":"bb3a7d0fbaa42ea1","type":"inject","z":"f8cf7ea5344d6287","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":115,"y":1655,"wires":[["d7be39d7c9d5d065"]]},{"id":"d7be39d7c9d5d065","type":"change","z":"f8cf7ea5344d6287","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"test","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":1655,"wires":[["dd9edfd759fabc58"]]},{"id":"b23c4a79f0cbe67e","type":"inject","z":"f8cf7ea5344d6287","name":"1 minute","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"60000","payloadType":"num","x":120,"y":1530,"wires":[["f6563354b274ffda"]]},{"id":"f6563354b274ffda","type":"change","z":"f8cf7ea5344d6287","name":"Save global context","rules":[{"t":"set","p":"test","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":1530,"wires":[[]]},{"id":"3c5f9811dee39200","type":"inject","z":"f8cf7ea5344d6287","name":"5 minutes","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"300000","payloadType":"num","x":120,"y":1570,"wires":[["f6563354b274ffda"]]},{"id":"9b0446109897d3b2","type":"comment","z":"f8cf7ea5344d6287","name":"Save a value to context first!","info":"","x":180,"y":1490,"wires":[]},{"id":"58710b65558ea2fa","type":"comment","z":"f8cf7ea5344d6287","name":"...then run the flow","info":"","x":145,"y":1615,"wires":[]}]
1 Like

Here is an example of setting setTimeout() in a function node.
hope it helps

[{"id":"3fd3fa3f.9e49b6","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":4560,"wires":[["cd62cb00.fe0e28"]]},{"id":"cd62cb00.fe0e28","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"timeout","pt":"flow","to":"$ceil($random()*10)*1000","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":350,"y":4560,"wires":[["37ca434a.148d5c"]]},{"id":"37ca434a.148d5c","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"$flowContext(\"timeout\")","statusType":"jsonata","x":580,"y":4560,"wires":[]},{"id":"471caf5c.2b2b68","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":4620,"wires":[["30203cba.52c8f4"]]},{"id":"30203cba.52c8f4","type":"function","z":"b779de97.b1b46","name":"","func":"let time = flow.get(\"timeout\") || 0\n setTimeout(function(){\n    node.send({payload:\"whatever you wish\"});\n }, time);\nreturn null;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":4620,"wires":[["bc1b288e.4f06"]]},{"id":"bc1b288e.4f06","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":4620,"wires":[]}]

pressing first inject will set flowcontext to a random time in milliseconds.
press the second inject to see the resulting timeout.

I use one called contrib-mytimeout

+1 for the Trigger node with msg.delay

Meanwhile, can you elaborate more on what dosing pumps you are using for this drink maker, and how you plan to set this up? It sounds like a cool idea.

I could not get that to work. I'm sure I fat fingered something. I did get a setTimeout example to work. Thank you for your help.

I did get this to work with some adjustment to my calculations. The only problem I have is understanding why it works so I can use this later in other flows. I have a bit of homework to do. Thank you for your help.

Did you import & try the example flow?
What exactly didn't work, what happened when you tried it?

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