I am trying to code a rule to turn a light off after 5 minutes. The light either gets turned on via a smart switch or by opening a door. This part is simple enough.
What I can’t get to work is that if the door is opened sometime during the 5 minute wait period, I want the light to stay on for 5 minutes from that execution.
I have this working in webCore with a simple “if the contact stays closed for 5 minutes then turn the light off”, but I’m trying to convert everything to the rule engine. Using the “doors stays closed for 5 minutes” in the Rule Engine as a trigger results in the light being turned off right away, I’m guessing because the 5 minute counter doesn’t start with the light turning on.
The webCore piston also has the light turning on code in it, which is why it might be working with that trigger. I have a separate Rule Engine Rule to turn the light on when the door opens. Maybe the key is combining the two rules?
I’ve tried a number of variations, but here is where I’m at now. This turns the light off after 5 minutes UNLESS the door is opened in the interim, which results in the light staying on.
Hey @Bry, you can create two rules for this scenario. Please see below for the examples and let me know if you have any questions.
Rule 1: this is based on your screenshot and combines the other rule you mentioned for turning on the light when door is opened.
Trigger: when the light is turned on, or when the door is opened
Flow: turn the light on, wait for 5 minutes, and turn the light off if the door has stayed closed for the past 5 minutes.
Rule 2: when the door was open, then closed, and stayed closed for 5 minutes, turn the light off. So if the light wasn’t turned off because the door was opened during the 5 minutes interim in Rule 1, this rule will turn off the light 5 minutes after the door was closed.
Trigger: when the door has stayed closed for 5 minutes
Flow: turn the light off.
Please note that in general, it’s ok to send “on” or “off” command to a light when it’s already in the “on” or “off” state, and it won’t change the state, unless the device handler is implemented to toggle internally. If so you will need an IF_Condition in the beginning of both rules to determine if the command should be sent based on its current state.
Thanks, James. That makes perfect sense. It didn’t occur to me that opening the contact was what started the timer for the “remains open for” portion of the rule.
Following up, is it possible to use conditional logic on the trigger side? In this application, the rule turns on an outside light, so in the flow I made it conditional to only turn the light on and off after sunset.
But I’d also like the light to turn off after 5 minutes when I turn it on via the paddle on the switch or via an app. I could add it to the current trigger, but the light would only shut off if that occurred after sunset.
I suppose I could set a separate rule but I’m curious if there is a way in the trigger to specify conditionally.
IF the contact changes to open AND the time is after sunset
IF the light is turned on via the switch
You can use the IF_Condition in the Flow to only turn on the light if it’s after sunset. So it will be a tiny change in the Rule 1 example. See the screenshot below for example when the door is opened, it will only turn the light on if it is after sunset, and the remaining of the light off logic remains the same.
The triggers should be simple, and the condition logic should be in the Flow section to determine if it should continue the execution or different action should be taken. Let me know if this helps.
Got the logic! Thank you!