How to loop a rule?

Hi to all,

As my thermostat valves (Eurotronics Spirit) do not always be set correctly at the first try nor the second, I want to install a loop that ensures that the valves are set correctly.

The rule I imagine is the following, but replacing rule 9000A by the rule itself.

The idea is to create a loop that tries every 10 seconds to set the valve mode until the mode changed. I can't select the rule itself under the run rule actions. Any ideas ?

Or will this work ?

Instead of Exit rule, add the command « Stop Repeating Actions ». Make sure you set your loop to stoppable. Yours is currently not setup that way.

It might loop forever if it doesn’t detect that it changed to heat mode. You could set a limit to the number of iterations if you want to avoid this.

1 Like

Thank you for your help. Did you think at a rule like this:

1 Like

You can use a 'while' type of loop. Use a Simple Conditional action with the test for the correct mode on the thermostat, and the action is the Repeat every 10 seconds. It will repeat until the mode is correct.

2 Likes

What you wrote should work. That said, I would go with @bravenel’s excellent recommendation.

I haven’t had an opportunity to try while loops yet - will have to try them!

Sorry, but I don't understand how to set up this rule. I know while loops in programming, but I don't get at it here.

while Act_Heating_Kitchen Mode: off
Thermostats: Act_Heating_Kitchen --> Mode: Heat
Delay 10
wend

There's an example in this thread, which I often refer people to for repeats:

The topic at hand there is repeated notifications, but that's really irrelevant--the information about repeats in general is pretty useful. :slight_smile: There's the "while"-style loop, using a simple conditional as mentioned above, as the first and second example in that post.

2 Likes

Thank you for the link. So the rule will be like this:

Once you get it, it sounds logic. Personally, I would prefer the possibility to create a true WHILE - WEND (or in Hubitat language END-WHILE) loop and also a FOR - NEXT (END-FOR) loop. Even if all this is doable in an other way it is not the standard programmer way.

You should be able to achieve both of those with the existing repeat action. It may be different from what you're used to, but rules aren't really programming (though you do have to be able to think through what you're doing like a programmer). If you do want that, you have the option to write a custom Groovy "app" for Hubitat--but I've written quite a bit and that particular syntax isn't something I think I've ever used on Hubitat. Guess it doesn't seem "Groovy" enough to me. Still available if you want it, though, given the compatibility with Java. :slight_smile:

Not sure about that. There are many hubs out for beginners that allow simple rules. To be honnest, I experience the same problems with Hubitat that I experienced with other hubs (reports not correct, zwave commands that don't execute everytime, and so on) BUT with the rule engine I can mostly work around these problems. This is the true power of Hubitat and it may be more attractive to geeks and programmers than other systems. For me, Hubitat is for Smart Home what KNX is for large buildings (I have to do with large buildings). So it should feature a programmer logic, that can coexist with the other logic. A compromise might be a kind of help page "For programmers" that features examples for all possible cases a programmer might look for like "How to simulate a while loop", "How to ..." But maybe I'm simply to exigent. At least there is a very kind and helpful community :wink:

Here is one of my rules that I changed, but now I have another question. Normally every "while" loop will run until the condition is true and the remaining lines of the rule will only be executed once the loop is exited. So this rule may take some time to finish. What happens if another event starts this rule again while it still runs ? Is the rule executed as a separate instance and both instances run side by side ? - are they qued or restarted ?

Yes, you get two instances of the rule, and this will probably blow up on you. So, you should use Private Boolean (which starts out true), to protect against that. Test is for false at the beginning, and if false, exit the rule. Then right after that set it to false. At the end of the rule, set it to true. That way, only a single instance will actually run.

1 Like

Thank you for the clarification.
My rule is triggered by different events and one is a window contact. So when the window is opened the rule triggers, but when the window is closed 5 seconds later the rule is triggered again and the second instance has to be run to set heating correctly. If the rules were queued, everything would be fine.
Is there a way to archive this ? - Exiting the first instance before running the second one ?

No. You need to rethink your logic, to better separate the cases.

Suppose I create a global variable "Semaphore_HL".
At the start of the rule I do a while loop that checks the semaphore. If it is true, the while loop, loops until it is false. If it is false, I set it to true and the rule continues. At the end I set the semaphore to false. The second instance should hang on at the loop at the beginning until the first instance executed. Isn't that a good logic ?

Something along those lines can be made to work.