Better Automations
I feel like this must have been discussed a million times, but since I couldn’t find a discussion like it - here we go:
Also, before I start. I am aware there are more advanced extensions/plugins/add-ons that provides functionality similar to what I suggest. However, I would personally love to have something like this in the core distribution. I have therefore tried to not make it too complicated and keep the syntax similar to the current workflow with automation.
The problem with the current system
I cannot be the only one here who likes Home Assistant, but gets frustrated with automations as soons as they become anything but the simplest “if this then that”. A typical example would be my outdoor lights.
- I want them to turn on at 30% brightness at sunset
- I want them to turn off at 22:00
- If my motion sensor(s) detects any motion I want to turn them up to 100% for 5 minutes
The scenatio above seems simple enough af first glace, but as most of you probably already see there a a bunch of edge cases which are not easily handeled.
The naiive approach would be to make a few automations like this
- One which turns the lights on at 30% at sunset
- One which turns them off at 22:00
- One which turns them up to 100% when motion is detected
- One which turns them back to 30% 5 minutes after the motion was detected
The problem is obviously. What happens if I detect motion at 21:58 ? Then the lights would turn off at 22:00 and then turn back on at 30% at 22:03 and stay on for the rest of the night.
This can of course be fixed by a “hack” using a fifth automation, or perhaps just stop motion detection 5 minutes before the lights turn off anyway.
It gets even worse in my living room. I have a morning/evening mode (lights on), a night mode (dim lights), and a TV mode. All these modes seems to interact in annoying ways which requires way too much tinkering to get right.
For instance: If the lights turn on for an hour from motion in the evening, and 10 minutes (dimmed) during the night, and should stay dimmed when I watch TV. If I then trigger an “evening motion” 20 minutes before it’s night, and then a “night motion”, then watch some TV, pause the TV and the timers run out - what should happen? Don’t try to figure it out, because it was just a senario I made up, and I don’t know if it’s actually an edgecase. However, it’s something you need to think about when automating you home, and I think it’s a mess.
I think the main point is that automations lack some sort of encapsulation. When I have written an automation I never feel it is “done”. Because it always seems to be affected by the next automation I might write.
Scheduling
This is not a replacement for the current system, but rather an addition.
Since all the good names (automation, state, scenes, events etc) are taken I will here call it a schedule.
Instead of the current automation layout:
- Trigger
- Condition
- Action
One could have a system where a schedule looks like this:
- Start
- End
- Condition
- Priority
- Scene
I believe this is much more flexibel to the current system and can coexist with it.
It does require that home assistant keeps track of all the active schedules, but it feels like the coexistence of multiple use cases becomes so much easier. For instance, my outdoor lights could be written as:
start:
- platform: sun
event: sunset
end:
- platform: time
at: "22:00:00"
priority: 5
scene: scene.outdoor_evening
start:
- platform: state
entity_id: binary_sensor.occupancy_outside
to: 'on'
end:
platform: state
entity_id: binary_sensor.occupancy_outdoor
to: 'off'
for:
minutes: 5
condition:
- condition: state
entity_id: sun.sun
state: 'below_horizon'
- platform: time
before: "22:00:00"
priority: 10
scene: scene.outdoor_evening_motion
Depending on how advanced you want to go it will in many cases (but not all) make sense to build a tree structure of schedules. Defining a parent makes it easier to limit some schedules to only be active when the parent is active.
id: light outside
start:
- platform: sun
event: sunset
end:
- platform: time
at: "22:00:00"
priority: 5
scene: scene.outdoor_evening
id: motion light outside
parent: light outside
start:
- platform: state
entity_id: binary_sensor.occupancy_outside
to: 'on'
end:
platform: state
entity_id: binary_sensor.occupancy_outdoor
to: 'off'
for:
minutes: 5
priority: 10
scene: scene.outdoor_evening_motion
In any case. The main advantage is that when the “motion outside” start, and then ends, home assistant will automatically transition to the active scene with the highest priority. There is no need to handle a lot of edge cases with overlapping time intervals. If there is no active scene they should probably just turn off or go to some other sensible default.
Please let me know what you think