Need Help With State Trigger Automation Using Template

Hello, Home Assistant community!

I am currently working on an automation that triggers based on the state of multiple light entities from the ESPHome integration. My goal is to notify myself whenever any light from the ESPHome integration turns on. Here’s the automation I’ve tried:

- alias: Alerta - Test
  initial_state: true
  trigger:
    - platform: state
      entity_id: >-
        {{ states.light
          | selectattr('entity_id', 'in', integration_entities('esphome'))
          | map(attribute='entity_id')
          | join(', ')
        }}
      to: 'on'
  action:
    - service: notify.antonio_desktop
      data:
        title: "Teste"
        message: "{{ trigger.to_state.name }} ligou"

However, I am encountering an error: Automation with alias 'Alerta - Teste' failed to setup triggers and has been disabled: Entity {{ states.light | selectattr('entity_id' is neither a valid entity ID nor a valid UUID for dictionary value @ data['entity_id']. Got None

From my understanding, it seems like I cannot use a template in the entity_id field of a state trigger. However, I really need to be able to monitor multiple entities and would like the flexibility of a template for this purpose.

Does anyone have any suggestions on how I can adjust my automation to work as intended? Or is there another approach that could help me achieve my goal? I would really appreciate any help or guidance!

Thanks in advance!

You are correct, templates are not supported in State triggers except in the for key.

You may be able to use a template trigger, but it would be helpful if you could more clearly describe what your goal is. From what you have posted I would start by creating a template sensor:

template:
  - sensor:
      - name: ESPHome lights on
        state: |-
          {{ states.light
          | selectattr('entity_id', 'in', integration_entities('esphome'))
          | selectattr('state', 'eq', 'on')
          | list | count }}
        attributes:
          most_recent: |-
            {{ states.light
            | selectattr('entity_id', 'in', integration_entities('esphome'))
            | selectattr('state', 'eq', 'on')
            | sort(attribute='last_changed', reverse=true)
            | map(attribute='entity_id')
            | first }}
- alias: Alerta - Test
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.esphome_lights_on
      not_to: 
        - unavailable
  condition:
    - "{{ trigger.to_state.state | int > trigger.from_state.state | int }}"
  action:
    - service: notify.antonio_desktop
      data:
        title: "Teste"
        message: "{{ state_attr(trigger.to_state.attributes.most_recent, 'friendly_name') }} ligou"

Keep in mind that any new light entities coming from ESPHome that you add, but don’t want to be notified about will need to be rejected in your template. In the long run, approaches like this usually end up requiring just as much, or more, maintenance than you will have with a static list of entities in a State trigger.

1 Like

Hey, thanks for getting back to me, that was super helpful. I’m trying to create a flexible entity_id list (template) that can automatically track some specific devices based on a filter, so I can use the trigger.to_state to get their names in the notify template.