Icon template with ESPHome cover

I have just replaced the Konnected firmware with ESPHome for my garage door controllers. Using the simple garage door cookbook, I have it working with covers:

cover:
  - platform: template
    name: "Left Garage Door Controller"
    lambda: |-
      if (id(left_sensor).state) {
        return cover::COVER_OPEN;
      } else {
        return cover::COVER_CLOSED;
      }
    open_action:
      # Cancel any previous action
      - switch.turn_off: left_close_switch
      # Turn the OPEN switch on briefly
      - switch.turn_on: left_open_switch
      - delay: 0.5s
      - switch.turn_off: left_open_switch
    close_action:
      - switch.turn_off: left_open_switch
      - switch.turn_on: left_close_switch
      - delay: 0.5s
      - switch.turn_off: left_close_switch
    stop_action:
      - switch.turn_off: left_close_switch
      - switch.turn_off: left_open_switch
    optimistic: true
    assumed_state: true

This works well. Previously, I was using an icon template to change the default cover icon to an open/closed garage door icon:

icon_template: >-
  {% if is_state('binary_sensor.left_garage_door_status', 'on') %}
    mdi:garage-open
  {% else %}
    mdi:garage
  {% endif %}

Is there any way to use an icon template with a cover created in esphome?

1 Like

I would probably create a template cover in home assistant that copies everything but adds you icon template.

I would probably also add a feature request in ESPhome for this. It seems icon is listed as an option for sensor, but not for cover. It isn’t clear whether one can use a lamba for icon in the sensor either.

Thanks. I have changed things so that the cover defines in HA rather than ESPHome - now working. Creating the momentary switch was a pain - I did that in the end by creating the GPIO switch in ESPHome, then creating a second template switch that turns the first switch on, waits a delay, then turns the switch off.

Hi,
Would you be willing to share all the code for this? I’m going to start looking at controlling my doors with ESPHome.
Thanks,
Rob

Sure. My setup has two garage doors. Each door has a momentary button for open and a second one for close. I also have a simple wired open/close sensor at the bottom of each door. The ESP home code looks like:

esphome:
  name: garage_node
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: ###
  password: ###

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: ###

ota:
  password: ###

switch:
  - platform: gpio
    pin: D1
    id: left_open_switch
  - platform: gpio
    pin: D2
    id: left_close_switch
  - platform: gpio
    pin: D5
    id: right_open_switch
  - platform: gpio
    pin: D6
    id: right_close_switch
  - platform: template
    name: "Left Garage Door Open Switch"
    lambda: 'return false;'
    turn_on_action:
      - switch.turn_on: left_open_switch
      - delay: 0.5s
      - switch.turn_off: left_open_switch
  - platform: template
    name: "Left Garage Door Close Switch"
    lambda: 'return false;'
    turn_on_action:
      - switch.turn_on: left_close_switch
      - delay: 0.5s
      - switch.turn_off: left_close_switch
  - platform: template
    name: "Right Garage Door Open Switch"
    lambda: 'return false;'
    turn_on_action:
      - switch.turn_on: right_open_switch
      - delay: 0.5s
      - switch.turn_off: right_open_switch
  - platform: template
    name: "Right Garage Door Close Switch"
    lambda: 'return false;'
    turn_on_action:
      - switch.turn_on: right_close_switch
      - delay: 0.5s
      - switch.turn_off: right_close_switch

binary_sensor:
  - platform: gpio
    pin:
      number: TX
      mode: INPUT_PULLUP
    device_class: garage_door
    name: "Left Garage Door Sensor"
    id: left_sensor
  - platform: gpio
    pin:
      number: RX
      mode: INPUT_PULLUP
    device_class: garage_door
    name: "Right Garage Door Sensor"
    id: right_sensor

The GPIO switches are for internal use in ESPHome only. The template switches create the momentary action for each of the 4 buttons (2 open, 2 closed).

Then in Home Assistant I have the following code:

# Garage door covers

cover:
  - platform: template
    covers:
      left_garage_door:
        friendly_name: "Left Garage Door Controller"
        value_template: "{{ is_state('binary_sensor.left_garage_door_sensor', 'on') }}"
        open_cover:
          - service: switch.turn_on
            data:
              entity_id: switch.left_garage_door_open_switch
        close_cover:
          - service: switch.turn_on
            data:
              entity_id: switch.left_garage_door_close_switch
        icon_template: >-
         {% if is_state('binary_sensor.left_garage_door_sensor', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}
      right_garage_door:
        friendly_name: "Right Garage Door Controller"
        value_template: "{{ is_state('binary_sensor.right_garage_door_sensor', 'on') }}"
        open_cover:
          - service: switch.turn_on
            data:
              entity_id: switch.right_garage_door_open_switch
        close_cover:
          - service: switch.turn_on
            data:
              entity_id: switch.right_garage_door_close_switch
        icon_template: >-
          {% if is_state('binary_sensor.right_garage_door_sensor', 'on') %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}

The icon_template changes the icon between open and closed for the cover.

2 Likes

Thanks a lot, I’ll give it a try this weekend.

or override the icon in esphome integration using mdi:garage

Dynamic icon is also possible, in this example with a text sensor (can be applied to other template sensors and automations as well:

text_sensor:
  - platform: template
    name: Status sensor
    id: status
    lambda: |- 
          if (id(some_sensor).state) {
            id(status).set_icon("mdi:timer-sand");
            return {"Waiting"};
          }

The function id(status).set_icon("mdi:timer-sand"); sets the icon here. You can apply as many icon rules as you want.

3 Likes

Unfortunately in my tests I could use set_icon() only once. Any subsequent calls were silently ignored.
I could also not find any official documentation about this method. Maybe the behavior has been changed?