Two msg in delay with function node

Hi guys, I'm trying to send two messages from one funcion it's kind off something like this....

msg.payload = "hi";
msg.delay = 5000;
msg.payload = "goodbye";
msg.delay = 10000;
return msg;

basically I have a msg to send after the first one, I was looking for some information because I saw it before the msg.delay function. Some help please I din't found anything else

There are two ways you can do this.

1 - the function node does it all
2 - you add a delay node after the function node.

This is a flow that shows both ways.

[{"id":"bbda58b0.80b1d8","type":"function","z":"2a10ff07.234f98","name":"","func":"msg.payload = \"First message\";\nnode.send(msg);\nmsg.payload = \"Last message\"\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1860,"wires":[["46e569fc.bd4918"]]},{"id":"e97297e2.98a31","type":"inject","z":"2a10ff07.234f98","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":1810,"wires":[["db5ebf42.239088","bbda58b0.80b1d8"]]},{"id":"46e569fc.bd4918","type":"delay","z":"2a10ff07.234f98","name":"Delay","pauseType":"rate","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"5","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":480,"y":1860,"wires":[["34c57a8d.7684d6"]]},{"id":"db5ebf42.239088","type":"function","z":"2a10ff07.234f98","name":"","func":"//\n//  Note the position of the first and last messages.\n//\nsetTimeout(function(){\n    msg.payload = \"last message\";\n    node.send(msg);\n}, 5000);       //  This is how many milliseconds to wait.\n\nmsg.payload = \"first message\";\nreturn msg;","outputs":1,"noerr":0,"x":330,"y":1810,"wires":[["7c1fbeb3.10b04"]]},{"id":"34c57a8d.7684d6","type":"debug","z":"2a10ff07.234f98","name":"Second way","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":670,"y":1860,"wires":[]},{"id":"7c1fbeb3.10b04","type":"debug","z":"2a10ff07.234f98","name":"First way","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":660,"y":1810,"wires":[]}]

The only problem is that there is an initial 5 second delay for the first message too.

2 Likes

Aside from the delay issue (for which you should use something like the Delay node), your example won't work in a Function node because the first payload gets overwritten. I suggest you read up on using Functions ... https://nodered.org/docs/user-guide/writing-functions.

An uncomplicated flow would be: Inject ->Function ->Delay ->Function -> Delay -> Debug

Alternatively,have a read of Can I have a delay / sleep inside Function node

Sorry, me or @lalo?

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