# HomeyScript infinite loop

**URL:** <https://community.homey.app/t/homeyscript-infinite-loop/84613>\
**Category:** Questions & Help\
**Tags:** homeyscript\
**Created:** [July 15, 2023, 7:35am UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613 "2023-07-15T07:35:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fnoller](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@fnoller](https://community.homey.app/u/fnoller)\
**Post date:** [July 15, 2023, 7:35am UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/1 "2023-07-15T07:35:30Z")

</div>

Hi There,

I’m trying to make an infinite loop with a homey script. I want the loop to run as long as a variable is set to true.

This is my code.

```auto
let lightLoop = await Homey.logic.getVariable({id:lightLoopId});
while (lightLoop.value) {
  log('Light loop: ', lightLoop.value);
  await wait(2000);
  lightLoop = await Homey.logic.getVariable({id:lightLoopId});
}

```

Super simple and it works perfectly except the variable lightLoop never updates inside the loop, even when I change the variable to false.

What am I missing?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [July 15, 2023, 8:35am UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/2 "2023-07-15T08:35:19Z")

</div>

> [@fnoller](#):
>
> I’m trying to make an infinite loop with a homey script

That’s not something you should do with HomeyScript (scripts are timed out after 30s).

> [@](#):
>
> except the variable lightLoop never updates inside the loop

My guess is that you’re getting a cached value. Not sure if it’s possible to get a non-cached value, but this sort of functionality should really be implemented as a normal flow.

---

<div class="post-metadata">

**Author:** ![fnoller](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@fnoller](https://community.homey.app/u/fnoller)\
**Post date:** [July 15, 2023, 10:43am UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/3 "2023-07-15T10:43:57Z")

</div>

Thank you for your answer 🙂  
I’ll make it as a normal flow, but I think I will have the same issue if the variable value is a cached value.  
Will it be possible to implement a while loop inside a normal flow, that can be stopped from outside the flow?

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [July 15, 2023, 10:47am UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/4 "2023-07-15T10:47:18Z")

</div>

Flows are event-based, not polling-based (like while-loops).

Your script is waiting for a particular variable to become false. With flows, you just wait for a “Variable has changed” event, and then check if it’s false (and if so, continue):

IF variable XX has changed  
AND variable XX has value “false”  
THEN …  
ELSE …

---

<div class="post-metadata">

**Author:** ![fnoller](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@fnoller](https://community.homey.app/u/fnoller)\
**Post date:** [July 15, 2023, 2:05pm UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/5 "2023-07-15T14:05:36Z")

</div>

Thank you @robertklep , now I know what you mean by flow. I thought you meant, I should code my own “Then” module.

What I’m trying to do is making a light show of 118 hue bulbs. I’m trying to fade each of them through an array of colors. I can’t make this by a standard flow.

If I could make a “Then” module with a infinite while loop, looping through all the bulbs and change each color with the next value in the array every 2 second, as long as the variable lightshow is set to true and stop the loop when it is set to false.

Hope this makes sense.

---

<div class="post-metadata">

**Author:** ![robertklep](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/robertklep/32/160628_2.png) [@robertklep](https://community.homey.app/u/robertklep)\
**Post date:** [July 15, 2023, 3:49pm UTC](https://community.homey.app/t/homeyscript-infinite-loop/84613/6 "2023-07-15T15:49:22Z")

</div>

Your best chance is to write an app for this, because flows will either be too limiting or simply too slow to pull this off (and even with a custom app I doubt that Homey will be able to do this).

To control devices from other apps (like the Hue bulbs) you also need to use the Web API (aka as [`homey-api`](https://athombv.github.io/node-homey-api/)) in your app.
