Another 'Icon color change based on state' question

I have been trying for some time to figure out how I can make an icon color change based on the state. I see that there are a lot of topics about this, but none of them offers me results and the topics date from 2017/2018 so I doubt that those are the right methods.

So i want to change the icon and icon color based on state, the icon change succeeded but the color did not. i have te following setup:


sensor:
  - platform: template
  sensors:
      voordeur:
      value_template: '{% if is_state("binary_sensor.aeon_labs_zw089_recessed_door_sensor_gen5_sensor", "on") %}Open{% else %}Dicht{% endif %}'
      friendly_name: 'Voordeur'
      icon_template: >-
          {% if is_state('sensor.voordeur', 'Open') %}
          mdi:lock-open-variant-outline
          {% else %}
          mdi:lock
          {% endif %}

customize:
  sensor.voordeur:
    templates:
        icon_color: >
        if (state === 'Open') return [143,29,29];
        return [45,133,58];

Capture
open

Would love to know the answer to this as well. Iā€™ve resorted to building my own sensors and using entity_picture_template along with custom MDI icons that I create and then save locally in needed colors. Cumbersome.

    xc90_doors:
      value_template: >-
       {% if is_state('lock.xc90_door_lock','locked') %}
         Locked
       {% else %}
         Unlocked
       {% endif %}
      entity_picture_template: >-
        {% if is_state('lock.xc90_door_lock','locked') %}
            /local/lock-blue.png
        {% else %}
          {% if is_state('device_tracker.volvo_unknown','home') %}
            /local/lock-open-blue.png
          {% else %}
            /local/lock-open-yellow.png
          {% endif %}
        {% endif %}

and all this time I thought I was just being stupid for not figuring out how to do this in a non-hacked wayā€¦

Try with the hex color in single quotes, like

climate.oskar:
  unit_of_measurement: "%"
  icon: mdi:air-humidifier
  templates:
    icon_color: >
      if (attributes.hvac_action === 'heating') return '#f6d234';
      if (attributes.hvac_action === 'idle') return '#ff0000';

if im not mistaken you still have to use Custom UI for this.

At least thats what ive been using for 6 months or so.

unless someone else chimes inā€¦

then in customize.yaml:

light.sink:
  templates:
    icon: mdi:water
    icon_color: if (state === 'on') return 'rgb(0, 255, 0'; else return 'rgb(255, 0, 0)';
2 Likes

indeed you must have custom-ui installed for icon_color to be available in customize first of all.

if so, then use:

customize:
  sensor.voordeur:
    templates:
      icon_color: >
        if (state === 'Open') return 'rgb(143,29,29)';
        return 'rgb(45,133,58)';
1 Like

why not try:

      entity_picture_template: >-
        {% if is_state('lock.xc90_door_lock','locked') %}  /local/lock-blue.png
        {% elif is_state('device_tracker.volvo_unknown','home') %}  /local/lock-open-blue.png
        {% else %}  /local/lock-open-yellow.png
        {% endif %}

or was your nested if else intended? In that case please ignore my suggestion

Nope, youā€™re in the same boat as the rest of usā€¦ :wink:

Really seems like it should be built-in functionality - guess itā€™s time for a feature request for icon_color to work in sensors, switches and such - or even just icon_template / icon_color in lovelace would be fine with me - easier there than rebooting to reload updates sensors anyhow.

3 Likes

Ok, thanks all but it still doesnā€™t work.

I have installed CustomUI and can see the files in \www\custom_ui and also added these lines in my configuration.yaml

homeassistant:
  customize: !include customize.yaml

customizer:
  custom_ui: local

frontend:
  themes: !include themes.yaml
  javascript_version: latest
  extra_html_url:
    - /local/custom_ui/state-card-custom-ui.html
  extra_html_url_es5:
    - /local/custom_ui/state-card-custom-ui-es5.html

I have these lines in the customize.yaml

sensor.achterdeur:
  templates:
    icon_color: >
      if (state === 'Open') return [143,29,29];
      return [45,133,58];

sensor.voordeur:
  templates:
    icon_color: >
      if (state === 'Dicht') return [143,29,29];
      return [45,133,58];      

But still no results :frowning:

thats because your template rgb is incorrectly formatted ā€¦ check my suggestion again please.

btw the extra_html_url is deprecated.

also, be sure to check the state, it should be the exact state displayed in developer-tools/state

1 Like

