Template cover for garage door

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?

The startup issue?

You have to add a trigger for startup like this to your status:

template:
  - trigger:
      - platform: homeassistant
        event: start

And then I put the other conditions that can cause a problem at startup into a condition for NOT the startup state.

Here’s my sensor and status:

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        unique_id: "Garage_Door_shellyUNI_170"
        value_template: '{{ states("sensor.garage_door_status") }}'
        open_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        close_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        stop_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_button
        icon_template: >-
          {% if is_state('sensor.garage_door_status', 'closed') %}
            mdi:garage
          {% elif is_state('sensor.garage_door_status', 'closing') %}
            mdi:garage-alert
          {% elif is_state('sensor.garage_door_status', 'opening') %}
            mdi:garage-alert
          {% elif is_state('sensor.garage_door_status', 'error') %}
            mdi:magnet-on
          {% else %}
            mdi:garage-open
          {% endif %}

template:
  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.garage_door_open
          - binary_sensor.garage_door_closed
          - input_text.garage_rotary_state
      - platform: homeassistant
        event: start
    sensor:
      - name: "Garage Door Status"
        unique_id: "Garage_Door_status_shellyUNI_170"
        state: >
          {% if is_state('binary_sensor.garage_door_closed', 'on') and is_state('binary_sensor.garage_door_open', 'on') %}
            error
          {% elif is_state('binary_sensor.garage_door_closed', 'on') %}
            closed
          {% elif is_state('binary_sensor.garage_door_open', 'on') %}
            open
          {% elif trigger.platform != 'homeassistant' %}
            {% if (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_open') %}
              closing
            {% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_closed') %}
              opening
            {% elif is_state('input_text.garage_rotary_state', 'opening') %}
              opening
            {% elif is_state('input_text.garage_rotary_state', 'closing') %}
              closing
            {% endif %}
          {% else %}
            open
          {% endif %}
1 Like

Hello, i want to create a cover in my garage

my garage can open using 3 switch relay OPEN,CLOSE,STOP.

relay entity in my homeassistant are switch.sonoff_garaga_open , switch.sonoff_garaga_close , switch.sonoff_garaga_stop.

how to use the cover using this 3 entities ?

this is my current set up

sorry im new to this.
thanks in advance

You can use the code I had posted just above and replace entity names specific to your setup for checking their states.

no, unfortunately not. template covers should show unavailable until state can be calculated in my opinion this is a bug

This was exactly what I needed for a single switch with a contact sensor on the door.

I am having a hard time wrapping my head around setting up a cover template for my simple set of garage doors. I have a single magnetic switch that indicates when the door is closed/open. I have a relay connected to a esp32. the relay needs to cycle for 2 seconds (about) to tell the door motor. What entries in this code do I change to my devices/switches/entities?

what do you mean ‘cycle for 2 seconds’? You mean it needs to be on for 2 seconds? Regardless of that, the posted solution should work accept you’d change the value_template to point to your magnetic sensor, not the switch.

Thanks petro
Yes the relay needs to go on for 2 seconds then back off. I get confused sometimes with the code. I will change the value_template to my magnetic sensor

Thanks again

To me it looks like I have the value_template set to my magnetic sensor.

cover:

  • platform: template
    covers:
    garage_door:
    device_class: garage
    friendly_name: “Honda Door”
    unique_id: “Honda Door”
    value_template: ‘{{ states(“binary_sensor.honda_door_contact_2”) }}’
    open_cover:
    - service: switch.turn_on
    target:
    entity_id: switch.honda_door_relay
    close_cover:
    - service: switch.turn_on
    target:
    entity_id: switch.honda_door_relay
    stop_cover:
    - service: switch.turn_on
    target:
    entity_id: switch.honda_door_relay
    icon_template: >-
    {% if is_state(‘binary_sensor.honda_door_contact_2’, ‘closed’) %}
    mdi:garage
    {% elif is_state(‘binary_sensor.honda_door_contact_2’, ‘closing’) %}
    mdi:garage-alert
    {% elif is_state(‘binary_sensor.honda_door_contact_2’, ‘opening’) %}
    mdi:garage-alert
    {% elif is_state(‘binary_sensor.honda_door_contact_2’, ‘error’) %}
    mdi:magnet-on
    {% else %}
    mdi:garage-open
    {% endif %}

template:

  • trigger:
    • platform: state
      entity_id:
      • binary_sensor.garage_door_open
      • binary_sensor.garage_door_closed
      • input_text.garage_rotary_state
    • platform: homeassistant
      event: start
      sensor:
    • name: “Garage Door Status”
      unique_id: “binary_sensor.honda_door_contact_2”
      state: >
      {% if is_state(‘binary_sensor.garage_door_closed’, ‘on’) and is_state(‘binary_sensor.garage_door_open’, ‘on’) %}
      error
      {% elif is_state(‘binary_sensor.garage_door_closed’, ‘on’) %}
      closed
      {% elif is_state(‘binary_sensor.garage_door_open’, ‘on’) %}
      open
      {% elif trigger.platform != ‘homeassistant’ %}
      {% if (trigger.to_state.state == ‘off’ and trigger.from_state.state == ‘on’ and trigger.entity_id == ‘binary_sensor.garage_door_open’) %}
      closing
      {% elif (trigger.to_state.state == ‘off’ and trigger.from_state.state == ‘on’ and trigger.entity_id == ‘binary_sensor.garage_door_closed’) %}
      opening
      {% elif is_state(‘input_text.garage_rotary_state’, ‘opening’) %}
      opening
      {% elif is_state(‘input_text.garage_rotary_state’, ‘closing’) %}
      closing
      {% endif %}
      {% else %}
      open
      {% endif %}

Is it possible to have different platform for sensor and switch in a cover template ? i.e. my sensor is rest based, but switch is mqtt. Here is my sensor

rest:
  - scan_interval: 60
    resource: http://X.X.X.X/garagedoorconfig/json
    binary_sensor:
      - name: "Left Garage Door Rest"
        unique_id: "left_garage_door_rest"
        value_template : "{{ value_json.GarageDoor[0].Value}}"

Above sensor works and I can see correct status (though scan interval is 60 seconds, I manually refresh and see correct status when calling any actions.)

Here is my mqtt switch

mqtt:
  switch:
    - unique_id: garage_left_mqtt
      name: "Left Garage MQTT"
      command_topic: "garage/cmd"
      payload_off: "event,toggleleft"
      payload_on: "event,toggleleft"
      state_topic: "garageLeft/IsOpen"
      state_off: 0
      state_on: 1
      optimistic: true

I confirmed that I can open and close garage door using above mqtt switch too.
However when I try to incorporate both in a cover then I can see status but opening/closing doesn’t work

- platform : template
    covers :
      left_garage_door_cover :
        unique_id : "left_garage_door_cover"
        device_class : garage
        friendly_name : "Garage Door Left"
        value_template : >-
          {% if is_state('binary_sensor.left_garage_door_rest','on') %}
            Open
          {% else %}
            Closed
          {% endif %}
        open_cover :
          service : switch.turn_on
          target:
            entity_id : switch.garage_left_mqtt
        close_cover :
          service : switch.turn_off
          target:
            entity_id : switch.garage_left_mqtt

what am I doing wrong ?

I’m using it differently,
my setup is based on 2 different Shelly

  • First Shelly 1 is for the magnet to receive gate status
  • Second Shelly 1 is to control the gate (dry contact).

Here is my custom cover if it helps someone:

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        value_template: "{{ 'open' if (is_state('switch.parking_gate_sensor', 'off')) and ((now() > states.switch.parking_gate_sensor.last_changed + timedelta(seconds=5))) else 'closed' }}"
        open_cover:
          service: switch.turn_on
          target:
            entity_id: switch.parking_gate_switch
        close_cover:
          service: switch.turn_on
          target:
            entity_id: switch.parking_gate_switch

I’m connecting Uni to my “dumb” gate right now (one button open/close/stop). I have magnetic sensors attached, so I know if the gate is opened or closed. I’m also building a circuit to read ADC input using a rotary potentiometer.
How do you read the value of ADC and how are you changing it to text?
In my case, I’ll have a value in the range of 2-20V.
Sadly the value “jumps” (see the image below), so I’m not sure if I’ll be able to calculate the position using it.

How do you solve the issue with actions? In my case, if I press the button to open the gate, stop it, and want to open it more, then I must press once (close), do a second press (stop) and the third press will open the gate. So the action is dependent on the previous state and I must wait at least a second between button presses.

Template sensor

Test with a multimeter to see if the issue is in the pot output or the ADC on the Uni. Replacing the pot with something else might be the solution.

You don’t - and can’t. You’d have to make your own controller for the gate - if all you can do is activate one button on the original controller, you’ll have to live with the limitations you’ve seen.

Hello all,

I want to make a template for my garage door, but i have the following hardware:

  • Tasmota working with a pulse to open or close
  • red switch for open confirmation (ON door is 100% open, OFF door is open but >100%)
  • red switch for closed confirmation (ON door is 100% closed, OFF door is open)

How can i implement this in a single cover?

Thanks

I finally started working on refining my garage door setup and move it from a Homekit stop gap to HA and found this thread. This is the first time I work with template switches or covers and I am also new to Jinja2 in general. I currently use a Zigbee wall light switch to control a relay and I have a Fibaro Homekit door sensor that is now directly connected to HA as a sensor.

The basics:

Challenge 1:
HA only knows my switch as a light switch as it comes in via the Hue integration. I need to turn it into a normal switch (or cover?). I like to handle this within the same (cover ?) template.

Challenge 2:
It needs to turn on for 2 secs for both open and close operations. If it stays on, it ca’t trigger the alternative action. I have no idea how to incorporate this 2 S wait followed by turning. off into the same operation.

Nice-to-haves:

Challenge 3:
As the sensor will only know the closed state when the door movement has completed, it would be great to have a state of ‘moving’ that begins with the switch pulse for closing and ends with the sensor reporting closed. When opening, the move state could be a fixed time after the open command.

Challenge 4:
Ideally, I like to have the option to also stop the door. This would be just another 2sec pulse when the door is ‘moving’.

I hope you can help me. All I managed so far is this and the switch is unavailable:

  - platform: template
    switches:
      garage_door:
        value_template: "{{is-state('binary_sensor.fibaro_door_window_sensor_contact_sensor', 'on') }}"
        turn_on:
          service: light.turn_on
          target:
            entity_id: light.on_off_light_1_2
        turn_off:
          service: light.turn_off
          target:
            entity_id: light.on_off_light_1_2