Template cover for garage door

You can’t do it with that ui item. You’ll have to use something that accepts tap_actions

1 Like

Thank you!:slight_smile:

Hi! I had the same issue. Have simple door sensors on my garage and gate, and open/close it via simple RF433 command from Sonoff RF Bridge (so i have 2 scripts - for gate, and for garage).
Try similar to this:

- platform: template
  covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage"
        value_template: "{{ is_state('binary_sensor.garage_contact', 'on') }}"
        open_cover:
          service: script.1621708691102
        close_cover:
          service: script.1621708691102
        stop_cover:
          service: script.1621708691102
      front_gate:
        device_class: gate
        friendly_name: "Gate"
        value_template: "{{ is_state('binary_sensor.gate_contact', 'on') }}"
        open_cover:
          service: script.1621683565039
        close_cover:
          service: script.1621683565039
        stop_cover:
          service: script.1621683565039     
1 Like

My config for garage door with 1 magnetic sensor, work great:

cover:
  - platform: template
    covers:
    # секцию ниже не трогать. Рабочий прототип гаражных ворот
      garage_door:
        device_class: garage
        friendly_name: "гараж"
        unique_id: garagedoor
        value_template: >
          {% if is_state('binary_sensor.vorota_zakryty_contact', 'off') %}
            false
          {% else %}
            true
          {% endif %}
        open_cover:
          - condition: state
            entity_id: binary_sensor.vorota_zakryty_contact
            state: "off"
          - service: switch.turn_on
            target:
              entity_id: switch.gate_remote
        close_cover:
          - condition: state
            entity_id: binary_sensor.vorota_zakryty_contact
            state: "on"
          - service: switch.turn_on
            target:
              entity_id: switch.gate_remote
        stop_cover:
          - service: switch.turn_on
            target:
                entity_id: switch.gate_remote

But i have more complicated configuration. I am using 2 magnetic sensors for garage door. Sensor1 for “closed” state (off if gate is closed) and sensor2 for “open” state (off if gate is opened)
Based on real sensors i create templates:

template:
  - sensor:
      - name: vorota_opening
        state: "on"
      - name: vorota_closing
        state: "on"
      - name: vorota_open
        state: "off"
      - name: vorota_closed
        state: "on"

And appdaemon script:

import appdaemon.plugins.hass.hassapi as hass 

##############################################
### ПОМЕНЯТЬ ПЕРЕМЕННЫЕ ТУТ
##############################################
# имя датчика открытых ворот OpenSensor
open_sensor = "binary_sensor.0x00124b0022fef19f_contact" 
# имя датчика закрытых ворот Clopen_sensoredSensor
closed_sensor = "binary_sensor.vorota_zakryty_contact"

class garage_door_status(hass.Hass):
    def initialize(self):
        handle = None
        # слушем изменение датчиков открытых и закрытых ворот и если оно есть, запускаем функцию garage_door_status
        self.listen_state(self.garage_door_status, open_sensor, attribute="all")
        self.listen_state(self.garage_door_status, closed_sensor, attribute="all")
        # debug only
        self.listen_state(self.garage_door_status, 'cover.test_garage_door', attribute="all")

    def garage_door_status(self, entity, attribute, old, new, kwargs):
        # пишем в лог для траблшутинга, позже можно убрать
        self.log(f'===.input entity={entity}, new state is {new["state"]}. old state is {old["state"]}')
        # делаем проверку, изменился ли статус датчика
        if new["state"] != old["state"]:
            if entity == closed_sensor:
                if old["state"] == "off" and new["state"] == "on":
                    self.log("info 01. set gate to OPENING state")
                    self.set_state('sensor.vorota_closed', state="on")
                    self.set_state('sensor.vorota_closing', state="on")
                    self.set_state('sensor.vorota_open', state="on")
                    self.set_state('sensor.vorota_opening', state="off")
                elif old["state"] == "on" and new["state"] == "off":
                    self.log("info 02. set gate to CLOSED state")
                    self.set_state('sensor.vorota_open', state="on")
                    self.set_state('sensor.vorota_closing', state="on")
                    self.set_state('sensor.vorota_opening', state="on")
                    self.set_state('sensor.vorota_closed', state="off")
            elif entity == open_sensor:
                if old["state"] == "off" and new["state"] == "on":
                    self.log("info 03. set gate to CLOSING state")
                    self.set_state('sensor.vorota_open', state="on")
                    self.set_state('sensor.vorota_opening', state="on")
                    self.set_state('sensor.vorota_closed', state="on")
                    self.set_state('sensor.vorota_closing', state="off")
                elif old["state"] == "on" and new["state"] == "off":
                    self.log("info 04. set gate to OPEN state")
                    self.set_state('sensor.vorota_opening', state="on")
                    self.set_state('sensor.vorota_closed', state="on")
                    self.set_state('sensor.vorota_closing', state="on")
                    self.set_state('sensor.vorota_open', state="off")

