The logic for what I’m trying to do:
When the mode is evening or night, turn on chandelier at 30% brightness
When someone walks into the foyer (motion), increase brightness to 60%
When there is no motion for 45 seconds, dim to 30%
When mode is late night (after Night), turn off Chandelier.
Problem: The below rule I created for this seems to work most of the time. The motion sensor is a wired PIR device that instantaneously switches between active and inactive based on motion and has no built in delay before becoming inactive unlike some of my zigbee wireless sensors. The 45 second delay I added for dimming back to 30% works most of the time but once in a while I get an animated light show where the light continuously goes from 60% to 30% and back with no delay as I am moving in and out of motion sensor range.
I would appreciate any pointers on how I can fix this.
Your delay has no way to be cancelled. They function by using scheduled jobs. Once motion goes inactive, a job is scheduled for 45 second later to do the fade. As written, that will happen no matter if motion goes back active or not.
Simple solution would be to make the delay cancelable and add an action under your "motion active" section to "Cancel delayed actions."
Thank you so much for the response. I will try with Cancel Delayed Action. With my current logic I think the light show happens if I happen to be there at the end of 45 seconds. The light goes bright on motion but doesn’t wait 45 seconds any more to dim.
Thank you so much for the response. I will try with Cancel Delayed Action.
Great question on what turns off after Night mode. The lights have been turning off so I was assuming the logic worked but they probably turn off only because someone walks by after Night. I will test it by manually changing mode from Night to Late Night without motion.
I might set this up a slightly different way. There are always different ways to do things. I'd be really tempted to do it as two rules, just to simplify it: one rule to turn the light on when it's evening or night and off otherwise; and a second rule for the motion.
First rule:
Trigger:
Mode becomes *changed*
Action:
IF Mode in Evening, Night THEN
Dim: Chandelier: 30
ELSE
Off: Chandelier
END-IF
Second Rule:
Required expression:
Mode in [Evening, Night]
Trigger:
Motion Foyer motion Active
Action:
Dim: Chandelier 60
Wait for event: Motion Foyer all motion inactive and stay that way
for 0:00:45
Dim: Chandelier: 30 --> fade: 5
The wait for event will be cancelled automatically if the rule is retriggered by further motion.
I used to have really complex rules that did all kinds of things. But eventually I realized that rules that are smaller and less complex are way easier to debug. And, as the saying around here goes, rules are free. There's no reason not to have more of them.
There is one issue with the provided two rule approach. If motion is active when the mode change from Night to something else (I'm guessing Morning) then the first rule will turn the light off with someone in the room, which probably isn't what you want to happen. The second rule will then Dim the chandelier back to 30 when the wait is met so the light will turn back on in Morning mode. The second rule you can handle with cancel pending actions in the required expression. The first rule I think you need to add a Wait for Expression motion inactive before the Off command.
Slight modifications for problems that @pseudonym pointed out.
Trigger:
Mode becomes *changed*
Action:
IF Mode in Evening, Night THEN
Dim: Chandelier: 30
ELSE
Wait for event: Motion Foyer all motion inactive and stay that way
for 0:00:45
Off: Chandelier
END-IF
Required expression:
Mode in [Evening, Night]
Trigger:
Motion Foyer motion Active
Action:
Dim: Chandelier 60
Wait for event: Motion Foyer all motion inactive and stay that way
for 0:00:45
IF Mode in [Evening, Night]
Dim: Chandelier: 30 --> fade: 5
ELSE
Off: Chandelier
END-IF
The first rule should be Wait for Expression. If there isn't motion when the mode changes to anything except Evening or Night then there won't be an inactive motion event. The rule should be looking at the state instead.
The first rule may also want to look at the motion state when mode becomes Evening or Night. If there is motion then you probably want to Dim Chandelier 60 but there won't be anything to set it to 30 unless motion goes inactive then active again. It could stay at 60 until the next mode change. Dim and multiple rules gets very complicated with the interactions. This may just be an edge case but I like to address them. A single rule may be worth reconsidering.
Required Expression
Mode in [Evening, Night]
Trigger Events
Mode becomes Evening
OR
Motion Foyer motion active
Actions to Run
IF (Motion Foyer motion active) THEN
Dim: Chandelier 60
ELSE
Dim: Chandelier 30
END-IF
Wait for Expression: Motion Foyer motion inactive and stays that way for 0:00:45
Dim: Chandelier 30
Wait for Expression: NOT Mode in [Evening, Night]
Off: Chandelier
An assumption I've made is the Evening and Night modes are sequential. Once the rule is trigger for Evening then there isn't a reason to trigger again at Night.
Thank you jabecker and pseudonym for taking the time to provide the code for my scenario. Both the single rule and two rule options work well for me. I had never used Required Expression before so that's new knowledge for me. I will apply this to my other problem rules as well.