I am new to Home assistant and just trying a few things out.
I have magnet sensors on all my doors and windows. By default they have a square icon that changes based on their state.
I want to use another mdi icon and read about templates.
What am I doing wrong here? This is a test for just one sensor, but I would rather have it for all off the same device_Class “Opening”.
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Text to speech
tts:
- platform: google_translate
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
template:
- binary_sensor:
- name: Opening
state: >
{{ is_state("binary_sensor.lumi_lumi_sensor_magnet_aq2_821ef106_on_off", "on") }}
icon: >
{% if is_state("binary_sensor.lumi_lumi_sensor_magnet_aq2_821ef106_on_off", "on") %}
mdi:lock
{% else %}
mdi:lock-open-variant
{% endif %}
Could someone explain it to me like I was 5?
This seems to be such a basic task to do, yet I struggle to find any similar example and the docs are still a bit confusing for me.
Ran into this and use some of the code for my purpose (a door that can be open / closed or locked). Thank you! In my case a binary sensor does not work since that can only have 2 states, not 3, so I build a “normal” sensor that you can use in Lovelace. Code below, works for me.
@thompy93 perhaps you can try this. Please do make sure that you add the sensor under the correct part of the config. I see in your initial post that you start with “template”, instead of binary sensor: and than template (extra indent).
for example, my code below is in the “sensors” part of my config.
- platform: template
sensors:
voordeur:
friendly_name: "Voordeur"
#device_class: door # does not work since copy from bin sens.
value_template: >-
{% if is_state("binary_sensor.voordeur_contact", "on") %}
Open
{% elif is_state("binary_sensor.voordeur_slot", "off") %}
Locked
{% else %}
Closed
{% endif %}
icon_template: >-
{% if is_state("binary_sensor.voordeur_contact", "on") %}
mdi:door-open
{% elif is_state("binary_sensor.voordeur_slot", "off") %}
mdi:door-closed-lock
{% else %}
mdi:door-closed
{% endif %}
Thank you for your suggestion. By the looks of it that should work.
I wrote this post when I was just starting up with Home Assistant, and a little bit confused on how it all worked.
Thanks.
I am using exactly this code for icon color change.
But was looking for icon change (mdi:icon-name) with state change.
The default icons used by device_class are not always what I need.
Have been struggling with this for a while with a picture-elements card.
This added to my customize.yaml file will change to default icon from the device_class one, but haven’t found a way to change it by state.
This should work for you as well. You will get a new sensor which changes icon based on state. If you just want to show it in lovelace somewhere there are multiple ways, like the custom button card.
In this case, if the Xbox is off, then there’s an icon that turns it on. If the Xbox is on, then it turns the green. This stopped working after 2022.3.0.
I’m so lost in where all the configurations should go…
I got a similar situation: I got a night-hook on the front-door → when it’s unhooked the read-contact is closed (off) but Lovelace treat this by default as closed and when you tell lovelace it’s either a door or a lock becomes locked/closed when the contact is closed (off)…
I did a lot of reading and searching the internet, but I don’t understand how this all is fitted into lovelace… I mean if I put the example from @matthijsberg into the developer → template window (for my own sensor) it does seems to work on the template-output, but I am absolutely clueless how to add it to the dashboard -
Handling this (on is off, and off is on) in node-red to activate a light with a certain color is not a problem. Just rewiring the logic.
So where and how do I make this work so lovelace sees a closed binary_sensor as “on” (with appropriate color - become yellow by default when open but should be red - in my case - and pref be green when closed)
Lot of questions - srry that I hijacked/piggy-bagged the thread but I did not really find an answer.
So yeah - where in hassio should this be placed to use on the lovelace dashboard?
- platform: template
sensors:
frontdoor_hook: # <-- does this become a new "virtual" sensor ??
# binary_sensor.openclose_64 is the actual name of the sensor
friendly_name: "front-door hook"
value_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
hooked
{% else %}
unhooked
{% endif %}
icon_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
mdi:door-closed
{% else %}
mdi:door
{% endif %}
It doesn’t tie into lovelace. You’re making a new sensor (not a binary_sensor). binary_sensors can only be on or off, becuase you want to use hooked/unhooked… you’ll need to create a sensor which accepts any state. Then you’ll use that new sensor in the interface instead of the previous binary_sensor.
Changing colors in dashboards requires custom solutions. It’s not something that native home assistant can do.
I had to install the file editor plugin for that… While searching I found that I could add an include file like so which makes a lot more sense now:
binary_sensor: !include binary_sensor.yaml
Then I added this yaml into it - some trial and error got involved since I’m (for now) not entirely sure what I’m doing but I got it working. My /config/binary_sensor.yaml has this content now and does what I want (except for the color but that’s for an other day):
- platform: template
sensors:
frontdoor_hook:
device_class: opening
friendly_name: "front-door hook"
value_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
off
{% else %}
on
{% endif %}
icon_template: >-
{% if is_state("binary_sensor.openclose_64", "on") %}
mdi:lock-outline
{% else %}
mdi:lock-open-variant-outline
{% endif %}
And this is the card I added to lovelace where openclose_64 is the original sensor and frontdoor_hook the custom sensor: