# User Variable Timer

**URL:** <https://discourse.nodered.org/t/user-variable-timer/51806>\
**Category:** General\
**Created:** [3 October 2021 18:34 UTC](https://discourse.nodered.org/t/user-variable-timer/51806 "2021-10-03T18:34:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudomac](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@sudomac](https://discourse.nodered.org/u/sudomac)\
**Post date:** [3 October 2021 18:34 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/1 "2021-10-03T18:34:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [3 October 2021 19:06 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/2 "2021-10-03T19:06:42Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [3 October 2021 19:25 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/3 "2021-10-03T19:25:15Z")

</div>

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

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [3 October 2021 21:20 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/4 "2021-10-03T21:20:56Z")

</div>

I use one called contrib-mytimeout

---

<div class="post-metadata">

**Author:** ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)\
**Post date:** [4 October 2021 01:53 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/5 "2021-10-04T01:53:47Z")

</div>

+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.

---

<div class="post-metadata">

**Author:** ![sudomac](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@sudomac](https://discourse.nodered.org/u/sudomac)\
**Post date:** [5 October 2021 00:13 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/6 "2021-10-05T00:13:51Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sudomac](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@sudomac](https://discourse.nodered.org/u/sudomac)\
**Post date:** [5 October 2021 00:17 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/7 "2021-10-05T00:17:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [5 October 2021 08:12 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/8 "2021-10-05T08:12:19Z")

</div>

> [@sudomac](#):
>
> I could not get that to work

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 December 2021 08:12 UTC](https://discourse.nodered.org/t/user-variable-timer/51806/9 "2021-12-04T08:12:34Z")

</div>

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