Trouble using trigger.entity_id

New to templates, I’ve looked at many examples here and in blueprints but nothing exactly like my use case: I’m trying to use the state of the trigger.entityID of the sensor that caused the automation to run in the first place (state = on) to determine when to end a repeat reminder loop (end loop when state = off). The message in the repeat loop shows the correct trigger entity name, but the template spec in the until section is apparently not evaluating to the proper name because the state of the triggering entity is definitely “off” but the repeat continues.

I am assuming it is the template spec that is the problem because I am assuming that the state of the template is evaluated each time the loop executes, so it should stop after 2 minutes if the door is immediately closed after the automation fires.

alias: Gate Open
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.entry_gate_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    from: null
  - trigger: state
    entity_id:
      - binary_sensor.south_gate_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    from: null
  - trigger: state
    entity_id:
      - binary_sensor.north_gate_sensor_door
    to: "on"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    from: null
conditions: []
actions:
repeat:
      until:
        - condition: template
          value_template: |
            "{{ is_state('{{ trigger.entity_id }}','off') }}"
      sequence:
        - action: notify.mobile_app_iphone
          data:
            message: The {{ trigger.to_state.name }} is still open
        - delay:
            hours: 0
            minutes: 2
            seconds: 0
            milliseconds: 0

I also tried:

   value_template: "{{ state_attr('trigger.entity_id', 'off' }}"

which also doesn’t work.

You can’t nest Jinja statements this way, and you don’t need to. Inside a Jinja expression, trigger.entity_id is a variable that doesn’t need a {{ }} delimiter.

Try:

repeat:
      until:
        - condition: template
          value_template: |
            "{{ is_state( trigger.entity_id ,'off') }}"
      sequence:
        - action: notify.mobile_app_iphone
          data:
            message: The {{ trigger.to_state.name }} is still open
        - delay:
            hours: 0
            minutes: 2
            seconds: 0
            milliseconds: 0
1 Like