Hello! I have gotten HA set up in the last week and I’m having great fun creating lots of automations. However, some of them are pretty clunky, and I am wondering if there is a better way to implement what I would call the “any/all” pattern.
What I mean is many of my automations have this form:
trigger:
- (no motion on sensor 1)
- (no motion on sensor 2)
…
- (no motion on sensor X)
condition:
- (no motion on sensor 1)
- (no motion on sensor 2)
…
- (no motion on sensor X)
action:
- (invoke scene to turn out all the lights, set the alarm, etc)
In other words the trigger is any of N things happening (1hr of no motion on any sensor), and the condition is the exact same list to make sure all of those N things are in the same state (1hr of no motion on all those sensors). The result of this any/all pattern is the last one to reach the desired state triggers the action. I find I use this pattern for a bunch of things related to presence of various forms. I solved a bunch of things this way:
– Turn down the thermostat at night when there’s no activity anywhere in the house
– Turn on the alarm when everyone has left the local network AND there’s no motion in the house
– Turn the path lights off when none of the outdoor sensors see motion
It turns out to be a pretty useful pattern BUT it results in a lot of duplication–the same list of sensors appears over and over, and it gets pretty verbose. If I add a new sensor, I have to go through and add it to every automation in both the trigger and the condition.
Is there a way to create some kind of synthetic entity? So that it’s true if any of a list of conditions is true? Or if none, or all are?
Then I could re-use those synthetic conditions in all my different automations without having to keep copying long lists like this.
Maybe I’m just missing some basic concept somewhere but I couldn’t figure out how to do this in an elegant way.
For that example, I think groups would work well. Create a group like:
group:
motion_sensors:
name: Motion Sensors
entities:
- binary_sensor.motion_sensor_1
- binary_sensor.motion_sensor_2
- binary_sensor.motion_sensor_3
This group will be “on” if any of the motion sensors are triggered and “off” if they are all clear.
You can then use group.motion_sensors in your trigger and/or condition instead of the individual motion sensors.
If you ever want to trigger on all motion sensors being triggered rather than all being clear, setting all: true
for the group makes it become “on” only if all the members of the group are “on”.
3 Likes
Thanks! This is working for a number of cases where the triggers are all the same type. I’m running into a bit of a hiccup when they are different types because I can’t put different types into the same group and I can’t use a group in a condition with a duration.
Specifically, I can’t do this:
trigger:
- platform: state
entity_id: group.all_sensors
from: 'on'
to: 'off'
for: 00:30:00
- platform: zone
entity_id: person.a
zone: zone.home
event: leave
- platform: zone
entity_id: person.b
zone: zone.home
event: leave
condition:
- condition: not
conditions:
- condition: or
conditions:
- condition: zone
entity_id: person.a
zone: zone.home
- condition: zone
entity_id: person.b
zone: zone.home
- condition: state
entity_id: group.all_sensors
state: 'off'
*for: 00:30:00*
That last statement, “for: 00:30:00” is not valid in a condition so far as I can tell? At least the UI doesn’t allow “for” to be used with a state change on a condition, and on the other hand, it seems that I can’t treat the group as a device. Device conditions DO allow it, so I can make this work by expanding all the sensors into a big long list as devices and asserting that all of them must individually have been off for 30 minutes.
Similarly I can’t put a time condition on the zone element in the condition either, and in that case, not even without the group, which is a bit of a pain–but I can achieve that by playing with device tracker and making it universally not consider a device away until 30 minutes.
So it’s still pretty clunky when the any/all pattern includes different types (e.g., people and switches).
One of the examples here shows it as a valid option, so it may be worth trying it anyway (switch to YAML mode in the editor).