Set the correct value on startup

Hi Folks,

I’ve searched a lot about this topic but I couldn’t find anything that suits me needs.
I created a template cover for my garage door.
My Shelly Plus One is attached to my Somfy Garage Motor with a sensor that tracks the open and close status of the garage door.

My template:

  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage-Test"
        unique_id: "GaragentorEichenweg-Test"
        value_template: "{{ states('sensor.garagentor_zustand') }}"
        open_cover:
          - condition: state
            entity_id: sensor.garagentor_zustand
            state: "Geschlossen"
          - service: switch.toggle
            target:
              entity_id: switch.garagentor
        close_cover:
          - condition: state
            entity_id: sensor.garagentor_zustand
            state: "Offen"
          - service: switch.toggle
            target:
              entity_id: switch.garagentor
        stop_cover:
          - condition: state
            entity_id: sensor.garagentor_zustand
            state: "Öffnet"
          - service: switch.toggle
            target:
              entity_id: switch.garagentor
        icon_template: >-
          {% if states('sensor.garagentor_zustand', 'off') %}
            mdi:garage-lock
          {% else %}
            mdi:garage-open
          {% endif %}

My sensor. garagentor_zustand is configured like this:

  - sensor:
      - name: "Garagentor-Zustand"
        unique_id: "Garagentorzustand"
        state: >
          {% if is_state('binary_sensor.garagentor_input', 'on') %}
          Offen
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (states.garagentor_input.last_changed < (now() - timedelta(seconds=29)))) %}
          Öffnet
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (switch.garagentor.last_pressed < (now() - timedelta(seconds=29)))) %} 
          Gestoppt
          {% else %}
          Geschlossen
          {% endif %}

The problem is, my state of my template cover is always open after a reboot.
I’ve read several posts about this but I cannot get my head around this…

This happens due to the garagentor_input returning unknown or unavailable during the startup. You can just make your template sensor to reflect the same…

something like this:

- sensor:
      - name: "Garagentor-Zustand"
        unique_id: "Garagentorzustand"
        state: >
          {% if states('binary_sensor.garagentor_input') in ['unknown','unavailable'] %}
            {{ states('binary_sensor.garagentor_input') }}
          {% elif states('switch.garagentor') in ['unknown','unavailable'] %}
            {{ states('switch.garagentor') }}
          {% elif is_state('binary_sensor.garagentor_input', 'on') %}
            Offen
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (states.garagentor_input.last_changed < (now() - timedelta(seconds=29)))) %}
            Öffnet
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (switch.garagentor.last_pressed < (now() - timedelta(seconds=29)))) %} 
            Gestoppt
          {% else %}
            Geschlossen
          {% endif %}

Hi Edward,
thanks for the tip - unfortunately it doesn’t work…

Got this error at boot:

Logger: homeassistant.config
Source: config.py:867
First occurred: 13:52:59 (1 occurrences)
Last logged: 13:52:59

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘sensor’][0][‘state’]. Got "{% if is_state(‘binary_sensor.garagentor_input’, [‘unknown’,‘unavailable’]) %} {{ states(‘binary_sensor.garagentor_input’) }} {% elif states(‘switch.garagentor’, [‘unknown’,‘unavailable’]) %} {{ states(‘switch.garagentor’) }} {% elif is_state(‘binary_sensor.garagentor_input’, ‘on’) %} Offen {% elif (is_state(‘binary_sensor.garagentor_input’, ‘on’) and (states.binary_sensor.garagentor_input.last_changed < (now() - timedelta(seconds=29))) %} Öffnet {% elif (is_state('binary_sensor.gara… (See /config/configuration.yaml, line 141).

My conf looks like this now:

  - sensor:
      - name: "Garagentor-Zustand"
        unique_id: "Garagentorzustand"
        state: >
          {% if is_state('binary_sensor.garagentor_input', ['unknown','unavailable']) %}
          {{ states('binary_sensor.garagentor_input') }}
          {% elif states('switch.garagentor', ['unknown','unavailable']) %}
          {{ states('switch.garagentor') }}        
          {% elif is_state('binary_sensor.garagentor_input', 'on') %}
          Offen
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (states.binary_sensor.garagentor_input.last_changed < (now() - timedelta(seconds=29))) %}
          Öffnet
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (states.switch.garagentor.last_pressed < (now() - timedelta(seconds=29)))) %} 
          Gestoppt
          {% else %}
          Geschlossen
          {% endif %}

This is not valid.

Which error you have if you use exactly the code I’ve shared to you?

Oh I thought this approach was correct as I read through the docu and found this formation…

I get this error when using your version:

Logger: homeassistant.config
Source: config.py:867
First occurred: 05:37:36 (1 occurrences)
Last logged: 05:37:36

Invalid config for [template]: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘sensor’][0][‘state’]. Got "{% if states(‘binary_sensor.garagentor_input’) in [‘unknown’,‘unavailable’] %}\n {{ states(‘binary_sensor.garagentor_input’) }}\n{% elif states(‘switch.garagentor’) in [‘unknown’,‘unavailable’] %}\n {{ states(‘switch.garagentor’) }} \n{% elif is_state(‘binary_sensor.garagentor_input’, ‘on’) %} Offen {% elif (is_state(‘binary_sensor.garagentor_input’, ‘on’) and (states.binary_sensor.garagentor_input.last_changed < (now() - timedelta(seconds=29))) %} Öffnet {% elif (is_state('binary_sen… (See /config/configuration.yaml, line 141).

Are you sure you are not missing something when copying? I’ve used that code as it is and didn’t got any error message in my logs.

By the way, I was looking in yours elif conditions and those for Öffnet and Gestoppt will never happens as they need the garagentor_input to be on and, when that is the case, it will always return Offen.

You can move the Offen to after Öffnet and Gestoppt, then it will make more sense.

I just recopied your code and made the adjustments for the if states but I still get the same error…weird

And what is the state of binary_sensor.garagentor_input?

I think I finally got what is the problem you have…

Try this (with the values in English and lowercase):

- sensor:
      - name: "Garagentor-Zustand"
        unique_id: "Garagentorzustand"
        state: >
          {% if states('binary_sensor.garagentor_input') in ['unknown','unavailable'] %}
            {{ states('binary_sensor.garagentor_input') }}
          {% elif states('switch.garagentor') in ['unknown','unavailable'] %}
            {{ states('switch.garagentor') }}
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (states.garagentor_input.last_changed < (now() - timedelta(seconds=29)))) %}
            opening
          {% elif (is_state('binary_sensor.garagentor_input', 'on') and (switch.garagentor.last_pressed < (now() - timedelta(seconds=29)))) %} 
            stopped
          {% elif is_state('binary_sensor.garagentor_input', 'on') %}
            open
          {% else %}
            closed
          {% endif %}
  • please fix my translations properly

States of standard entities are always in English. The translations are done in the front end.

You will have to change the tests in your cover template to English too.

I don’t understood how your cover works… You always use switch.toggle…
Do you have a different reaction to your switch turning on or off based on the garage door state?

If you really have to toggle all the time, you should be using if…then… for this, no?
Anyways, let’s focus ou find out the initial state first, as this is the issue you asked for help here.