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,
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,
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?)
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
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:
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.
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.
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”.
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 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 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.