# 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:** 1\
**Showing post:** 7

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

---

_[View the full topic](https://discourse.nodered.org/t/delay-node-with-variable-delay/18486)._
