Good day,
I expect that i made a realy simple mistake.
The idea is that when a message arrive the output count down.
var i = 10;
while (i >= 5) {
msg.payload=i;
node.send(msg);
i--;
}
Expected output
10
9
8
7
6
5
However the number of outputs is correct. but the payload is wrong.
10
5
5
5
5
5
What do i do wrong?
JGKK
6 April 2020 19:16
2
Try a for function.
for(i=10;i>4;i--){
node.send({payload:i});
}
node.done();
return;
Johannes
JGKK
6 April 2020 19:23
3
actually i don’t see anything wrong with the while loop either as it works for me:
let i = 10;
while(i >= 5){
msg.payload = i;
node.send(msg);
i--;
}
node.done();
return;
My guess is the op is using node-red pre version 1
Message is getting overwritten in the loop before debug output.
In later version, message is cloned by default to handle the new asynchronous nature of version 1
3 Likes
I think you are missing the whole context
part.
The variable i
is not honoured between messages.
So the code would have to look (something) like:
if (context.get("counter) > 0
{
let i = context.get("counter");
} else
{
context.set("counter",10);
}
while (i > 5)
{
msg.payload = 1;
i = i - 1;
context.set("counter",1);
return msg;
}
JGKK
6 April 2020 20:52
6
This doesn’t work what your proposing. The first part will will only get executed when a new message arrives and the while loop will only go once because you return out of it.
He needs to use the node.send() function as sending messages from a loop is asynchronous work.
This will count down one by only when new messages arrive and the while loop has no function at all in your variant.
Yeah, ok.
I am just now learning about this asynchronous
stuff and reading about node.send( )
.
Sorry for the incorrect reply.
I am going to have to read up on what this new thing is. Though it has been around since NR 1.x onwards.
And I think I also didn't quite get what was meant anyway.
Another offering!
This time with a optional delay built into the countdown.
[{"id":"fbf79e8f.4b4f9","type":"function","z":"4487e413.bb781c","name":"Countdown","func":"var counter = 10;\n\nvar countdown = setInterval(function(){\n node.send({\"payload\":(counter)});\n counter--;\n\n if (counter <5) {\n clearInterval(countdown);\n }\n}, 1000);","outputs":1,"noerr":0,"x":380,"y":990,"wires":[["773a223c.2189ac"]],"icon":"font-awesome/fa-hourglass-1"},{"id":"3a80d1cb.a423de","type":"inject","z":"4487e413.bb781c","name":"","topic":"","payload":"","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":210,"y":990,"wires":[["fbf79e8f.4b4f9"]],"icon":"node-red/arrow-in.svg"},{"id":"773a223c.2189ac","type":"debug","z":"4487e413.bb781c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":560,"y":990,"wires":[]}]
Edit// flow updated
Good day all,
Thank you all for the effort.
the version installed is 0.15.2 (i think i need to do some updating )
I tested the version of Paul-Reed and that does exactly what i need.
the current version is 5 nodes and i can finaly replace them.
Again many thanks for your quick help.
1 Like
system
Closed
21 April 2020 08:33
10
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.