Detect external state change on light in group

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 }}"

Actually, while I am still interested in the answer to the above for when something like this comes up in the future, I am seeing that what I wanted to accomplish is pretty much impossible for other reasons.

I need to look at trigger.event.context to figure out whether the change was initiated externally or by one of my automations and… turns out that’s just not going to work here. Hue lights can be controlled using either a native Hue group or by directly changing the light directly. If the operation is done on a Hue group, I can use the context fields to figure out from the state_changed event that fires on the group entity_id whether the state change was initiated by HA or externally by the Hue app… but I’m still going to get state_changed on all of the individual lights, and the context on those is going to have ‘null’ user_id and parent_id whether the operation on the Hue native group was done by HA or by the external Hue app.

Thanks!

Trying to do exactly the same thing for the same reason. I’ve been playing with storing a time stamp in a helper to ‘remember’ every time an automation or script changed the light - then checking last_updated against the light group to see if I’m still in control. Works ok most of the time - but what I’ve found is that the hue lights will ‘change’ an attribute at random when they are on - seems to be temperature related(?) either way the last_updated stamp gets changed and makes the whole approach flawed… nice try but no banana…