Yes! Sorry I missed your message. it works now :slight_smile:

Capture

1 Like

Ok, sorry to open up this topic again. I cant get something working, probably beginnners levelā€¦

I want a simple motion sensor icon to not use the default yellow/blue colors but some others.

I have have been reading and googling the last hour but there is so much information, I went from card_mod to css to customisationā€¦ but never another colour of my icons :frowning: done a lot but this I think must be close to the solution. I have:

binary_sensor.pir_motion_sensor_toilet_01:
  templates:
    icon_color: if (state === 'detected') return 'rgb(204, 255, 204'; else return 'rgb(255, 0, 127)';

or

binary_sensor.pir_motion_sensor_toilet_01:
  templates:
    icon_color: if (state === 'on') return 'rgb(204, 255, 204'; else return 'rgb(255, 0, 127)';

And the state is:

And I use:

But nothing happens to my icon colour, it keeps the same:
image
image

Where am I missing the boat?

First question: did you install custom-ui?

You need that to be able to use icon_color at all

And the states are on/off so ā€˜detectedā€™ will never work, thatā€™s the frontend representation only.

No I donā€™t think so because I do not know what it isā€¦

Where can I get that? HACS?

Thought so, but eventually tried everythingā€¦

For now I ended up with this config in a picture elements card which works:

  - type: state-icon
    entity: binary_sensor.pir_motion_sensor_toilet_01
    title: Toilet-01
    style:
      top: 53%
      left: 39%
      '--mdc-icon-size': 40px
      '--paper-item-icon-color': lightgreen
      '--paper-item-icon-active-color': red

Yep, thatā€™s the core way now, per card, per entity.

Checkout GitHub - Mariusthvdb/custom-ui: Add templates and icon_color to Home Assistant UI for its options and possibilities

No need to try everything, simply check the state in developer-toolsā€¦

Hi Marius,

I had found and tried this. But there is no information icon colors per state?

Hold on, I misread, wrong link. I will try this and report back.

Also what I found:

'--mdc-icon-size': 40px
'--paper-item-icon-color': lightgreen
'--paper-item-icon-active-color': red

Is not mentioned there. Where would these ā€œoptionsā€ be documented? Have now scraped them from the forum. So I am still in the darkā€¦

Any other solution?

And for ā€œ-7 Happy customizing!ā€ in your linkā€¦ how :man_shrugging:

The style: is not from custom-ui but from card-mod by Thomas Loven. Thatā€™s what you should read up on. Please tell me you installed that?

No use blindly copying configs from the community without understanding what you are doing.

Come back here after installing it, and weā€™ll see if you still have trouble coloring per state;-)

Yes, I have:
image
image

That is what I am trying to understand. But there is so much information (also outdated) on this forum and I cannot find a simple way how to do what I want. Thus I end up trying out without knowing what I am doing.

Even with your advice that I greatly appreciate (!) I am not able to understand what I should do where and whyā€¦

You said:

But how would I do that? Where would I start to do that per entity? Or is that what 'not you meant? That I canā€™t do icon colors per entity so that it is use in every entity card in lovelace?

But instead I should so i in a card in lovelace per entity and how?

See where I am struggling (as a non-yet coder) :slight_smile:

EDIT: What I do want to avoid is to create a template sensor/thing per entity that I want a different icon colour based on stateā€¦

Edit: I am totally lost now where to start properly :see_no_evil: :ok_man:

EDIT2: I now have a lot more options (Perhaps due to the plugin):

But when I ad thing to this or via yaml, nothing happens with the color in lovelaceā€¦

This is what I use in customize.yaml:
</s> <s>binary_sensor.pir_motion_sensor_toilet_01:</s> <s> icon: mdi:account</s> <s> templates:</s> <s> icon: mdi:water</s> <s> icon_color: if (state === 'on') return 'rgb(128, 255, 0'; else return 'rgb(255, 0, 127)';</s> <s>

EDIT4: Steep learning curveā€¦ not sure what did the trick but got it workingā€¦ pfew

And you can use Jinja templates. Really, check the examples there and in the dedicated card-mod thread.

As for the Ha customize Ui you show, donā€™t use that for custom-ui, you can only use that for core customization. So not for icon_color or templates. As I stated in the repo, many of the custom-ui options are no longer supported, and, though displayed in that Ui, are no longer valid.

Thank you. So now I still do not know the right way. Is that the customise.yaml file and customise per entity or customise in lovelace?