Template Sensor Garage Door Dynamic Icon

I have being trying to customize the front end of my HA. Under the “Binary Sensor” card I have an Aqara open/close sensor that I will like to use to determine the state of the garage door in our residence. The key element that I want to achieve is to have a dynamic icon that changes state base on the condition of the door between open and close.


I have being able to successfully do a dynamic icon with some assistance from the community utilizing the “Configuration>Customization” menu by changing the “Device Class” of the binary sensor entity ID. But the icon that it displays is the “Garage_Door” which is what I considered a “Single Car Garage”. I will like to do the same thing with the “Garage-variant” icon which is the equivalent to a “Double Car Garage”. I was able to change the icon to the garage-variant by modifying the “Customize.yaml” file with the following code,

binary_sensor.openclose_6:
  icon: mdi:garage-variant
  device_class: garage_door

But after doing this, the icon lost the ability to be Dynamic and it didn’t change form when opening or closing the door. I have being told that I need to use a “Template Binary Sensor” to do what I want. After all the reading about “Templates” I was able to come up with this code,

binary_sensor:
  - platform: template
    sensors:
      openclose_6:
        value_template: >-
          {{ is_state("binary_sensor.openclose_6", "on") }}
        icon_template: >-
          {% if is_state("binary_sensor.openclose_6", "on") %}
            mdi:garage-open-variant
          {% else %}
            mdi:garage-variant
          {% endif %}

I was successful at adding this code my configuration.yaml file which appear to have created a new “Entity” like you see on the image below,

Now, I just don’t know what to do next in my pursuit to have a “Garage-Variant” icon changing from open to close base on the state of the Aqara openclose sensor I have.

In customize you change the default icon, not make it dynamic. I’ll have to look into your garage-variant mdi. Thanks.

Apparently the Aquara integration creates a binary_sensor. Fine.

I’ve never used the HA configuration editor, so I have no idea about how well separate code you put in configuration.yaml will play when you don’t use mode: yaml. (Have you considered changing to yaml mode? The code you wrote is spot on).

However, assuming you won’t break something in the editor by mixing yaml code with the non-yaml HA mode, just change open_close_6 in your binary_sensor code to something like ferrari_garage to give it an appropriate contextual name. You will then have a binary_sensor.ferrari_garage entity that you can use elsewhere that will have the appropriate icon based on on-off state. From here on, ignore openclose_6 and just use binary_sensor.ferrari_garage everywhere you reference the garage door.

Here’s a bit of code I use for my garage door that’s not directly applicable. But, its a state icon-display / open-close action button overlaid on a floorplan of my home. The icon is defined in the button itself, in this case. (Presumably, there’s a service provided with the integration to open/close the garage door?)

      - type: custom:button-card
        style:
          left: 90.5%
          top:  38.0%
        entity: binary_sensor.garage_door
        show_name: false
        show_state: true
        show_icon: true
        state:
          - value: 'on' # open
            icon: mdi:garage-open
            color: 'red'
          - value: 'off' # closed
            icon: mdi:garage
            color: 'green'
        hold_action:
          action:  call-service
          service: script.open_close_garage_door
        tap_action:
          action: more-info
        styles:
          card:
            - width: 4vw
            - height: 4vh
            - font-size: 1vw
            - background-color: rgba(135, 146, 175, 0.96)

I had to write my own open-close script since I have a relay wired in parallel with the hardwired wall mounted garage door opener to trigger the door motor itself. Actually, here’s the script (switch.garage_door_opener is the relay). The script behaves as if I pushed the garage door opener button on the wall for one second:

open_close_garage_door:
  alias: Open or Close the Garage Door
  sequence:
    - service: homeassistant.turn_on
      data:
        entity_id: switch.garage_door_opener
    - delay: '00:00:01'
    - service: homeassistant.turn_off
      data:
        entity_id: switch.garage_door_opener

I use the ‘button-card’ add on for this sort of thing.

Then in Lovelace you can have something like the following in your code:-

icon: |
  [[[ return states['cover.garage_door'].state == 'closed'
    ? 'mdi:garage-variant'
    : 'mdi:garage-open-variant';
  ]]]

In principle, you’re already on the right track, it’s just that things got a bit mixed up.

“openclose_6” is the name of your binary sensor. So when you create a template sensor which is also called “openclose_6” you have duplicated it. Therefore your template sensor appears as “binary_sensor.openclose_6 _2

Instead of this you can create a simple template sensor like this:

