Change Icon with State Change

homeassistant:
  customize:
    binary_sensor.garage_door_one:
      icon: mdi:garage
    binary_sensor.garage_door_two:
      icon: mdi:garage

I have been searching for a couple of weeks now and can’t seem to find the answer. It seems like it should be easy…and probably is, I just can’t find it. I want to use to different icons “garage-open” and “garage” and possibly “garage-alert” depending on the state. Any help to point me in the right direction would be appreciated.

Thanks

You want to use device classes

homeassistant:
  customize:
    binary_sensor.garage_door_one:
      device_class: garage_door
    binary_sensor.garage_door_two:
      device_class: garage_door

You won’t get to choose your icons, but they will change depending on the state.

If you want to choose your icon, there are many ways to do this. The easiest is probably a new template sensor with a icon_template.

binary_sensor:
  - platform: template
    sensors:
      garage_door_one_template:
        value_template: "{{ is_state('binary_sensor.garage_door_one','on') }}"
        icon_template: "mdi:garage{{ '-open' if is_state('binary_sensor.garage_door_one','on') else '' }}"

try the device class first, i believe it uses the garage icons that you want.

7 Likes

Thank you Petro! You’re right, the device_class does exactly what I was trying to do.

Rick

how to do when you don’t want to change the device_class?
my config in customize is:

binary_sensor:
  - platform: mqtt
    name: Tenda Studio
    state_topic: 'tapparella/binary_sensor/tenda_studio/state'

customize:
binary_sensor.tenda_studio_2:
  friendly_name: "Tenda Studio"
  icon: mdi:home

I would like to change the icon based to the state. It’s possible?
Thank’s!

You gotta make a template binary sensor and use icon_template.

Yes, Is possible?

Yes, read the links.

That’s all I wrote but it doesn’t work.


  - platform: mqtt
    name: Tenda Cucina
    state_topic: 'tapparella/binary_sensor/tenda_cucina/state'
    device_class: window
#    device_class: opening
    availability_topic: 'tapparella/status'
    icon_template: >-
      {% if is_state("binary_sensor.tenda_studio_2", "on") %}
        mdi:weather-sunset-up
      {% else %}
        mdi:weather-sunset-down
      {% endif %}

That’s not a template sensor. You just added icon_template to your existing sensor, which will not work.

You need to make a template sensor that pulls the information from your mqtt sensor and use the icon template there.

The examples in the help do that. Read the examples in the links.

sorry, I’m not very experienced. Can you help me by posting a sample code?

1 Like

Yes, let me copy exactly from the link that I keep sending you.

binary_sensor:
  - platform: template
    sensors:
      sun_up:
        entity_id:
          - sun.sun
        value_template: >-
          {{ is_state("sun.sun", "above_horizon") }}
        icon_template: >-
          {% if is_state("binary_sensor.sun_up", "on") %}
            mdi:weather-sunset-up
          {% else %}
            mdi:weather-sunset-down
          {% endif %}
4 Likes

quite remarkable really that the example uses the entity_id for updating the binary_sensor template. It doesn’t need it at all, since the entity to update is quite clear in the value_template…

This confuses readers of that documentation, thinking every template sensor would need those added entity_id’s.
On that same page is described when in fact we need to use the added entity_id.

btw note this is an example of an icon_template referencing itself in the template :wink:

I read the page given what you said (to align perspectives)
And though I agree that the entity id is not required (sun.sun is a tracked entity and obvious to the interpreter from the template) it positions the concept of entities that ‘may’ be referenced to update the template, so it’s not ‘wrong’
The self referencing in the icon template is also valid and I’ve done this myself, typically when the value template is ‘complex’. Why repeat a long template (possibly with a mistake in it) to evaluate when you can just reference it’s result ?
This also means you can copy a sensor and just change a minimal amount

of course you are right, I merely think it would have been a better educational insight to show the entity_id on a sensor that truly needs it. Most useful might have been a template using now(), which is done on most HA installations I would think.

As an example that wouldn’t need it, this would be the case…

Again, you’re right, and I have many sensors like that. It was an inside jest maybe to @petro with whom I had recently communicated on the essence of this, in my offline testers: see Why won't this template update in jinja (and work just fine in customize JS)

Yes, I see…
But that is a special case where you are iterating over a group???
I think by the time a beginner gets there, he may have the skills to do some more digging ???

Having said that, you had issues (as I would have done) so putting another example in the entity update would be sensible (with an explanation as to why)
And a caution note about the self referencing in the case of groups, would be of value.

You argue well, and your English, syntax and grammar is amazing.
Sumbit a pull request to update the docs as you suggest, I’m sure it will be approved :+1:

1 Like

Hello,

I would also like to change state of icon in front of Shutter.
Where do I have to place the if loop?
Should this be done at the Configuration.yaml of the cover or in the Lovelace UI entities?

Unfortunately the icon is not shown for first items, only placing fix icon e.g. for second item works!

entities:
  - entity: cover.rolladen_kuche
    icon: "if (state === '0') return 'mdi:inbox-arrow-down'; return 'mdi:inbox-arrow-up';"
  - entity: cover.rolladen_wohnzimmer_1
    icon: mdi:inbox-arrow-down

You have to put your template under icon_template: in an item that will accept icon_template:
(ie. not sure where you are putting that and I’m not sure that cover. will accept it (I don’t have covers))
Sometimes you have to create a sensor (template) to display the desired value AND the desired icon.

Also your template makes no sense. That is not permissible python.
you’d need something like : -

icon_template: "{{ 'mdi:television' if is_state('binary_sensor.tvfr', 'on') else 'mdi:television-off' }}"

Sorry for maybe basic question but I have configuration.yaml & the GUI entity.

Configurationy.yaml:

cover:
  - platform: mqtt
    name: "Rolladen Küche"
    command_topic: "/hap/100/80/set"
    state_topic: "/hap/100/80/status"

GUI:

entities:
  - entity: cover.rolladen_kuche
show_header_toggle: false
title: Rolläden
type: entities

So you mean I create a custom.yaml, that is referenced in configuration.yaml?

like

platform: template                                                                                                                                   
  covers:                                                                                                                                             
    cover.rolladen_kuche:
    icon_template: "{{ 'mdi:television' if is_state('cover.rolladen_kuche', 'on') else 'mdi:television-off' }}"

Hello,

Iam not fully understanding maybe also the Payload function.

Shutter is send the position from 0-100. Why do I get this error in logs?
The value 9 was position of shutter…

Payload is not supported (e.g. open, closed, opening, closing): 9

From what you’ve said, I would say you need advice from someone who a) has covers b) uses mqtt to talk to them
I also would suspect that your cover is returning an ‘unexpected’ value of 9. I have no idea what this means, you will have to read the documentation that comes with your device.