# Loop not ending

**URL:** <https://discourse.nodered.org/t/loop-not-ending/68753>\
**Category:** General\
**Tags:** function-node\
**Created:** [9 October 2022 09:48 UTC](https://discourse.nodered.org/t/loop-not-ending/68753 "2022-10-09T09:48:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![khfischbach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khfischbach/32/16627_2.png) [@khfischbach](https://discourse.nodered.org/u/khfischbach)\
**Post date:** [9 October 2022 09:48 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/1 "2022-10-09T09:48:47Z")

</div>

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

Input payload object is:

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

```

The condition of the loop is:

```auto
msg.payload.NewTotalAssociations >= 0

```

Inside the loop I have created a function with:

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

```

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

Thanks

Kallewirsch

---

<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:** [9 October 2022 10:00 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/2 "2022-10-09T10:00:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 October 2022 10:02 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/3 "2022-10-09T10:02:09Z")

</div>

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}}`

---

<div class="post-metadata">

**Author:** ![khfischbach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khfischbach/32/16627_2.png) [@khfischbach](https://discourse.nodered.org/u/khfischbach)\
**Post date:** [9 October 2022 10:21 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/4 "2022-10-09T10:21:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [9 October 2022 10:23 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/5 "2022-10-09T10:23:42Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 October 2022 10:43 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/6 "2022-10-09T10:43:49Z")

</div>

> [@khfischbach](#):
>
> ```auto
> msg.payload.NewTotalAssociations = msg.payload.NewTotalAssociations - 1;
> return msg;
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![khfischbach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khfischbach/32/16627_2.png) [@khfischbach](https://discourse.nodered.org/u/khfischbach)\
**Post date:** [9 October 2022 11:33 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/7 "2022-10-09T11:33:13Z")

</div>

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:

```auto
[{"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"}]

```

---

<div class="post-metadata">

**Author:** ![khfischbach](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/khfischbach/32/16627_2.png) [@khfischbach](https://discourse.nodered.org/u/khfischbach)\
**Post date:** [9 October 2022 11:36 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/8 "2022-10-09T11:36:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 December 2022 11:36 UTC](https://discourse.nodered.org/t/loop-not-ending/68753/9 "2022-12-08T11:36:47Z")

</div>

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