Accessing trigger.entity_id inside a wait_for_trigger?

Monitoring multiple doors - when any are open longer than x minutes, send a notification and wait for the door to close, then send another notification. It works fine until I try and get fancy with the entity_id in the wait_for_trigger. When I save, I get “Message malformed: Entity {{trigger.entity_id}} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]”. Is there a way to pass the entity_id to the wait_for_trigger context?

trigger:
  - type: opened
    platform: device
    device_id: 0a122d0a899643c919161c48dec67429
    entity_id: binary_sensor.test_device
    domain: binary_sensor
condition: []
action:
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was left open"
  - wait_for_trigger:
      - platform: state
        entity_id: >
          {{trigger.entity_id}}
        to: "off"
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was closed"

In the wait action you are defining a new trigger that has not happened yet so there is no trigger.entity_id.

You may be able to get around it with a variable to store the first trigger’s entity_id (untested):

trigger:
  - platform: state
    entity_id: binary_sensor.test_device
    to: 'on'
condition: []
action:
  - variables:
      first_trig_entity_id: "{{ trigger.entity_id }}"
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was left open"
  - wait_for_trigger:
      - platform: state
        entity_id: >
          {{ first_trig_entity_id }}
        to: "off"
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was closed"

I also removed the device trigger as I hate them (for good reason).

If you are going to add a list of entities in the trigger you need to run this automation in parallel mode. So that each door event gets it’s own wait for close. The default automation mode is “single”. In single mode if one door opens then another opens before the first door is closed the automation will ignore this second trigger.

1 Like

Thanks for jumping in to help. I get “Message malformed: Entity {{ first_trig_entity_id }} is neither a valid entity ID nor a valid UUID for dictionary value @ data[‘entity_id’]” when trying to save with your suggestion. There has to be a way to access the original trigger entity_id! :slight_smile:

Okay, found the solution - wait_template.

alias: Notify - Test Device Testing 2
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.test_device
    to: "on"
condition: []
action:
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was left open"
  - wait_template: "{{ is_state(trigger.entity_id, 'off') }}"
    continue_on_timeout: true
  - service: notify.signal_group_debug
    data:
      message: "{{ trigger.to_state.name }} was closed"
mode: parallel
max: 10
1 Like