Hello everyone,
I have a fairly large number of Hue lights at home. I also have a state-based set of automations that keeps track of whether lights should be “ambient,” “bright,” etc., depending on what the need for lighting is. The series of automations responds to switches, times of day, etc., and takes care of setting the appropriate scene using the Hue integration.
It is also possible to control the Hue lights from outside of Home Assistant, using the Hue app. When this happens, Home Assistant can detect the state change and fires a state_changed event.
I’m trying to detect when a light in a particular room is controlled in this way, so that the Home Assistant-based automations know to back off for some time and just let the externally set scene go unchanged. The rationale is that if I’ve set a scene through the Hue app, I know better than the automations.
The following code works for this purpose. But with the large number of state_changed events, I am wondering whether it’s not advisable to be iterating over groups to construct the full list of entity_ids each time a state_changed event fires. It’s not practical to maintain an entire list of lights just for this purpose, which is why I’m using groups.
I could add a condition that checks for “light.” in the entity_id, but I don’t know enough about HA’s processing of conditions to be sure that when that pattern isn’t met, that the other condition won’t be checked.
Can anyone think of a more efficient way to do this? Or are there enough optimizations under the hood that the concern about efficiency above is moot?
- alias: a_i_detect_external_hue_activity
mode: parallel
trigger:
- platform: event
event_type: state_changed
variables:
groups:
- group.bed_on_group
- group.hall_on_group
condition: >
{% set ns = namespace() %}
{% set ns.entities = () %}
{% for group in groups %}
{% set ns.entities = ns.entities + state_attr(group, 'entity_id') %}
{% endfor %}
{{ trigger.event.data.entity_id in ns.entities}}
action:
- service: back_off_automation
data:
light: "{{ trigger.event.data.entity_id }}"