Template cover for garage door

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

Update: I tried to ask ChatGPT for the code and even though it suggested a template switch at first that wasn’t as useful as a template cover, it provided me with 90% of what I needed to get 1., 2. and 4. done by using scripts to incorporate the delay.

For those with a similar need:

configuration.yaml:

cover:
  - platform: template
    covers:
      cover_garage_door:
        device_class: garage
        friendly_name: Garage Door
        value_template: "{{ is_state('binary_sensor.fibaro_door_window_sensor_contact_sensor', 'on') }}"
        open_cover:
          service: script.garage_door
        close_cover:
          service: script.garage_door
        stop_cover:
          service: script.garage_door

script.yaml:

garage_door:
  alias: Garage Door
  sequence:
    - service: light.turn_on
      entity_id: light.on_off_light_1_2
    - delay:
        seconds: 2
    - service: light.turn_off
      entity_id: light.on_off_light_1_2
  mode: single
  icon: mdi:garage

I feel your pain @holger
I use raspberry pi for HA and garage door, so code will differ.
A mqtt garage door switch command topic uses set_on_ms instead of set. The payload_on can then be set to time in milliseconds.
Two position sensors (reed switch) let me me know open or closed.
I use the cover to get state from the template and perform an action.
The template defines the door position at any given moment.

Switch and sensors:

mqtt:
  switch:
    - unique_id: "garage_door"
      name: "Garage Door"
      state_topic: "home/garage/output/switch23"
      state_on: "ON"
      state_off: "OFF"
      command_topic: "home/garage/output/switch23/set_on_ms"
      availability:
        - topic: "home/garage/status"
          payload_available: "online"
          payload_not_available: "offline"
      payload_on: 1000
      payload_off: "OFF"
      optimistic: false
      icon: "mdi:garage"
  binary_sensor:
# Garage Door Position Sensors --------------------------------------
    - unique_id: "close_sensor"
      name: "Close Sensor"
      state_topic: "home/garage/input/sensor24"
      availability:
        - topic: "home/garage/status"
          payload_available: "online"
          payload_not_available: "offline"
      icon: "mdi:garage"
# -------------------------------------------------------------------
    - unique_id: "open_sensor"
      name: "Open Sensor"
      state_topic: "home/garage/input/sensor25"
      availability:
        - topic: "home/garage/status"
          payload_available: "online"
          payload_not_available: "offline"
      icon: "mdi:garage-open"
# End Garage Door Position Sensors ----------------------------------

Cover and Template

cover:
# Garage Door Cover -------------------------------------------------
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door"
        unique_id: "garage_cover"
        availability_template: "{{ is_state('switch.gagage_door') }}"
        value_template: "{{ is_state('sensor.garage_door_position', 'OPEN') }}"
        open_cover:
          - condition: state
            entity_id: binary_sensor.close_sensor
            state: "off"
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door
        close_cover:
          - condition: state
            entity_id: binary_sensor.close_sensor
            state: "on"
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door
        stop_cover:
          - condition: state
            entity_id: sensor.garage_door_position
            state: "moving"
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door
        icon_template: >-
          {% if is_state('sensor.garage_door_position', 'OPEN') %}
            mdi:garage-open
          {% elif is_state('sensor.garage_door_position', 'MOVING') %}
            mdi:pan-vertical
          {% elif is_state('sensor.garage_door_position', 'ERROR') %}
            mdi:garage-alert
          {% else %}
            mdi:garage
          {% endif %}
# End Garage Door Cover ---------------------------------------------
template:
# Garage Door Template ----------------------------------------------
  - sensor:
    - name: "Garage Door Position"
      unique_id: "sensor.garage_door_position"
      state: >
        {% set open_on = is_state('binary_sensor.open_sensor', 'on') %}
        {% set close_on = is_state('binary_sensor.close_sensor', 'on') %}
        {% if not open_on and close_on %}
          OPEN
        {% elif open_on and not close_on %}
          CLOSED
        {% elif open_on and close_on %}
          MOVING
        {% else %}
          ERROR
        {% endif %}
# End Garage Door Template ------------------------------------------

Hope it helps