Change Icon with State Change

you need to use position topic, this is all covered in the mqtt docs and it’s covered pretty well.

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

after you do this, you probably won’t need an icon template because it will handle the icon for you.

@Mutt Icon_templates are only available on specific integrations and that list is extremely limited. Personally, I don’t offer this as a solution anymore because of those reasons.

1 Like

I’m afraid I have to re-open this :frowning:

I was also looking into changing the icon for a few of my door/window sensors. I really thought I understood how this works by reading this topic through, plus the “Template Binary Sensor” page and partly the big “Templating” page.

I started with just overwriting one sensors icon based in it’s state, but I keep getting a new entity with the name of my template binary sensor. And this new entity works and changes its icon based on the actual sensors state… But I can’t get it to work, that the actual sensor gets its icon changed in the frontend:


That’s my template:

binary_sensor:
  - platform: template
    sensors:
      living_room_door_on_off_icon:
        entity_id:
          - binary_sensor.living_room_door_on_off
        # Note that 'value_template' is required. I have to add it, but I only want to change the icon...
        value_template: "{{ is_state('binary_sensor.living_room_door_on_off','on') }}"
        icon_template: >-
          {% if is_state("binary_sensor.living_room_door_on_off", "on") %}
            mdi:door-open
          {% else %}
            mdi:door-closed
          {% endif %}

I’ve also tried to name the template binary sensor exactly like the actual sensor, but that gives me the same result, just that it’s named living_room_door_on_off_2

Did I still understood that wrong? Will this never change the actual sensors icon in the frontend by itself, but just gives me another entity that I could use if I would do custom dashboards or something?
Or doesn’t it work, because my sensor is actually not added through the “binary_sensor” integration, but ZHA!?

Thanks for the help!
Greetings,

Andy!

You already have a binary sensor with on/off states. Delete your binary_sensor template you have there, and instead use the customize config to change the device class to door… like this in your configuration.yaml:

  customize:
    binary_sensor.living_room_door_on_off:
      device_class: door

Much simpler… now restart HA and should be done. The door class will make use of those neat opened and closed door mdi’s (note not all lovelace cards display icon changes… verify the icons are working right using glances or dev/states). Here’s more binary_sensor classes that you may want to be familiar with:

On a side note to help clear some confusion… yes it is likely the ZHA integration went ahead and added in all the necessary entities for you when you set it up. Hence why you magically had the binary_sensor in your dashboard. It does that by default (see the zha integration switch for “adding newly discovered entities”… it’s on by default). So with the above customize: config, you are just telling HA that it should refer to that sensor as a door rather than a generic binary_sensor with an ugly/boring square. :wink:

1 Like

Thank you so much! That’s the solution for my problem :smiley: I didn’t understand yet, that the device class is “just” used for representation in the frontend. I thought it has some more, deeper usage and I didn’t want to play around with the device class of an entity, not wanting to risk to break something :wink:

But still, just so I know / understand the templating thing I did there:
Is the outcome of what I did there (the 2nd binary sensor) the expected outcome? Or is that just here in my setup where the entity I tried to change is not actually a raw binary sensor entity?

Thanks again!

Yes a template sensor creates a new separate sensor.

Interesting… To be fair, according to what the config looks like, it totally makes sense, as it configures a binary sensor with the platform “template”… Still interesting concept, that you can’t affect the sensor itself but need to use this template sensor instead if you want your icona… So really only helpful if you have custom UI/dashboards, I guess. Tbf, probably 90% of all HA users have tbem, but I am not even two weeks into HA, so I still stick with the default UI and dashboard :wink:

Hi Petro,

why thisnot working (template):

binary_sensor:

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

lovelace:

          - type: 'custom:button-card'
            template: ben
            name: Bedroom
            style:
              top: 20%
              left: 75%
              width: 10%
            entity: light.lowboard
            icon: 'mdi:lightbulb-group'
            tap_action:
              navigation_path: /lovelace/bedroom
            hold_action:
              service: light.turn_on
              service_data:
                entity_id: light.lowboard
            custom_fields:
              temp:
                card:
                  type: 'custom:button-card'
                  template: subsection_temp
                  entity: sensor.temperature_10
              window:
                card:
                  type: 'custom:button-card'
                  template: subsection 
                  entity: binary_sensor.openclose_19

Shows a wrong icon:

grafik

can you help me pls?

Sorry, I use a completely different method than that. You’re better off asking in the custom button card thread.

Thank you, I will do that

Thank you.
device_class works perfectly!

Could anybody please help?
I am trying to do this, but can’t get it to work, included the following in configuration.yaml

customize:
  binary_sensor.open_close_sensor_27_79_46:
    device_class: door

I am getting this error
Integration error: customize - Integration ‘customize’ not found.

Home Assistant 2022.9.1
Supervisor 2022.08.6
Operating System 8.5
Frontend 20220907.0 - latest

It must be somewhere under the homeassistant: section.

homeassistant:
  ...
  customize:
    binary_sensor.open_close_sensor_27_79_46:
      device_class: door

Thanks! I got it to where it isn’t causing a configuration error. My original purpose was to get my door icons to show open icon and closed icon. That didn’t work they are still the same so I will continue to research.

Setting the device class only gives a static icon. To have a dynamic icon, define a template cover.

the whole idea of device_class is to show state dependent icons… Binary sensor - Home Assistant

why would you say:

?

Because I unintentionally mixed two things: the highlighting of an icon (something that wasn’t consistently working in all places before) vs changing the icon itself to indicate the state. How about you explain @waank1’s issue then instead of only so smartly pointing out my error.

Welll, since your suggested customize config is correct ofc, well have to wait for the further ’research’

I am interested in the integration creating these binary sensors. Maybe they already have a device_class set? Or some other customization going on.

We need a screen of these sensors in the dev states to be able to say anything further.

Highlighting can be done in the card, by using
state_color: true see Entities Card - Home Assistant and the options listed there

Ofc, we now also can do this in the entity in the Ui:

Show as

[quote=“parautenbach, post:32, topic:130279”]

customize:
    binary_sensor.open_close_sensor_25_be_7e:
      device_class: door

Newest development: I have these lines in my Config.yaml No errors, I have tried it with several entities but the icon doesn’t change. Somehow the device class has no effect whether I do in in yaml or if I do it in the entity in the UI.

customize: needs to be under homeassistant:

homeassistant:
  customize:

can you confirm to have that in configuration.yaml like that?

also, please answer the other questions for us to be able to help you at all. We need to understand which integration creates this binary sensor. Is it zwave? is it a template binary etc etc.

and also a sceenshotof the sensor in dev tools states


This is what I have in config.yaml

homeassistant:
  internal_url: https://homeassistant.local:8123
  name: Home
    elevation: 1152
  unit_system: imperial
  time_zone: America/Chicago
  customize:
    binary_sensor.open_close_sensor_25_be_7e:
      device_class: door