Repeat While Loop

This is the special case called a While Loop. It starts with a Simple Conditional Action on a Repeat Actions. The loop runs until the condition is false. If the condition is false to begin with, it skips the actions up to End-Rep.

Sorry to push this a bit further but this is the first time I've come across this method.
In the very simple rule below (ignore the trigger) while the contact sensor is open it will turn the switch on every 5 seconds. If it closes, the rep will stop.

EDIT: The repeat should probably be after the IF.


(I do appreciate the concept of while loops BTW). :blush:

I think you may be missing the point to use the Simple Conditional Actions instead of the normal If.
That way, the if should be evaluated each time the repeat ends.

Actually, the IF is evaluated before each time through the actions following the Repeat.

There is a table of actions to run, in the order they should run. Suppose a regular Repeat is action number 3. The actions will loop back to action number 4 until stopped (or the n times counter is done). For a while-loop repeat as action number 3 (the Simple Conditional Action being number 3), it will loop back to number 3, thus evaluating the Simple Conditional Action again each time around. If false, it skips actions until it hits the End-Rep.

The logic is that simple. Either loop back to this action plus 1 (normal repeat), or loop back to this action (while loop repeat).

2 Likes

In general (not just relating to Repeat) when to use a 'Simple Conditional Action' vs an 'IF (conditions) THEN' is confusing to me.

Simple Conditional Action, as its name suggests, is very simple. It has a single condition applied to a single action. If the condition is true, do the action, otherwise don't do it.

IF (conditions) THEN is more complex. It allows a full logical expression built up of conditions, and logical operators like AND and OR, and parenthesized sub-expressions. It starts a block of actions, that will either run or not depending on the evaluation of the logical expression. It can be followed by ELSE-IF (conditions), which means if the preceding IF (conditions) is false, then try these conditions instead. Or, it can be followed by ELSE. If the preceding conditions were false, then run the actions after the ELSE.

2 Likes

Simple Conditional Actions are useful to do tests that would restrict running the rule. For example, suppose we only want a rule to run if it's a weekday

IF (Days Of Week: Saturday, Sunday) Exit Rule

Or we only want a rule to run if the mode is Night:

IF (NOT Mode: Night) Exit Rule

etc.

In general, if you only have a single condition, and have a single action, use Simple Conditional Action. Otherwise, use IF (Conditions) THEN.

With one exception, any Simple Conditional Action could be done with the longer IF (conditions) THEN form, just taking more steps to do the same thing. The one exception is the Repeat While Loop, which only works with a Simple Conditional Action as discussed above.

Gotcha, thanks.

Thanks for the comprehensive answer.
I think that was where I was getting confused.
I very rarely use Simple Conditional Actions (I am starting to use them more now) so that was probably where my confusion came from.
Thanks again for the clarification. :+1:

@bravenel When is the 'Repeat every X' delay of the While loop implemented?

Immediately after satisfying the IF condition and before the actions that follow or after all of the actions before re-evaluating the IF condition?

At the time when the Repeat Actions action runs, it schedules the next time the loop will run. Then the subsequent actions after the Repeat and before the END-REP run.

So the order is:

  1. Evaluate IF
  2. If true, schedule next check
  3. Execute actions

?

Yes, that's basically what happens. If the IF is false, then it skips the following actions until the END-REP.

And if I use 'Cancel Timed Actions' from another rule, execution will break out of the while loop and any actions below END-REP will be executed?

No, the rule will stop. Delays and Repeats schedule an event to pickup where they left off, at some time in the future. If you Cancel Rule Timers, all scheduled events are unscheduled. So the rule never comes back.

Thanks for this. My rule setup to chirp piezo repeatedly when arming away delay and intrusion delay states were active worked but had error messages on logs. Revising it to add the "end-rep" which was missing from my rule, eliminated the errors.

2 Likes

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