So My logic is:

  1. when sensor1 old state is “off” and new state is “on” - then i assume that gate is “opening” (sensor.vorota_opening’, ‘off’)
  2. when sensor2 old state is “off” and new state is “on” - then gate is “open” (sensor.vorota_open’, ‘off’)
  3. when sensor2 old state is “on” and new state is “off” - then gate is “closing” (sensor.vorota_closing’, ‘off’)
  4. when sensor1 old state is “off” and new state is “on” - then gate is “closed” (sensor.vorota_closed’, ‘off’)

On paper it should work fine, but i have strange behaivor in log:
Screenshot_20210919_115019
When sensor.vorota_closed change it state from “off” (means gate is closed) to “on” means (gate is opening) - gate state is changed to “open” and in moment later “opening”

Is some one can explain to me what is my mistake?

Something might be missing where is the code that actually converts the template code into the 4 sensor states?

why are you using 4 sensors to define the state instead of a single sensor with 4 states? You’re over complicating it. Secondly, if you switch to a single sensor, you won’t need appdaemon to handle anything. A single template sensor can do the job. Lastly, you can use the previous state of the single sensor to determine what the new state will be. I.E. if the last state was closed and both sensors are off, then you’re opening. If the last state was open and both states are off, then you’re closing.

template:
- sensor:
  - name: Vorota state
    state: >
      {% set open_on = is_state('binary_sensor.0x00124b0022fef19f_contact','on') %}
      {% set close_on = is_state('binary_sensor.vorota_zakryty_contact','on') %}
      {% if open_on and not close_on %}
        open
      {% elif not open_on and close_on %}
        closed
      {% elif open_on and close_on %}
        sensor error
      {% else %}
        {% if is_state('sensor.vorota_state', 'open') %}
          closing
        {% else %}
          opening
        {% endif %}
      {% endif %}
4 Likes

What do you mean? AppDaemon code do all job

Thanks for your thoughts. I cant realy remember why i use 4 sensors instead 1. Your code have some syntax problems, i’ll try this later, but from now i cant figure out how system knows last state of sensors (i need to read more docutentations)

That code will work out of the box. Just copy paste into your config.

that pulls the information from the state machine, which will always be the last value.

{% if is_state('sensor.vorota_state', 'open' %}
          closing

) - In the end is missing
And after reloading template I have got strange state:

copy the current version, it had 2 ifs that should have been else-ifs

Resul

if you’re familiar with python you should be able to spot the error. If not, I updated it again.

thanks. my python skills is very low. your code work without errors, but little bit wrong. i think i should change state on to off - because when sensor off, it means it ok. for example if closed sensor is off, then gate is closed

I went off how magnetic sensors should be set up. Meaning if the magnet is in use, that means it would be on. Is this not the case for your binary sensor for the closed sensor? If not, you can invert the state with a not in front of it

