Help With Rule - Repeat While Switch Remains On

Hi there, hoping for some help with something that I think someone will hopefully find quite simple and show me the obvious method that is eluding me.

I have had a number of scenarios that involve me needing to create repeating actions in Rule Machine which are started and stopped with the on/off of a switch.

I'd like the actions in a rule to start with the turning on of a switch, then run repeatedly over and over again in a loop until I turn that switch off.

To begin I am going into RM and choosing Repeat While Expression

I then indicate the switch that needs to be on in order for the rule to repeat (while)

I dont understand the next stage, it then asks me to specify the number of repeats I want using n or the time period before it repeats again (I think) - neither of which I want, but the UI insists on this. I dont feel like I should need to know how long it takes to run my rule before it begins again which is what I think the first one is asking for?

My only option is to either cancel or go back into the rule from scratch, if I go in from scratch the rule I was just trying to create is missing.

If I go to create new action

It then remembers I was part way through the rule but keeps insisting on the number or the duration

I feel like I have really misunderstood how to use this.

I've looked in the documentation

It shows the usage as this

But I cant even complete the first stage unless I specify the number or duration, if I do then I can make it run like this

But how do I just get it to start my rule when I turn my switch on, repeat it and only stop it when I turn the switch off again?

Thanks in advance

What are you hoping to accomplish with the repeat? Basically, what specific actions do you want to occur while the switch is on? With repeats, Hubitat needs to know either how many times you want it to repeat (number) or how often you want it to repeat (time).

Your final example illustrates how the repeats work. Every hour, it will turn on the light while the virtual switch is on. Once the switch is turned off, the repeat will end as the expression is no longer true.

The repeat is not dependent upon the amount of time it takes for the actions within it to run. It'll execute as often as the interval you define. Here is an example where the repeat is 7 seconds but the actions take 15. You can see that the repeat is repeating faster than the actions can occur. The First Entry log of the second repeat is happening before the Third Entry log of the first repeat.


If you could setup a repeat without an interval the repeats would happen so fast (likely milliseconds apart) that it would either be useless or cause you system to overheat.

Thanks for your replies and questions.

If, for example I wanted to create a loop where a light turns on and then goes through a set of colour changes in a fixed sequence in a rule and I wanted it to loop that over and over again while ever a switch remained on?

Here's a general example of how you could do 4 colors every five seconds until Test is turned off.

While (Test is on) Repeat every 0:00:20
    Set Color 1
    Delay 0:00:05
    Set Color 2
    Delay 0:00:05
    Set Color 3
    Delay 0:00:05
    Set Color 4
END-REP

I prefer Waits over Delays but the concept is the same.

2 Likes

@pseudonym's example will help with understanding how to do this in Rule Machine. However, for this specific example, Hubitat has a built in app that would do the trick. Have a look at the Color Animation app (Color Animation | Hubitat Documentation) as it is specifically designed for this type of scenario.

1 Like

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