Issue with template states

Hi,

I am struggling with states on this binary sensor. Since im just listening to a event, this sensor ends up being in an “unknown” state once in a while. I want it to go open and closed based on the messages i receive, but if it goes “unknown” i want it to be closed.

Is it possible? Im not sure what i am missing here since “else” is “Closed”

image

Template:
  - trigger:
    - id: 'on'
      platform: event
      event_type: sia_event_XXXX_XXXXX
      event_data:
        code: 'BA'
        message: '1' 
    - id: 'off'
      platform: event
      event_type: sia_event_XXX_XXXXX
      event_data:
        code: 'BR'
        message: '1' 
    binary_sensor:
    - name: Dorsensor_ajax_ny 
      device_class: door
      state: >
       {% if trigger.id == 'on' %}
          Open
       {% elif trigger.id == 'off' %}
          Closed
       {% else %}
          Closed
       {% endif %}

Just wanna close this as solved. Incase someone else need this info.
Thanks too @TheFes at Discord :slight_smile:

Adding this to the template makes the template trigger when home assistant restarts.
That way “unknown” will be gone.

    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
template:
  - trigger:
    - id: 'on'
      platform: event
      event_type: sia_event_XXXX_XXXXX
      event_data:
        code: 'BA'
        message: '1' 
    - id: 'off'
      platform: event
      event_type: sia_event_XXXX_XXXXX
      event_data:
        code: 'BR'
        message: '1' 
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
    binary_sensor:
    - name: Dørsensor Ajax ny
      device_class: door
      state: "{{ trigger.id == 'on' }}"

Something like this? I Still struggle a little with the spacing and when and when not to use - :stuck_out_tongue:

Question on the topic:

  1. Wouldnt it be possible to run an automation that would reload all templates? I guess i would either have to add all the sensors in the automation, or make a group of the sensors?
  2. Instead of adding this into all the sensors, could i add the restart and the reload on top of all the senors? I will probably have about 20 of them in a short time.
template:
  - trigger:
    - id: 'on'
      platform: event
      event_type: sia_event_XXXX_XXXXX
      event_data:
        code: 'BA'
        message: '1' 
    - id: 'off'
      platform: event
      event_type: sia_event_XXXX_XXXXX
      event_data:
        code: 'BR'
        message: '1'
    - platform: homeassistant
      event: start
      id: 'off'
    - platform: event
      event_type: event_template_reloaded
      id: 'off'
    binary_sensor:
    - name: Dørsensor Ajax ny
      device_class: door
      state: "{{ trigger.id == 'on' }}"

After a little thinking i went with having the adding two triggers to each Trigger-based Template Binary Sensor. More of a personal thing i guess, but i feel its less to follow up.
For me its just a few more lines in yaml, but cleaner and easier to track :slight_smile:

Also i dont have to maintain the automation on the side when i add or remove sensors.