Customize Binary Sensor Open/Closed Icons

Hi.

I currently have 12+ window sensors. Each one has its own binary_sensor with device class set to window. Because I find it fairly difficult to differentiate between the default mdi window icons in their open/closed state, I have created an additional template binary sensor for each one that sets the variant window icon instead - I find these are easier to differentiate even though they don’t look like my windows in real life (well neither do the other ones, anyway!)

This means I have 24+ sensors in total - 2 for each contact.

I came across the ability to customize the icons in the homeassistant section of configuration.yaml and I can get the following code to change one of the sensors.

homeassistant:
  customize:
    binary_sensor.bathroom_window_contact:
      friendly_name: "Bathroom Window"
      device_class: window
      icon: mdi-window-closed-variant

This will change the original sensor’s icon but it will not respond to open/close state changes - it always stays the same. I have tried adapting the icon to icon_template to include the logic that my additional template sensors have for managing the icon on state change - however this does not appear to work.

The documentation on customization only mentions icon as a customizable element so I’m assuming it cannot be done, unless anyone else knows differently?

I already have additional template sensors as a workaround. I am looking for a solution where I can just have one binary_sensor to represent my contact sensor - with icons of my choice. If it’s not possible I’ll submit a feature request (if one hasn’t been submitted already).

Thanks in advance.

2 Likes

You can only do that using a template binary sensor (which you already have)

Just add icon_template: into that binary sensor template and add your if statement there as per the docs.

Thanks @apmillen yes, like you say I’ve already got that solution in place but it means I’ve got an additional binary_sensor for each contact. I was hoping it could be fixed at the customize level, but it looks like it just hasn’t been implemented yet.

No, not yet. But feel free to enter a feature request :slight_smile:

I’ve created a feature request here in case anyone else lands on this page and wants to vote on it :grin:

I believe you should try this:

A long time ago, what you want to do was achievable using a custom component called Custom UI. However, by the time version 0.110 was released, it rendered Custom UI unusable and its author was no longer willing to adapt it. @Mariusthvdb took over and helped to convert it to be compatible with 0.110+. Not all of the functionality of the original Custom UI was retained (for various reasons) but it does let you control an entity’s icon and icon color.

Examples

1 Like

though easily changed using the custom-ui, I do spot an issue in the OP’s config. If you use the device_class, you shouldn’t set an icon additionally. The device_class takes care of the icon, though I haven’t tested the class window myself, I would suppose it to change according to state.

tada! Perfect, thanks @123 that’s just what I was looking for. I was using HACS but never found it in the repos until you sent me that link and then I was able to add it as a custom repo. Blimey did it take some restarting and cache emptying to fire it up, but works a treat now.

1 Like

Thanks @Mariusthvdb great work on the adaptation.
I did manage to get the icon changed and device class set to window at the same time using this:

homeassistant:
  customize_glob:
    binary_sensor.*_window_contact:
      templates:
        icon: >
          if (state == 'on') return 'mdi:window-open-variant';
          return 'mdi:window-closed-variant';
    binary_sensor.*_window_*_contact:
      templates:
        icon: >
          if (state == 'on') return 'mdi:window-open-variant';
          return 'mdi:window-closed-variant';
  customize:
    binary_sensor.bathroom_window_contact:
      friendly_name: "Bathroom Window"
      device_class: window
    binary_sensor.landing_window_rear_contact:
      friendly_name: "Landing Window"
      device_class: window
    binary_sensor.lounge_window_front_contact:
      friendly_name: "Lounge Front Window"
      device_class: window
    binary_sensor.lounge_window_rear_contact:
      friendly_name: "Lounge Rear Window"
      device_class: window
    binary_sensor.kitchen_window_left_contact:
      friendly_name: "Kitchen Left Window"
      device_class: window
    bbinary_sensor.kitchen_window_right_contact:
      friendly_name: "Kitchen Right Window"
      device_class: window

I only wanted to change the icons but looks like I’ve got loads more customizations at my fingertips now! :smiley: :+1:

change this into:

  customize_glob:
    binary_sensor.*window*contact:
      device_class: window

still, again, the device_class also sets an icon per state. Only use them both if you want to override that

also note that you will get some serious comments from the core HA dev’s, they don’t like the custom-ui.
Originally this was written for the per-Lovelace states interface. So be glad we can keep working what’s working for now, no promises made…

1 Like

Apologies for bumping this old thread, but I thought this workaround could be useful for future readers.

In my case, I have a binary_sensor that’s device_class: presence, but it’s for a car, so the default mdi:home/mdi:home-outline icons don’t quite fit. I was trying to do this without needing custom-ui (since this is the only thing I want to customize, and don’t want to worry about the add-in compatibility).

I was able to do it by defining a new template binary_sensor which mimics the real sensor’s value, but customizes the behaviour:

binary_sensor:
  - platform: template
    sensors:
      car_presence:
        unique_id: car_presence
        friendly_name: "Car presence"
        device_class: presence
        value_template: "{{ is_state('binary_sensor.raw_car_presence', 'on') }}"
        icon_template: >
          {{ iif(is_state('binary_sensor.car_presence', 'on'), 'mdi:car', 'mdi:car-outline') }}

Hope this helps!

edit: thanks @123 for the awesome suggestion on in-lining the icon_template

You can use an Immediate If.

        icon_template: "mdi:car{{ iif(is_state('binary_sensor.car_presence', 'off'), '-outline', '') }}"
1 Like

or make it even shorter without the iif:

icon_template: >
  mdi:car{{'-outline' if is_state('binary_sensor.car_presence','off')}}

‘regular’ (inline) templates dont require the ‘else’ that iif (ternary) does