Cancelling a WHILE loop

Short Version: Does a running WHILE loop (repeating actions) terminate when a rule is re-triggered?

Long Version: I have two rules...let's call them Rule A and Rule B. Rule A is triggered when my thermostat mode (home, away, etc.) changes. It sets some zone occupancy states (various HVAC zones are declared occupied or unoccupied) and then Runs rule B. Rule B is triggered by either Rule A or by a change in my thermostat's operating state (idle, heating, cooling, etc.). If idle, the rule exits. If heating or cooling, the rule evaluates the temperatures and occupancy states of each zone and closes any zone damper to spaces that are either unoccupied or where the setpoint is already satisfied. A while loop continues to check each zone every minute or so and closes off each zone that becomes satisfied. Once the heating or cooling cycle is done, the operating state returns to idle, the WHILE loop terminates and the rule opens all the dampers before exiting.

I'm tinkering with the rule tonight to change some things and am seeing some undesireable behavior. It seems I have a timing issue. There are some cases where the mode changes (triggering Rule A) while there is an active heating or cooling cycle (Rule B is already running). When Rule A completes and triggers Rule B (which is already running and repeating actions), the two instances of Rule B are conflicting with each other. Is this accurate? Or am I looking at this the wrong way? If I'm right, is there a way that Rule A can terminate the repeating actions of Rule B before re-triggering it?

I should also add that I use a local boolean in Rule B. The rule has a predicate condition for the boolean to be TRUE. The first action of Rule B is to set the local boolean to FALSE, and then back to TRUE before the rule exits. If the local boolean is false, will the "Run Actions" command from Rule A even work?

No; it is important to carefully consider your triggers and actions to guard against this "re-entrant" behavior. An easy way, if you don't otherwise care that the rule is re-triggering, is to make the repeat stoppable and insert a "Stop Repeating Actions" first in your rule (or somewhere before it gets to a point that matters). This must be done with in the same rule. From another rule, you can use the "Cancel Rule Timers" action from one rule on another (or the same rule); that affects more than just repeats, but if you don't have any other schedules in that rule that you care about (delays, periodic triggers, time-based required expressions, etc.), you should be good.

If that doesn't help, it would probably be easiest to answer the rest with a screenshot of both rules (and a description of what you want if you left out anything above).

Good luck!

1 Like

Just tried this without success. It does cancel the repeating actions, but also seems to exit the rule without running the actions AFTER the while loop, so the dampers are left in the closed state and the private boolean is left as FALSE, preventing any future triggering of the rule.

Tomorrow I'll try your suggestion to insert some logic to "Stop Repeating Actions. "

I ended up spending my time adding some additional conditions and actions to Rule B that eliminated the need to retrigger it from Rule A. With that relationship eliminated there isn't any need to cancel the WHILE loop. Overall this has simplified the rules and made them more rubust. A better solution all around.