Help with rules for outdoor motion lights

I've just gotten a Hue outdoor motion sensor and have ordered a few Sonoff ZBMINIs. I'll be using these to automate my outdoor lights in the backyard. The existing switches are just standard "dumb" toggle switches. I have three outdoor lights on different circuits that I want to automate to work together. I've learned that it helps for me to plan out my rules and think through the scenarios before I just dive into RM. I'd appreciate some feedback on this set of rules to see if I'm on the right track or if I'm missing something.

My intent is this:
Scenario 1: Outdoor illuminance greater than 50 lux. Motion detection does nothing.
Scenario 2: All lights are off and outdoor illuminance is less than 50 lux. If the motion sensor detects motion, all three lights come on for 5 minutes and then turn off.
Scenario 3: All lights are off, illuminance is any value. Any of the three switches connected to the ZBMINIs is toggled, all lights turn on and remain on. Toggling any switch will turn the lights off.
Scenario 4: All lights are on (based on previous toggling of a switch), illuminance is less than 50 lux. Motion is detected and the lights (which are already on) are commanded to turn on again. No perceptible change. Five minutes later the lights DO NOT turn off.

I'm planning to build my rules like this:
Create global variable, BackyardLightsOn (varBLO, for short) as a boolean. Motion sensor is MS1, ZBMINIs are SW1, SW2, and SW3.

Rule 1, Switch-based Operation:
Trigger: SW1, SW2, or SW3 is changed.
Actions: IF varBLO is FALSE, Turn SW1, SW2, SW3 to ON. Update varBLO to TRUE.
ELSE Turn SW1, SW2, SW3 to OFF, Update varBLO to FALSE.

Rule 2, Motion-based Operation:
Trigger: MS1 = Active.
Actions: IF lux > 50, Exit Rule.
ELSEIF varBLO = TRUE, Exit Rule
ELSE turn SW1, SW2, SW3 to ON.
Wait 5 minutes,
IF varBLO = FALSE, turn SW1, SW2, SW3 to OFF. (simple conditional)

I added that last conditional for the case that the lights are off, turned on by motion, and then I toggle a switch to keep them on. In practice, I think that the act of toggling the switch will turn the light off (due to behavior of ZBMINI) and it will immediately be turned on again by Rule #1. I think this will be acceptable, but I might have to add a short delay in Rule #1 to prevent the switch from changeing states too fast and getting out of sync.

As an option, I've thought about creating a third rule that will check illuminance and "Pause" Rule #2 if lux > 50. That way I could use this same rule to pause/resume any future rules that are only intended to run during daylight or darkness.

Thoughts or advice? Also, how does one handle the case of additional motion=active triggers that would occur during the 5 minutes that the lights are supposed to be on? I would like them to remain ON until the state has changed to INACTIVE for 5 minutes, as opposed to 5 minutes from the first ACTIVE state. So if motion is detected intermittently over a period of 3 minutes, the total time ON would be 8 minutes.

I've had some success, but still not working like I'd planned.

First I created the rule to control the three lights in unison based on toggling any one of the switches.

I had to add a delay because the first time I tested the rule, it resulted in a continuous loop that had all three lights flashing on and off. Essentially, when the actions of the rule turn the other two lights ON or OFF, it re-triggers the rule...twice. By adding the delay it ensures that all three instances of the rule are able to complete the switch actions before changing the global variable. It seems messy, so I'm open to suggestions to clean it up.

The second rule is meant to operate the lights based on a motion sensor.

This is the second version of the rule. The first time I ran the original version, I realized that the actions (turning on the switches) were triggering the first rule. This resulted in setting the global variable to TRUE and lights would not turn off after the 5 minutes had elapsed. I confirmed this by watching the live log. Then I added the PAUSE and RESUME logic in order to prevent Rule #1 from executing it's actions. It didn't work. When I test this rule it ends up executing the actions of the first rule and setting the global variable to TRUE and the lights don't turn off. I didn't expect to see this since Rule 1 was paused prior to the the switches being turned on.

Any insight is appreciated.

I've got the two rules working together just like I want them now...

Rule 1: Manual Operation from Toggle Switch

And Rule 2: Motion-based operation

I ended up having to create another global variable that is used as a condition for Rule 1 to execute. Rather than pausing Rule 1, I use an action in Rule 2 to change the variable. That way when Rule 1 triggers it immediately exits. The variable is reset after allowing a couple of seconds for the actions to complete.

My only question now is how Rule 2 will behave when the motion sensor is re-triggered within the 5 minute wait time. I'm assuming I need to add a "cancel actions" line somewhere, but this is my first attempt at motion-based lighting so I'm not sure exactly what to add or where to add it.

When the rule is re-triggered, the Wait is automatically cancelled.

Thanks @bravenel. I was just about to edit my post to update that. I was able to confirm that it works as intended by triggering motion, waiting 1 minute, then retriggering motion. The lights were on for a total of ~6 minutes.

Thanks! I'm excited that I was able to get these lights all working as intended.

Now, if you've got any advice on how to prevent my circular triggering issue (actions within the rule cause it to re-trigger), I'd still like to fix that somehow. Even though I was able to use delays to get it to work, it just seems sloppy.

This is very complex, and my instinct is to keep things as simple as possible. I'd tear it all down, and start over.

Get the motion rule working without all of the variable stuff in it at all, but with the luminance check.

For Scenarios 3, this doesn't really have anything to do with motion. So 3 is just a simple rule that turns all 3 on if any switch is turned on; add another rule to do the off part of Scenario 3, instead of using changed. Three rules in all.

That doesn't cover a case where they turn on by motion, but you want them to stay on. You didn't describe that situation in your scenarios. The way I've described it just above, turning any switch off after they've been turned on by motion would turn them all off, irrespective of the 5 minute timer.

To handle Scenario 4 you could add a single variable turned on / off by the Scenario 3 rules that disables the off side of the motion rule only -- to be checked just before turning them off.

[quote="bravenel, post:6, topic:71702"]
This is very complex[/quote]

Yeah... it sure seems that way.

But won't This result in the same circular behavior? Turning on switch #1 triggers the rule and turns on switches #2 and #3, triggering the rule two additional times. Maybe this isn't a big deal... just telling lights that are already on to turn on again.

But wouldn't the motion rule also result in triggering the switch based rules (both on and off)?

You're right, I didn't. But I did decide to add that. That is another reason for the OutsideLightsBackOn boolean and for checking its value before allowing the motion rule to turn the lights off.

Anyway, I hear your advice that a couple extra simpler rules are better than fewer, more complex ones. I'll give this a shot using your suggestions and see if I get get it working with less "overhead. "


You should use Physical On as the trigger for the Scenario 3 rule. This only responds to the physical operation of the switch, not an app turning it on.


This is huge! Thanks!

Just had time to rebuild these this morning. Switching to the "physical switch" trigger was huge. It eliminated all of the circular behavior which in turn allowed me to remove all of the delays and the "Suspended" variables/logic. Separating the manual toggle rule from a single rule into two did eliminate an IF-ELSE condition, but not sure how much impact that will have on performance. Overall, rule execution seem to be much faster and predictable.

The only real change the to rule behavior is as you mentioned above. If the lights are turned on by the motion rule, toggling any switch turns them off. If I want them to be on, I just have to toggle back on again. This could be addressed by checking the boolean at the beginning of each of the manual-on, manual-off rules, but I think I'll leave it as-is for simplicity's sake.

Thanks again, Bruce!