Global customize icon color for state sensors don't work

I got a few zwave power socket switches (neo coolcam), and I’ve created special state sensor because these switches should always stay on and I dont want to turn them off by mistake (fridge, freezer etc).

The state sensors look like this:

platform: template
sensors:
  state_switch_bosch_freezer:
    friendly_name: "Switch State Freezer"
    value_template: "{{ states.switch.bosch_freezer.state }}"

the cust glob file like this:

"sensor.state_switch_*":
  icon: mdi:power-socket-eu
  templates:
    rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

In HA for these sensor_state_switch_* entities I can see that the customization is attached to the entities:

friendly_name: Switch State Freezer
icon: mdi:power-socket-eu
templates: { "rgb_color": "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];" }

however, in my lovelace glancecard for these entities the icons stay blue instead of the configured yellow for the state on.
For test purposes I tried to show the yellow coloured icon if the state of the sensor is ‘off’ but also then the color stays blue.

Any thoughts on what might be wrong?
If I use the actual switches that to color changes to yellow proberly, only for these sensors it seems to fail.

Lovelace does not use anything from the old interface, especially custom_ui stuff. They are 100% separate. You’ll have to configure that in lovelace.

not true, icons set in global customize or normal customize are reflected in lovelace.

I’ve also set the folowing global customize:

"switch.*":
  icon: mdi:power-socket-eu
  templates:
    rgb_color: "if (state === 'on') return [251, 210, 41]; else return [54, 95, 140];"

and that one works flawlessly in lovelace

edit: the switch icons change color by default so rgb_color doesn’t do a thing

I also use rgb templates for icons and they mostly work in Lovelace, however I think that in some cards they don’t seem to work. I haven’t investigated further.

some time ago rgb_color was disabled and changed into icon_color for customizations. Note, this was before Lovelace, so maybe not up to date, bit still, you might try this:

sensor.dorm_state:
  templates:
    icon: >
      if (state === 'on') return 'mdi:human-child';
      return 'mdi:hotel';
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

and see what happens. I use this on 0.81.6 non_lovelace with custom-ui installed, and works without any issue at all. Use any Css color name: https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/features.md#icon-color

@teachingbirds it not a card issue. the normal switches (also with custom icon and rgb color works just fine).
@Mariusthvdb I will try it for sure! thanks for the suggestion. One thing though, I found another topic where you mentioned that for template_sensors (which I try to change) I have to set the color in the declaration of the entity itself? I can’t find any reference though about the correct syntax.

I tried extending my template sensor with icon color like this:

platform: template
sensors:
  state_switch_central_heating:
    friendly_name: "Switch State Central Heating"
    value_template: "{{ states.switch.central_heating.state }}"
    icon_color: "if (state === 'on') return 'green'; return 'red';"

but icon_color is not accepted there. (also tried color names instead of rgb values but also no luck).

I keep finding it strange that a global customize icon does work for template_sensors, but the color doesn’t, while it does work for the actual switches.

edit: and rgb_color is actually still working as well but I changed it to icon_color for future updates.

No, what I tried to say was that if you use a template sensor, you need to set available options/customizations in that sensor definition (and not in customize section)

icon_color is not one of them, so if you want that, you need to set it in customize, but you can only do so after having installed custom-ui and use the template I provided.

please not the difference in syntax between the 2.

ok, got it. I did find the mentioning of custom-ui and I do not have it installed yet, but as you mentioned, lovelace is completely separated from custom-ui so installing it will not help me in my case?

And still, in global customize I cán set the icon for all my state sensors, and I cán set the icon ánd icon_color for my normal switches (without having installed custom-ui)

It’s a bit unclear to me what should work out of the box and what not, but I will try to find out how to set the icon colors in lovelace itself.

edit: hmm correcting, disabling rgb_color for switch.* does nothing. I guess for switches the on off icon_color changes are part of HA code.

If you want anything else than the regular colors, you’d need to customize it. Maybe best to see first if you like the built in front-end presentation of HA and if needed, add customization. the green and red of your example are not standard.

I do like the built in front-end presentation of HA, but noticed that sensors with on/off states don’t change color, that’s why I try changing the colors for those icons.

Understand. In that case you’d best install custom-ui to be able to do something like this:

54

sensor:
  platform: template
    vijverpomp:
      friendly_name_template: >
        {{ states('sensor.owm_temperature')|float }}°C - Vijverpompen {{'mogen:' if states('sensor.owm_temperature')|float < 6 else 'moeten:'}}
      value_template: >
        {{'Uit' if states('sensor.owm_temperature')|float < 6 else 'Aan'}}
      icon_template: >
        {% if states('sensor.owm_temperature')|float < 6 %} mdi:engine-off-outline
        {% else %} mdi:engine-outline
        {% endif %}

and the customization:

homeassistent:
  customize:
    sensor.vijverpomp:
      templates:
        icon_color: >
          if (state === 'Uit') return 'blue';
          return 'red';

as you can see I use the available options of the template sensor in the sensor declaration itself, and customize its icon_color separately, which you can do using any CSS color code.

I have only imported my setup though the import tool and little to no experience with Lovelace, but from what I can see, the sensors do take the color I’ve set in custom-ui, so you might be safe there :wink: It doesn’t do a lot of other things, like the extra_data_templates, or badges, nor does Lovelace show the Tiles I’ve setup.

1 Like

thanks Marius, I got it working now with custom-ui enabled.

My icons finally change color :slight_smile:

1 Like

cool, now you got it working, you can color your HA setup to your liking :wink: have fun!

1 Like