Loop not ending

Hi,
I am trying to use a conditional loop, but the loop does not end.

Input payload object is:

{"_msgid":"e51e646b.aee378","payload":{"NewTotalAssociations":"2"}}

The condition of the loop is:

msg.payload.NewTotalAssociations >= 0

Inside the loop I have created a function with:

msg.payload.NewTotalAssociations = msg.payload.NewTotalAssociations - 1;
return msg;

Can anyone please give me a hint why the condition does not work?

Thanks

Kallewirsch

You may need to post the node/code as it is hard to see the bigger picture.

Are you doing a for/while loop inside a function OR looping with nodes and wires?

PS, your variable should be a number not a string

{"_msgid":"e51e646b.aee378","payload":{"NewTotalAssociations":"2"}}
{"_msgid":"e51e646b.aee378","payload":{"NewTotalAssociations":2}}

Hi,
I am using node loop of "node-red-contrib-loop".
The payload I showed was the original output from some other node.

When using "while-loop" from node-red-contrib-loop-processing the condition seems to work. Some other things don't still work within the code. So I have to look for more details before pasting updates.

Thanks for help.

If you describe what you are doing/trying to achieve and include a minimal flow example, it is more than likely we can show you how to process your data WITHOUT a loop.

bottom line - loops are dangerous. Avoid loops unless there is no other solution (and 99 times out 100, there is an alternative solution using built in nodes)

If you are returning the msg within the loop, you will never get to 0 ?

Hi,
I am trying to fetch all devices connected to my FRITZ!Box.
This seems to be not so straight forward, since I first have to get the number of devices active by some service of the FB and using this as index I can catch the detailed information. At least this is what I found out how to do it.

I meanwhile get some results with:

[{"id":"eece7cd2.675be","type":"inject","z":"33667947.7eff86","name":"manuell Object","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1D 0:04:00","payloadType":"str","x":120,"y":620,"wires":[["128d2f8c.e6465"]]},{"id":"128d2f8c.e6465","type":"fritzbox-in","z":"33667947.7eff86","device":"7da5e9e7.66eaf8","name":"xxx","service":"urn:dslforum-org:service:WLANConfiguration:1","action":"GetTotalAssociations","arguments":"{}","x":380,"y":620,"wires":[["da43ab0f.6417d8","1ae1a958.2f481f"]]},{"id":"1ae1a958.2f481f","type":"change","z":"33667947.7eff86","name":"","rules":[{"t":"set","p":"count","pt":"msg","to":"$number(payload.NewTotalAssociations)","tot":"jsonata"},{"t":"set","p":"count","pt":"msg","to":"count - 1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":620,"wires":[["8a0949a9.abfa3","a860553.06c9d28"]]},{"id":"8a0949a9.abfa3","type":"while-loop","z":"33667947.7eff86","name":"","condi":"msg.count >= 0","limit":true,"limitTime":"20","time":"wl8a0949a9abfa3","timeType":"msg","x":580,"y":700,"wires":[["72ab32e1.7c935c"],["866055a5.fe5ef8","b606bdd6.246fa8"]]},{"id":"b606bdd6.246fa8","type":"change","z":"33667947.7eff86","name":"","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload","pt":"msg","to":"{}","tot":"json"},{"t":"set","p":"payload.NewAssociatedDeviceIndex","pt":"msg","to":"$string(count)","tot":"jsonata"},{"t":"delete","p":"payload.NewTotalAssociations","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":1020,"wires":[["8e147fdd.fbd43","aa0294d.f4d9568"]]},{"id":"aa0294d.f4d9568","type":"fritzbox-in","z":"33667947.7eff86","device":"7da5e9e7.66eaf8","name":"xxx","service":"urn:dslforum-org:service:WLANConfiguration:1","action":"GetGenericAssociatedDeviceInfo","arguments":"{\n  \"NewAssociatedDeviceIndex\": \"value\"\n}","x":520,"y":1020,"wires":[["23a8216b.b01ec6","ae105d2.252da2"]]},{"id":"ae105d2.252da2","type":"function","z":"33667947.7eff86","name":"","func":"msg.count = msg.count - 1;\nmsg.payload = {};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":700,"y":1020,"wires":[["8a0949a9.abfa3","77fff4e3.df961c"]]},{"id":"7da5e9e7.66eaf8","type":"fritzbox-config","name":"xxx","host":"xxx.example.com","port":"49000","ssl":false,"user":"xxx"}]

why not if I reduce by "1" and use this as condition.
When I debug it shows the value is reduced.
O.K. - meanwhile I use a different loop-node and this seems to work with the same condition.

I am still not finished with my dev. So there might be some errors still in.

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