Hey all–
I’m still trying to get used to HA’s programming model and trying to figure out how to solve certain problems. Here’s one that I’m trying to solve now:
I have driveway lights that I currently turn on at 15 minutes before Sunset and turn off at 1am. This is controlled with a very simple automation.
I want to add a rule such that if it’s dark (let’s say for simplicity sunset to sunrise), if the garage door opens, the garage lights turn on for 10 minutes and then turn off.
Sounds simple right? Just add another rule. Then I start thinking about the edge cases. What if the garage door opens at 12:58am? With a stateless event-driven approach, the problem becomes very complicated quickly as you add more rules. I’d also like to add a third rule that says if the switch ever controlled manually, the “manual override” will take precedence for some amount of time (say an hour). You can start to see even more edge cases here.
My instinct is to keep everything as simple and separated as possible, but it results in a lot of overhead. Implement each of those three rules either as a automation-controlled input_boolean, or as a sensor template:
Rule 1: Sunset-15m to 1am: Is it preferable to implement this as an sensor template, so that I don’t need a separate automation and input_boolean? I’m probably missing something obvious, but after sunset, how do I check if the current time is after the last sunset (there doesn’t appear to be a Sun variable associated with this–only next sunset). Call this the driveway_nighttime_sensor.
Rule 2: This is probably an automation + input_boolean (pseudocode):
Trigger is “garage door state” from: “closed” to: “open”
condition: it’s dark (sunset to sunrise)
Action:
- Set “garage door opened recently input_boolean” to “true”
- Delay: 10
- Set “garage door opened recently input_boolean” to “false”
Rule 3: Similar to rule 2, but with two input_booleans, one to capture whether we’re in “manual override mode” and one to capture what the overridden state is. Yuck.
Now, how to combine everything?
Is it better to combine everything as a template sensor, or again to use an automation and action to control the light? The latter would likely involve duplication between the trigger and the condition or action:
Template sensor approach (pseudocode):
value_template: if (manual override is in effect) -> manual override state
else -> driveway_nighttime_sensor OR garage door opened recently input_boolean
Paired with an automation that triggers on changes of the above template sensor to turn the light on and off.
OR
A single automation (pseudocode):
trigger: “change in manual override in effect” OR “change in manual override state” OR
“change in garage door opened recently input_boolean” OR
“change in driveway_nighttime_sensor”
(a few ways to approach the rest of this: condition/service template, action to call service light.turn_on with value_template, etc.)
Are there any efficiency concerns to be aware of with the above approach? For example, for Rule #1, using a state-evaluation approach likely causes that state to be re-evaluated whenever the time changes (every minute? every second?).
There’s a lot of information here–I’m basically looking for best practices how to solve challenges like these using the tools HA gives us. I’m also looking for recommendations on tried-and-true extensions that people might use to make it easier to solve these programs and easily maintain these configurations (few interdependencies, low duplication, etc.). Any optimizations to the approach above, or even a completely different way of thinking about the problem is absolutely welcome.
Thank you in advance for any time you’re willing to spend and for any information you’d willing to share.