It is trivial to turn every entity in a particular domain off. E.G:
service: light.turn_off
data:
transition: 30
target:
entity_id: all
This ends up being super helpful but only if your intention was to turn everything off.
But what if I want to keep some lights on? To make my use case explicit:
- I have some grow lights that are on their own schedule that really should not be disrupted… even if they’re off for just a second and some other automation turns them back on.
- I have several multi-button light switches switches that have LEDs for each button. The LEDs come on at sunset and should remain on until sunrise even when my “going to bed now” automation turns the rest of the lights off. The LEDs are exposed to HA because I have other automations that indicates state by doing things with the LEDs (e.g. the light switch that turns the garage lights on/off will have the leds blink if the garage door has been open more than $duration) so I can’t reasonably “hide” the leds from HA by setting them to
internal
(the light switches use ESPHome <3).
Right now, the best “solution” is to build and maintain scenes or groups but this can quickly get untenable. You might end up adding each new device/entity to multiple groups.
The far better way would be to support some basic / templatable filtering based on device attributes.
This way you’d end up with something that looks like this:
service: light.turn_off
data:
transition: 30
target:
entity_id: all
exclude_ids: >-
{{ expand(states.light) | selectattr("light_type", 'in' ['ambient', 'signage', 'garden_grow']) | join(', ') }}
Note: where is the selectattr()
function documented? I had to guess about the supported args based on a few community posts!
But, essentially, this turns the “all lights off” statement into “all lights off except those that have an attribute called light_type
with a value of ambient
or signage
or garden
…”.
And ideally ESPHome would support adding arbitrary attributes to an entity so the amount of work required to add a new light to the exceptions list is minimal but that’s a different ask in a different thread; I would be OK with making one change to customize.yaml
for each new light entity to make this work. That is certainly less than creating multiple groups (light.ambient
and light.signage
and light.garden_grow
… etc) and having to manually add each new entity to the appropriate group(s).
This issue has been requested before, but I figured I’d bring it up again because there’s been a variety of proposed workarounds that have varying degrees of success.
- Automation: Turn off all lights, except X
- Exclude Entities: Turn on/off everything, except X - #20 by jon102034050
One of the more ‘scalable’ solutions that comes up is something like this:
- service: light.turn_off
data_template:
entity_id: >-
{% set domain = 'light' %}
{% set state = 'on' %}
{% set compare = 'eq' %}
{% set filter =['nothing to exclude'] %}
{% set MyVal= states[domain] | selectattr('state',compare, state)
| rejectattr('entity_id','in', filter)
| rejectattr('attributes.is_hue_group', '==' , true)
| map(attribute='entity_id')
| join(',')
%}
{% if MyVal.count('.') >=1 %}
{{ MyVal }}
{% else %}
light.dummy
{% endif %}
Which is a more involved version of the above proposed solution because this builds an inclusive list rather than what would effectively be a entities_to_manipulate = set(allEntitiesInDomain) - set(entitiesFromTemplate)
call.
TL;DR: Give use the ability to ‘tag’ entities for automatic and scalable configuration of entities. E.G.: I would like to turn off all lights except those tagged with ambient
OR signage
OR garden_grow
. The ‘tags’ are essentially device attributes.
EDIT: I have created a feature request on the ESPhome side of things, too. Hopefully that provides a bit more context and shows how the capabilities in HA / ESPHome might be used to work together: Provide arbitrary attributes for entities 'announced' to HA via MQTT · Issue #1903 · esphome/feature-requests · GitHub