s1r-J / node-red-contrib-loop-processing

Node-RED nodes to help flow looping.
https://www.npmjs.com/package/node-red-contrib-loop-processing
Apache License 2.0
12 stars 21 forks source link

While loop multiple true? #21

Closed zimbo86 closed 3 years ago

zimbo86 commented 3 years ago

Hi, i got a issue using a while loop - may i´m just misunderstanding sth. I start the loop daily a 3pm by an inject. While loop is checking a global variable "brightness". When brightness is under 600 while loop is true and exits. However i notice, that the command is sent multiple times.

For example the loop has just exited and the command was sent, but there is still a waiting "delay" which will send it again after the delay is finished.

image

Is the while loop periodically checking the global variable or what am I missing?

s1r-J commented 3 years ago

While-loop node evaluates condition expression field. If the field is true, it continues loop. This behavior is the same as JavaScript while statement. I think your flow has the wrong True/False.

FYI, I made an example flow for while-loop node.

[{"id":"ff9f2049.a55ef","type":"inject","z":"cacee7e4.aa35c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":140,"y":180,"wires":[["33f3d766.e74ad8"]]},{"id":"feea5c65.bb726","type":"inject","z":"cacee7e4.aa35c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"200","payloadType":"num","x":110,"y":60,"wires":[["1a29dfe7.6420f"]]},{"id":"afff3514.f10948","type":"debug","z":"cacee7e4.aa35c8","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":60,"wires":[]},{"id":"1a29dfe7.6420f","type":"change","z":"cacee7e4.aa35c8","name":"","rules":[{"t":"set","p":"myvar","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":60,"wires":[["afff3514.f10948"]]},{"id":"66596375.a38bec","type":"inject","z":"cacee7e4.aa35c8","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1000","payloadType":"num","x":110,"y":100,"wires":[["1a29dfe7.6420f"]]},{"id":"33f3d766.e74ad8","type":"while-loop","z":"cacee7e4.aa35c8","name":"","condi":"global.get('myvar') > 600","limit":false,"limitTime":10000,"time":"wl33f3d766e74ad8","timeType":"msg","x":400,"y":180,"wires":[["fd434cd.c1e74b"],["571249c5.724ed8"]]},{"id":"fd434cd.c1e74b","type":"debug","z":"cacee7e4.aa35c8","name":"myvar is more than 600","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":650,"y":160,"wires":[]},{"id":"571249c5.724ed8","type":"delay","z":"cacee7e4.aa35c8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":340,"y":280,"wires":[["33f3d766.e74ad8"]]},{"id":"a2f6c3bd.e3be","type":"comment","z":"cacee7e4.aa35c8","name":"myvar is 600 or less than 600","info":"","x":340,"y":320,"wires":[]}]
zimbo86 commented 3 years ago

hmm I dont think so. As mentioned I inject at 15:00, today I logged the outputs:

15:00:00;FALSE 15:10:00;FALSE 15:20:00;FALSE 15:30:00;FALSE 15:40:00;FALSE 15:50:00;FALSE 16:00:00;FALSE 16:10:00;FALSE 16:20:00;FALSE 16:30:00;FALSE 16:40:00;FALSE 16:50:00;FALSE 17:00:00;FALSE 17:10:00;FALSE 17:20:00;FALSE 17:30:00;FALSE 17:40:00;FALSE 17:50:00;FALSE 18:00:00;TRUE 18:06:59;TRUE

18:06 is the 2nd time true which should not happen..

( Labels TRUE and FALSE are inverted here, but just the lables. Outputs are coorect.)

zimbo86 commented 3 years ago

I ended up removing the while loop an using if statements. Now everything works as expected.. image

s1r-J commented 3 years ago

I'm sorry I couldn't be of any help. I will verify while-loop node's code.