Help with this template

Hi, Unfortunately I can not figure out what is wrong and need some help here. (this code was published on the forum in 2022 and I’m attempting to adapt it. UID is new)
Thank you.
I have two sensors as you can see in the template. My goal to display status as Closed, Opening, Open, Closing,
For the sensors: reed switches reported to HA from ESP32 as this: Garage Door Closed: OFF. Door in the middle: both ON, Garage Door Open: OFF
This is what I see on the dash board when door is closed. And sensor unavailable for any other state.


I think logic is correct but syntax is a big question.


  - trigger:
      - platform: state
        entity_id:
           - binary_sensor.garage_attic_status_garage_door_open
           - binary_sensor.garage_attic_status_garage_door_closed
      -  platform: homeassistant
         event: start 
  - sensor:  
           name: "Garage Door Status"
           unique_id: "917ff5d4-70d2-4553-8d3a-97c9f1beea4b"
           state: >
             {% if is_state('binary_sensor.garage_attic_status_garage_door_closed', 'off') %}
               Closed
             {% elif trigger.platform != 'homeassistant' %}
               {% if (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garage_attic_status_garage_door_closed') %}
                 Opening
               {% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garage_attic_status_garage_door_open') %}
                 Closing
               {% else %}
                 Open
               {% endif %}
             {% else %}
               Open
             {% endif %}

If the sensor is supposed to be based on those triggers, then they need to be the same item. By having a hyphen before them both, you have created a sensor without a trigger and a trigger without a sensor.

- trigger:
    - platform: state
      entity_id:
        - binary_sensor.garage_attic_status_garage_door_open
        - binary_sensor.garage_attic_status_garage_door_closed
      to:
        - "on"
        - "off"
      from:
        - "on"
        - "off"
    - platform: homeassistant
      event: start 
  sensor:  
    - name: "Garage Door Status"
      unique_id: "917ff5d4-70d2-4553-8d3a-97c9f1beea4b"
      state: >
        {% if is_state('binary_sensor.garage_attic_status_garage_door_closed', 'off') %}
          Closed
        {% elif trigger.platform == 'state' %}
          {% if trigger.entity_id == 'binary_sensor.garage_attic_status_garage_door_closed' %}
            Opening
          {% elif trigger.to_state.state == 'on' 
          and trigger.entity_id == 'binary_sensor.garage_attic_status_garage_door_open' %}
            Closing
          {% else %}
            Open
          {% endif %}
        {% else %}
          Open
        {% endif %}

Thank you!! I understand now why the Template Editor was complaining about triggers.

Well, Template Editor still complains: trigger’ is undefined.
When
both sensors are ON ( door is in the middle)
and door Open is OFF (door open)

The Template editor tool doesn’t do anything but render templates… if you don’t set up a template to provide a value for the variable trigger it can’t do anything with that variable. For example, to test your restart trigger, you would need to add the following at the top of the Template editor’s field:

{% set trigger = {"id": "1", "idx": "1", "alias": null,
"platform": "homeassistant", "event": "start", "description": "Home Assistant starting" } %}

yeah, I tested by pasting the code into configuration.yaml too.
Similar result: It reports Closed on sensor Door Closed OFF
And Unavailable for all other conditions.
I made assumption that TE reading real time sensor status? I was manipulating sensors while watching TE output.
Is there a log I can read to find where the template fails?

Yes, it does read the sensor’s current state. However, the only part of the template that “reads a state” is this one:

{% if is_state('binary_sensor.garage_attic_status_garage_door_closed', 'off') %}

All else relies on reading the trigger variable which doesn’t exist within the Template Editor (unless you explicitly define it, for testing purposes, the way Didgeridrew suggested).

The following is working for me…

  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.garage_attic_status_garage_door_open
          - binary_sensor.garage_attic_status_garage_door_closed
        to:
          - "on"
          - "off"
        from:
          - "on"
          - "off"
      - platform: homeassistant
        event: start
    sensor:  
      - name: "Garage Door Status"
        unique_id: "917ff5d4-70d2-4553-8d3a-97c9f1beea4b"
        state: >
          {% set current = this.state or 'Open' %}
          {% set closed_ent = 'binary_sensor.garage_attic_status_garage_door_closed' %}
          {% set open_ent = 'binary_sensor.garage_attic_status_garage_door_open' %}
          {% if is_state(closed_ent, 'off') and is_state(open_ent, 'on') %}
            Closed
          {% elif is_state(closed_ent, 'on') and is_state(open_ent, 'on') %}
            {{ 'Closing' if current == 'Open' else 'Opening' }}
          {% else %}
            Open
          {% endif %}

First and most important, Digerdrew, Thank you for helping with this template. I’m learning a lot. But I’m new to this type of programming and probably making mistakes on my way. May be I missed something while copy - paste in to my configuration.yaml.
This my installation:

  • Installation methodHome Assistant OS
  • Core2025.5.2
  • Supervisor2025.05.3
  • Operating System15.2
  • Frontend20250516.0

Log details (ERROR)

Logger: homeassistant.config
Source: config.py:357
First occurred: 8:24:06 PM (6 occurrences)
Last logged: 8:28:22 PM

  • Invalid config for ‘template’ at configuration.yaml, line 96: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value ‘sensor->0->state’, got “{% set current = this.state or ‘Open’) %} {% set closed_ent = ‘binary_sensor.garage_attic_status_garage_door_closed’ %} {% set open_ent = ‘binary_sensor.garage_attic_status_garage_door_open’ %} {% if is_state(closed_ent, ‘off’) and is_state(open_ent, ‘on’) %}\n Closed\n{% elif is_state(closed_ent, ‘on’) and is_state(open_ent, ‘on’) %}\n {{ ‘Closing’ if current == ‘Open’ else ‘Opening’ }}\n{% else %}\n Open\n{% endif %}\n\n \n”
  • Invalid config for ‘template’ at configuration.yaml, line 96: invalid template (TemplateSyntaxError: unexpected ‘)’) for dictionary value ‘sensor->0->state’, got “{% set current = this.state or ‘Open’) %} {% set closed_ent = ‘binary_sensor.garage_attic_status_garage_door_closed’ %} {% set open_ent = ‘binary_sensor.garage_attic_status_garage_door_open’ %} {% if is_state(closed_ent, ‘off’) and is_state(open_ent, ‘on’) %}\n Closed\n{% elif is_state(closed_ent, ‘on’) and is_state(open_ent, ‘on’) %}\n {{ ‘Closing’ if current == ‘Open’ else ‘Opening’ }}\n{% else %}\n Open\n{% endif %}\n \n”

It looks like a had a typo… there should not be a ) after 'Open' in the first line of the template, I have corrected it above.

It Works now!. Thank you!