# While loop not working, even though false condition is met

**URL:** <https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520>\
**Category:** General\
**Tags:** debugging, home-assistant\
**Created:** [31 January 2023 21:45 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520 "2023-01-31T21:45:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kravalli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kravalli/32/75357_2.png) [@kravalli](https://discourse.nodered.org/u/kravalli)\
**Post date:** [31 January 2023 21:45 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520/1 "2023-01-31T21:45:54Z")

</div>

Hi,

I am trying to increment the brightness of my led while I hold down my Ikea tradfri button. If I hold the down button, the msg.payload.event.command output is "move". As soon as I let go it outputs "stop\_with\_on\_off" as you can see in the debug menu. However I still get an endless loop, because it still resorts to the true condition, even though the false was met.

Thanks in advance

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d9f0460b8a18a48dab73823f049045781ac98ad5.png)

---

<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:** [31 January 2023 22:04 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520/2 "2023-01-31T22:04:14Z")

</div>

Try to avoid loops at all cost in node-red.

The easiest way to do this is to use a trigger node, which can send a message every second.  
The trigger node can be cancelled by sending a msg.reset (usually i send msg.reset set to false)  
This will cancel the trigger and thus the brightness.

Something like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/3/63b667be6d3fce3c9c61ac72e075de64ab91fe59.png)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [31 January 2023 22:09 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520/3 "2023-01-31T22:09:04Z")

</div>

The Trigger node can also be reset by any msg.payload value you wish to use. So you can trigger on `move` and reset on `stop_with_on_off`.  
e.g.

```auto
[{"id":"c6b3048da8f259f9","type":"debug","z":"da8a6ef0b3c9a5c8","name":"debug 226","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":3820,"wires":[]},{"id":"17ae2d4bda5d3143","type":"trigger","z":"da8a6ef0b3c9a5c8","name":"","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"-1","extend":false,"overrideDelay":false,"units":"s","reset":"stop_with_on_off","bytopic":"all","topic":"topic","outputs":1,"x":400,"y":3880,"wires":[["c6b3048da8f259f9"]]},{"id":"4b948b0a3f5a39e5","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"move","payloadType":"str","x":170,"y":3860,"wires":[["17ae2d4bda5d3143"]]},{"id":"bde352fde2225293","type":"inject","z":"da8a6ef0b3c9a5c8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"stop_with_on_off","payloadType":"str","x":160,"y":3900,"wires":[["17ae2d4bda5d3143"]]}]

```

---

<div class="post-metadata">

**Author:** ![kravalli](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/kravalli/32/75357_2.png) [@kravalli](https://discourse.nodered.org/u/kravalli)\
**Post date:** [1 February 2023 09:54 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520/4 "2023-02-01T09:54:58Z")

</div>

Thanks this works flawlessly

---

<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:** [15 February 2023 09:55 UTC](https://discourse.nodered.org/t/while-loop-not-working-even-though-false-condition-is-met/74520/5 "2023-02-15T09:55:40Z")

</div>

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