sensor:
  - platform: template
    sensors:
      garage:
        friendly_name: Garage
        value_template: >
          {% if is_state('binary_sensor.openclose_6', 'on') %}
             garage open
          {% else %}
             garage closed
          {% endif %}
        icon_template: >
          {% if is_state('binary_sensor.openclose_6', 'on') %}
             mdi:garage-open-variant
          {% else %}
             mdi:garage-variant
          {% endif %}

In your card you now use it as

sensor.garage

Thank you so much to everyone. The advice has clear the questions I had. I’m assuming that base on the new understanding because this card on my HA front end is a “Binary Sensor Card” that if I change the entity ID to anything other than a “binary_sensor”, it would explain the error I’m getting.

So, as others have mention, I’m going to have to use a custom card to be able to attached my new sensors.

You can also create it as binary sensor:


binary_sensor:
  - platform: template
    sensors:
      garage:
        friendly_name: Garage
        value_template: >
          {% if is_state('binary_sensor.openclose_6', 'on') %}
             garage open
          {% else %}
             garage closed
          {% endif %}
        icon_template: >
          {% if is_state('binary_sensor.openclose_6', 'on') %}
             mdi:garage-open-variant
          {% else %}
             mdi:garage-variant
          {% endif %}

gives you

binary_sensor.garage

Thank you again. With this change I should be able to modify the current card I have. When ever I do changes to the configuration.yaml like for editing this code. Do I need to every time restart HA by going to Server Management or can any of those sub-groups be use instead.

Found it! is called “Template Entities”. Notice that by changing to a “binary_sensor” rather than a “sensor”, the portion of the code to change the state no longer shows close or open.

Now I’m getting the following error,

Ah, sorry, forgot the device class:


binary_sensor:
  - platform: template
    sensors:
      garage:
        friendly_name: Garage
        device_class: opening
        value_template: "{{ is_state('binary_sensor.open lose_6', 'on') }}"
        icon_template: >
          {% if is_state('binary_sensor.open lose_6', 'on') %}
             mdi:garage-open-variant
          {% else %}
             mdi:garage-variant
          {% endif %}

You can but not the way demonstrated in your example. Unlike a sensor, whose state value can be almost anything, the only valid values for a binary_sensor are on and off. The example sets the states to either garage open or garage closed which are fine for a sensor but not a binary_sensor. The resulting Template Binary Sensor will have a permanent state of off.

The following version sets the required state values. The addition of device_class enures the UI displays the states of on and off as “Open” and “Closed”.


binary_sensor:
  - platform: template
    sensors:
      garage:
        friendly_name: Garage
        value_template: "{{ states('binary_sensor.openclose_6') }}"
        icon_template: "mdi:garage-{{ 'open-' if is_state('binary_sensor.openclose_6', 'on') else '' }}variant"

Screenshot from 2021-04-24 18-26-33

Screenshot from 2021-04-24 18-26-44

I had to use an “Entity Card” and added the “device_class” to the code and it started working. Is it possible to force the color of the icon to change base on the state of the sensor.

The code in #8 works, but your example is more polish :slightly_smiling_face:

The code works in the Template Editor but it fails to create a functional Template Binary Sensor for the reasons I have explained. In addition, I tested and confirmed the resulting binary_sensor remains in the off state and is unable to change to on.

Review the requirements for the value_template of a Template Binary Sensor. The template must evaluate to True for the state to be on.

value_template template REQUIRED
The sensor is on if the template evaluates as True and off otherwise. The actual appearance in the frontend (Open/Closed, Detected/Clear etc) depends on the sensor’s device_class value

The code works, but perhaps we misunderstand each other?

The following code never reports True/False. It reports garage open/garage closed. When used in a Template Binary Sensor, it is always interpreted as off.

          {% if is_state('binary_sensor.openclose_6', 'on') %}
             garage open
          {% else %}
             garage closed
          {% endif %}

When binary_sensor.openclose_6 is on the template reports garage open which is not the boolean state True so the Template Binary Sensor will continue to report off. If the device_class is garage_door, the UI will display “Closed” (“Geschlossen”), even when binary_sensor.openclose_6 is on.

Ok, we misunderstood. I talked about my code in #8.

1 Like

Well now, that clears everything up! I was talking about potatoes and you were talking about tomatoes! :slightly_frowning_face: :man_facepalming:

Sorry about that! Yes, this template, in your post #8, will indeed create a functional Template Binary Sensor:

value_template: "{{ is_state('binary_sensor.open lose_6', 'on') }}"

It’s the one in post #6 that won’t work.

Never mind. Yours is still more elegant. Thank you!