# Delay node with variable delay

**URL:** <https://discourse.nodered.org/t/delay-node-with-variable-delay/18486>\
**Category:** General\
**Created:** [27 November 2019 03:06 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486 "2019-11-27T03:06:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)\
**Post date:** [27 November 2019 03:06 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/1 "2019-11-27T03:06:02Z")

</div>

I am attempting to pass different delay amounts to a delay node through msg.delay. Can anyone tell me where I'm going wrong?

```auto
[{"id":"4b2f9055.5f598","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"e6d312a4.bf93","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":80,"wires":[]},{"id":"5e64aaf4.dc9ef4","type":"change","z":"4b2f9055.5f598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":240,"wires":[["6517cc4d.61a88c","d932d3d4.be54f8"]]},{"id":"901243ac.17a7d8","type":"trigger","z":"4b2f9055.5f598","op1":"0","op2":"0","op1type":"num","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":660,"y":180,"wires":[["e6d312a4.bf93"]]},{"id":"6517cc4d.61a88c","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"delay","targetType":"msg","x":490,"y":300,"wires":[]},{"id":"fa236cb5.f3d9a8","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":60,"wires":[["e6d312a4.bf93","d932d3d4.be54f8"]]},{"id":"23f29ff.5bdca6","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":240,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"1ed1e079.f559e","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2.5","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":300,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d3895c08.15a62","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"3","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":70,"y":360,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d932d3d4.be54f8","type":"delay","z":"4b2f9055.5f598","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":180,"wires":[["901243ac.17a7d8"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [27 November 2019 20:08 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/3 "2019-11-27T20:08:19Z")

</div>

Too easy.

Don't feel bad, but I have fallen for this trap many times myself.

The nodes which set time time:  
The delay is in **milliseconds** not seconds.

So if you make the nodes:  
2000  
2500  
3000  
it will work.

---

<div class="post-metadata">

**Author:** ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)\
**Post date:** [27 November 2019 21:42 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/4 "2019-11-27T21:42:57Z")

</div>

I gave that a shot and am running into the same issue. If I inject the "ON Payload" then the delay works with one second and then the trigger node send a payload of "0". When I inject the 2000 payload i see the "2000" msg.payload, however when I attempt to send the "ON Payload" again the trigger node is never triggered.

```auto
[{"id":"4b2f9055.5f598","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"e6d312a4.bf93","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":80,"wires":[]},{"id":"5e64aaf4.dc9ef4","type":"change","z":"4b2f9055.5f598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":240,"wires":[["6517cc4d.61a88c","d932d3d4.be54f8"]]},{"id":"901243ac.17a7d8","type":"trigger","z":"4b2f9055.5f598","op1":"0","op2":"0","op1type":"num","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":660,"y":180,"wires":[["e6d312a4.bf93"]]},{"id":"6517cc4d.61a88c","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"delay","targetType":"msg","x":490,"y":300,"wires":[]},{"id":"fa236cb5.f3d9a8","type":"inject","z":"4b2f9055.5f598","name":"ON Payload","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":60,"wires":[["e6d312a4.bf93","d932d3d4.be54f8"]]},{"id":"23f29ff.5bdca6","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":240,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"1ed1e079.f559e","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2500","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":300,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d3895c08.15a62","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"3000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":360,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d932d3d4.be54f8","type":"delay","z":"4b2f9055.5f598","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":180,"wires":[["901243ac.17a7d8"]]}]

```

---

<div class="post-metadata">

**Author:** ![mwawrzyniec](https://avatars.discourse-cdn.com/v4/letter/m/d26b3c/32.png) [@mwawrzyniec](https://discourse.nodered.org/u/mwawrzyniec)\
**Post date:** [27 November 2019 22:02 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/5 "2019-11-27T22:02:22Z")

</div>

OK so I added a change node to inject msg.reset to the trigger node. Now the trigger node will work more than once, however the timer is always 1s regardless of the msg.delay that is sent.

```auto
[{"id":"4b2f9055.5f598","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"e6d312a4.bf93","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":870,"y":80,"wires":[]},{"id":"5e64aaf4.dc9ef4","type":"change","z":"4b2f9055.5f598","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":240,"wires":[["6517cc4d.61a88c","d932d3d4.be54f8"]]},{"id":"901243ac.17a7d8","type":"trigger","z":"4b2f9055.5f598","op1":"0","op2":"0","op1type":"num","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":660,"y":180,"wires":[["e6d312a4.bf93"]]},{"id":"6517cc4d.61a88c","type":"debug","z":"4b2f9055.5f598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"delay","targetType":"msg","x":490,"y":300,"wires":[]},{"id":"fa236cb5.f3d9a8","type":"inject","z":"4b2f9055.5f598","name":"ON Payload","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":60,"wires":[["e6d312a4.bf93","d932d3d4.be54f8","ee2cf192.b5f2d"]]},{"id":"23f29ff.5bdca6","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":240,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"1ed1e079.f559e","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"2500","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":300,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d3895c08.15a62","type":"inject","z":"4b2f9055.5f598","name":"","topic":"","payload":"3000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":360,"wires":[["5e64aaf4.dc9ef4"]]},{"id":"d932d3d4.be54f8","type":"delay","z":"4b2f9055.5f598","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":180,"wires":[["901243ac.17a7d8"]]},{"id":"ee2cf192.b5f2d","type":"change","z":"4b2f9055.5f598","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":100,"wires":[["901243ac.17a7d8"]]}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 November 2019 03:04 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/6 "2019-11-28T03:04:30Z")

</div>

Hi.

I **am** sorry for my quick reply. I had just got up and was about to go out.

I'll look at the latest flow now.

Quick question:

Why do you think you need the trigger node?  
And why do you call it "trigger and **block**"?  
Block what?

If I am understanding it right, you have a "message" you usually send through with a 1 second delay between messages?

Playing with it as you posted it, I am really missing something in what you are wanting to do.

Looking at the middle one, it isn't much better in expanding on the `delay` and `trigger` nodes and what they do.

Actually now looking I am not even getting what the first one is supposed to do.

But hang in there.

I'll be back shortly.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 November 2019 03:39 UTC](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486/7 "2019-11-28T03:39:23Z")

</div>

Ok, I made a big mistake with my reply.

**I** made it. Not you.

The `msg.delay` needs to be sent _with_ the message.  
So: If you send `msg.delay 3000` then send `msg.payload Hello` through the `delay` node as is, it will default to 1000 ms.

What you need to do is send a message with both the payload and the delay parts.

This is _tricky_ because you need to _store_ the delay value for subsequent messages.

I don't know how much you know about Node-Red.  
(No problem. We all start at the bottom and work up.)  
This is done with something called `context`.

Here is a flow which does what I think you want it to do.

```auto
[{"id":"ac4ad09f.1b53d","type":"inject","z":"fe07958a.5600b","name":"ON Payload","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":190,"y":470,"wires":[["6fa8b4cb.1defbc","acdfba77.5345c"]]},{"id":"6fa8b4cb.1defbc","type":"debug","z":"fe07958a.5600b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":640,"y":470,"wires":[]},{"id":"546a50ae.8b9668","type":"delay","z":"fe07958a.5600b","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":260,"y":580,"wires":[["a7780216.3f641"]]},{"id":"a7780216.3f641","type":"change","z":"fe07958a.5600b","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":450,"y":580,"wires":[["6fa8b4cb.1defbc"]]},{"id":"bd92bef8.e64028","type":"inject","z":"fe07958a.5600b","name":"","topic":"","payload":"2000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":670,"wires":[["115d620f.102986"]]},{"id":"115d620f.102986","type":"change","z":"fe07958a.5600b","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":670,"wires":[["7d436b89.2de1ec","acdfba77.5345c"]]},{"id":"b73d1884.72168","type":"inject","z":"fe07958a.5600b","name":"","topic":"","payload":"2500","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":730,"wires":[["115d620f.102986"]]},{"id":"a26c6ad3.4e5bf8","type":"inject","z":"fe07958a.5600b","name":"","topic":"","payload":"3000","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":790,"wires":[["115d620f.102986"]]},{"id":"acdfba77.5345c","type":"function","z":"fe07958a.5600b","name":"Store the delay value.","func":"if (msg.delay > 0)\n{\n // Delete the line below (or put // at the start)\n node.warn(\"Delay received\"); // this is just to show you\n context.set('DELAY',msg.delay);\n return;\n}\n // Delete the line below (or put // at the start)\n node.warn(\"Payload received\"); // this is just to show you\n let delay = context.get('DELAY') || 1000;\n msg = {payload:msg.payload,delay:delay};\n\nreturn msg;","outputs":1,"noerr":0,"x":220,"y":540,"wires":[["546a50ae.8b9668"]]},{"id":"7d436b89.2de1ec","type":"debug","z":"fe07958a.5600b","name":"Delay","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":480,"y":670,"wires":[]}]

```

Look at the `function` node (store the delay value) and you will see that it _stores_ the value of the delay when a message with a delay is received.

There are also a couple of lines you may want to delete.  
Lines 4 and 9.  
They are there only to show/help you see what is going on.  
Otherwise the mess up the debug list on the right of the screen.

Then when it receives another message, it gets that stored value and creates a message which also has the `msg.delay` part in it and sends that to the `delay` node.

I'm not sure about the `trigger` node. But looking at what it was doing, I think a `change` node will suffice.  
After the nominated time for the delay the message is sent out of the node and goes into the `change` node which then changes it to the _new_ value.

Hope that works/helps.
