Check my light automation rule for logic errors?


Rule machine is really powerful. There is a lot of complex logic that can be expressed. hats off to the person(s) that came up with this approach to expressing complex logic.

This is the first rule i've created. Its not too complex, The way i want the rule to work is so that the lights in the garage turns on if i open any of the doors to my garage or there is motion detected in the garage. I have a three car garage with a side door.

10 minutes after motion stops i would like them to turn off, unless any of the garage doors are open. I also would like the automation to stop running if the off switch for the lights is hit manually. If there is any motion during the ten minutes turn off delay, i want to cancel the delay and start a new 10 minute delay. Thus the light turns off 10 minutes after motion has stopped and the doors are closed.

I've set my motion detector to have about a ~60 second motion timeout (its a zooz outdoor sensor). i'm checking for any of the conditions every 30 seconds or so which i assume won't tax the processor on the hub very much.

Any error in this logic? I'm testing it as well but wanted to see what you think of this rule for my intended usage. If this works then i plan to use this type of rule in many other rooms where i have motion detectors.

I should state that it seems to run well. I just want to get a second set of eyes on it from you all to make sure i didn't make any beginners mistake that i should avoid.

I see two problems - one real small and the other one is a big problem.

First, you are setting up a delayed action to turn the light off just before the repeat. But since the first part of the the IF statement matches the triggers, it will always do that part of the IF statement on any trigger. And that cancels the delayed action and starts a new one. So that first delayed action before the repeat is only going to exist for a few milliseconds before it is cancelled, so you can remove that line.

The bigger problem is what happens when you get multiple triggers.

When rule machine gets to a repeat, it runs the repeat actions, exits the rule, and sets up a scheduled job that will restart the rule back at the repeat line. In your rule, that will happen every 30 seconds, until it is stopped.

However, while the delay is going, any trigger, such as the motion sensor or a door opening, will start the rule up again from the top, more or less a second copy or the rule. And that will end up starting a second repeat loop. That will happen any time you have the sensor being activated and a door opening, in either order. If another door is opened, you will get another repeat running.

Not only is that using up more hub resources, it can cause unexpected results and sometimes cause an error.

The general way to prevent that is to use a variable to make the rule exit if it gets triggered again while it is already doing a repeat.

Create a boolean variable, and set it to true. You could use a local variable for this.
Just before you start the repeat, set the variable to False.
Just after the Stop Repeating Actions, set the variable back to True.
Then add a line at the very beginning that exits the rule if the variable is False.

That should stop it from creating more than one repeat at a time.

A repeat is needed to make something happen repeatedly such as sending a message every 5 minutes or flashing a light on and off every 3 seconds. But if you are just waiting for certain conditions, there are lots of ways to do this without a repeat loop at all, and that will use even less system resources.

Instead of a repeat, use triggers to only run the rule any time something changes.

Change your triggers to motion sensor changed and any door sensor changed.

motion sensor changed
any door sensor changed

IF motion sensor active OR any door sensor is open then
On: lights
Cancel Delayed Actions
Off: lights —-> delayed 0:10:00 (cancellable)

That rule will run anytime the sensor goes active or inactive or a door opens or closes. With the motion active or any door open right after a trigger, it will turn on the lights and cancel any delayed action so the lights stay on.

If the sensor is inactive and all doors are closed, it will then start the delayed action to turn off the lights.

You don’t need to do anything if the light is turned off manually, since there is no repeat to be stopped. Eventually the rule will end up sending another off command, no big deal.

1 Like

I just found this: [Released] Rule 4.0
this is a fantastic thread, very helpful.

i just used the doc to construct the rule: Rule-4.0 - Hubitat Documentation
the doc is decent but not as informative of how logic is best constructed as the thread is.

your points about the logic processing makes sense with the help of the thread explaining the processing logic, thank you so much!!!