sorry for late response.
I was trying to use your code, but section
{% if is_state(‘sensor.vorota_state’, ‘open’) %}
newer was true.
let me explain what i am doing (assume if the magnet is in use, that means it would be on (not in my case)):

  1. Initial state, gate is closed nothing happens
    binary_sensor.0x00124b0022fef19f_contact is off
    binary_sensor.vorota_zakryty_contact is on
    phisical gate is closed
    sensor.vorota_state - closed
    This state work well
  2. I am pressing gate button - gates goes to opening
    binary_sensor.0x00124b0022fef19f_contact is off
    binary_sensor.vorota_zakryty_contact is off
    phisical gate is opening
    sensor.vorota_state - opening
    This state work well
  3. After 15 or 20 seconds gates beecame
    binary_sensor.0x00124b0022fef19f_contact is on
    binary_sensor.vorota_zakryty_contact is off
    phisical gate is open
    sensor.vorota_state - open
    This state work well
    4.Then i am press gate button and gate goes to closing
    binary_sensor.0x00124b0022fef19f_contact is off
    binary_sensor.vorota_zakryty_contact is off
    phisical gate is closing
    sensor.vorota_state - opening
    This state not work well
  4. After 15 or 20 seconds gates is closed
    binary_sensor.0x00124b0022fef19f_contact is off
    binary_sensor.vorota_zakryty_contact is on
    phisical gate is closed
    sensor.vorota_state - closed
    This state work well
1 Like

I have a garage cover template issue related to the open state… whatever I do, the cover template defaults to state 'open' during Home Assistant startup generating bogus state records.

Sparing no code expense, I’ve expanded the standard implementation with the closing and opening template cover states based on specific timings. I’m using a zwave garage door tilt sensor and shelly relays hardwired into my garage opener.

However, one thing is really bugging me, the state of the cover always defaults to “open” when starting Home Assistant, regardless of the underlying sensor state which is restored.

Even with a unique id in the cover template, button cards based on the cover show the garage open in error during startup, the availability template appears to be ignored during start up also. Once Home Assistant is loaded, the cover state is changed from open to closed, and Home Assistant records a new ‘closed’ logbook entry without a corresponding ‘opened’ entry… This bogus change throws off my home automation state machine integrity

- platform: template
  covers:
    side_garage_door:
      unique_id: side_garage_door_controller
      device_class: garage
      friendly_name: "Side Garage door"
      value_template: >
        {% if is_state('binary_sensor.side_garage_door','on') %}
          {% if (now()|as_timestamp() - states['switch.side_garage_door'].last_updated|default(0,true)|as_timestamp()|default(32767,true) < 12) and (now()|as_timestamp() - states['binary_sensor.side_garage_door'].last_updated|default(0,true)|as_timestamp()|default(32767,true) >= 12) %}
            closing 
          {% else %}
            open
          {% endif %}
        {% else %}
          {% if (now()|as_timestamp() - states['switch.side_garage_door'].last_updated|default(0,true)|as_timestamp()|default(32767,true) < 12) and (now()|as_timestamp() - states['binary_sensor.side_garage_door'].last_updated|default(0,true)|as_timestamp()|default(32767,true) >= 12) %}
            opening
          {% else %}
            closed
          {% endif %}
        {% endif %}
      availability_template: "{{ states('binary_sensor.side_garage_door') | default('none') not in ['unavailable','unknown','none'] and states('switch.side_garage_door') | default('none') not in ['unavailable','unknown','none'] }}"
      open_cover:
        - condition: state
          entity_id: binary_sensor.side_garage_door
          state: "off"
        - service: switch.turn_on
          target:
            entity_id: switch.side_garage_door
        - service: browser_mod.toast
          data:
            duration: 13000
            message: Opening side garage door
      close_cover:
        - condition: state
          entity_id: binary_sensor.side_garage_door
          state: "on"
        - service: switch.turn_on
          target:
            entity_id: switch.side_garage_door
        - service: browser_mod.toast
          data:
            duration: 13000
            message: Closing side garage door
      stop_cover:
        - service: switch.turn_on
          target:
            entity_id: switch.side_garage_door

Any idea how to avoid home assistant creating an imaginary state during startup outside creating a custom integration?

Anyone else having this problem? I have several important timing based actions based on perimeter sensor last change time, and this small issue prevents me from using template covers in any of my time based automations

I just found this thread while looking to create a dual sensor setup myself. The fix for the statement there is to change

{% if is_state(‘sensor.vorota_state’, ‘open’) %}

to

{% if is_state(‘sensor.vorota_state’, ‘open’) or is_state(‘sensor.vorota_state’, ‘closing’) %}

What is happening otherwise is it sets to closing for 1 cycle, then immediately changes to opening the next time it evaluates. This change allows it to stay closing.

Did you ever figure out a fix for this?