HomeyScript infinite loop

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.

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?

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

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.

Thank you for your answer :slight_smile:
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?

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 …

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.

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) in your app.