Template Cover with Open AND Closed Sensor

Hi Guys,

I’m wanting a cover with confirmation for both open AND closed states rather than closed/not closed with a single binary_sensor.

I have two binary_sensors. One each to represent the open and closed positions.
What I’m not sure of is how to integrate this into ESPhome.

With the below yaml everything is fine while one of the two sensors is on.
When the door is changing state and neither of the sensors is made the cover state flickers from open to closed until one of the sensors is made.

cover:
  - platform: template
    name: "Left Shed Garage Door"
    id: shed_Left_cover
    lambda: |-
      if (id(garage_door_left_open).state) {
        return COVER_OPEN;
      }
      if (id(garage_door_left_closed).state){
        return COVER_CLOSED;
      }
      
    open_action:
      - switch.turn_on: garage_door_left_switch
      - delay: 1s
      - switch.turn_off: garage_door_left_switch
    close_action:
      - switch.turn_on: garage_door_left_switch
      - delay: 1s
      - switch.turn_off: garage_door_left_switch
    stop_action:
      - switch.turn_on: garage_door_left_switch
      - delay: 1s
      - switch.turn_off: garage_door_left_switch
    optimistic: false
    device_class: garage

So I think I need three states ? Open, Closed, and Opening/Closing ?

If this is the case, how do I represent this in YAML or lambda ?

TIA

More of a work around than a solution, but I ended up creating a template sensor in ESPHome which output 0/50/100 depending on the state of two binary sensors.
The template sensor is then used in home assistant in a cover template position_template allowing me to see if the door is neither fully closed nor fully open.

Can you share your solution for this ?

I have tapped into the existing upper and lower limit switches on my garage door so also have both states.

I am using a text sensor in esphome based on this project: https://smartsolutions4home.com/ss4h-go-smart-garage-door-opener/ to do something similar but can’t get the open/closed icons to work.

Gaz

I lost an hour to this today before discovering that nominating a device_class meant that my icon_template was being ignored :frowning_face:

Sensors from ESPhome:

binary_sensor:
  - platform: gpio
    pin: GPIO32
    name: "garage_door_left_open"
    internal: true
    id: garage_door_left_open
    filters:
      - delayed_on: 1000ms
      - delayed_off: 1000ms

  - platform: gpio
    pin: GPIO33
    name: "garage_door_left_closed"
    internal: true
    id: garage_door_left_closed
    filters:
      - delayed_on: 1000ms
      - delayed_off: 1000ms

sensor:
  - platform: template
    name: "Shed Left Garage Door Position"
    lambda: |-
      if (id(garage_door_left_closed).state) {
        return 0.0;
      } else if (id(garage_door_left_open).state){
        return 100.0;
      } else {
        return 50.0;
      }
    update_interval: 1s

Cover yaml in Home Assistant:

cover:
  - platform: template
    covers:
      shed_left:
#        device_class: garage
        friendly_name: "Left Shed Roller Door"
        position_template: "{{states('sensor.shed_left_garage_door_position')}}"
        open_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_left
          - delay: 1
          - service: switch.turn_off
            target:
              entity_id: switch.garage_door_left
        close_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_left
          - delay: 1
          - service: switch.turn_off
            target:
              entity_id: switch.garage_door_left
        stop_cover:
          - service: switch.turn_on
            target:
              entity_id: switch.garage_door_left
          - delay: 1
          - service: switch.turn_off
            target:
              entity_id: switch.garage_door_left
        icon_template: >-
          {% if states('sensor.shed_left_garage_door_position')|int > 50 %}
            mdi:garage-open
          {% elif states('sensor.shed_left_garage_door_position')|int < 50 %}
            mdi:garage
          {% else %}
            mdi:garage-alert
          {% endif %}
        availability_template: "{{states('binary_sensor.esp_shed_status')}}"

The above shows cover states of open, closed and an in between state represented by the garage-alert icon.

Just what I wanted, unfortunately just not entirely in ESPhome as I was hoping. :man_shrugging:

1 Like