Custom UI - icon color change

Hi and many thanks for your help!!

Unfortunately I have no idea where and what the difference is to JS template or jinja template :slight_smile: i test the examples :slight_smile:
What is the right way for installation?

Can I see if everything is installed correctly and Custom UI is working properly?

binary_sensor.office_window_xiaomi_1_openclose:
  friendly_name: BĆ¼ro Fenster
  device_class: window
  templates:
    icon_color: >
      if (state == 'on') return 'rgb(200, 100, 30)';
      return 'rgb(10, 20, 30)';

the new code dont work :frowning:
Your code is simple and I understand it. But it does not work

apparently it isnt :wink:

did you read the readmeā€¦ ? Itā€™s all in there.

btw:

  extra_module_url:
    - /local/custom-ui/custom-ui.js

/usr/share/hassio/homeassistant/www/custom_ui/custom-ui.js

you are not using the same dash/underscore in the path. use either ā€˜_ā€™ or ā€˜-ā€™ in both

1 Like

OMG THANKS, yes in the Readme is the foldername: custom_ui

i rename the folder:
/usr/share/hassio/homeassistant/www/custom-ui/custom-ui.js

configuration.yaml

frontend:
  themes: !include_dir_merge_named themes
  extra_module_url:
    - /local/custom-ui/custom-ui.js

The file is now loaded in the developer tool:
CUSTOM-UI (JS) Version 20201218 adapted for HA 2020.X.X +

After ā€œrestartā€ it work :clap: :pray: :star_struck:

QA: if i edit a ā€œicon_color: >ā€ part, the color. Then always have to restart the completely HA?

no, as a rule of thumb, all customizations are reloaded if you click config/server_control RELOAD LOCATION & CUSTOMIZATIONS.

Sometimes a refresh of the browsers helps. On occasion, none of this kicks in and the next restart it will be fine.

1 Like

Thanks it works perfect!

i see in the example the way, for the color varible:

configuration.yaml

frontend:
  themes: !include_dir_merge_named themes
  extra_module_url:
    - /local/custom-ui/custom-ui.js

themes/ing_dark.yaml

# Status Color
alert_red:
  paper-item-icon-color: "var(--color-red-1)"
alert_blue:
  paper-item-icon-color: "var(--color-blue-1)"

# Theme
ING Dark:
  # Fonts
  color-red-1: "rgba(247,53,67,1)"
  color-blue-1: "rgba(6,169,244,1)"

customize:

templates:
    icon_color: >
      if (state == 'on') return 'alert_red';
      return 'alert_blue';

But it does not work :frowning:

where?

as stated, any valid css color value can be used. Not sure if that variable you created in your theme is causing your issue, please check this:

        icon_color: >
          if (state == 'on') return 'var(--primary-color)';
          return 'steelblue';

which works fine.

You canā€™t though use the theme name for the color_name, which is what you are doing.

1 Like

hehe, why is it so easy ? :smiley:
Thanks a lot ! Works great ! :clap: :clap:

maybe you can still include this example for others:

binary_sensor.office_window_xiaomi_1_openclose:
  friendly_name: BĆ¼ro Fenster
  device_class: window
  templates:
    icon_color: >
      if (state == 'on') return 'var(--color-red-1)';
      return 'var(--paper-item-icon-color)';

What about when you need to change the icon color based on the state of an entirely separate entity?

I have a button to execute a script on my dashboard, but because a script has no state the script toggles a boolean on or off. So even though my button entity is for a script, the script icon needs to changed based on the state of the associated boolean.

Hereā€™s what I have in my customization file but itā€™s not working:

script.script_fan_speed_0:
  friendly_name: 'Off'
  icon: mdi:fan-off
  templates:
    icon_color: >
      if is_state('input_boolean.boolean_fan_speed_0','on') return 'green';
      return 'white';

did you read the docs and check the examplesā€¦?
Guess not, because you use a mix between Jinja and JS templates, and not the required pure JS.

also, there are a couple of ā€˜globalā€™ examples.

# using an attribute of another (global) entity
    sensor.power_consumption:
      templates:
        icon_color: >
          if (entities['sensor.smart_meter'].attributes.power > 3000) return 'red';
          return green;

or use the state:

     binary_sensor.ha_update_available:
      templates:
        unit_of_measurement: >
          if (entities['sensor.ha_available_version']) return entities['sensor.ha_available_version'].state;
          return 'starting up';

btw, if you are using buttons in the Lovelace config, you might be better of using card-mod

Thanks. I did read. Half the docs supplied with integrations and custom cards still say to include resources in the Lovelace file, and examples are relevant assuming itā€™s still 2015. I am also using card-mod.

not sure what you are implying here.
Any ways, all provided examples are live in my own current setups, and valid up to 2021.3.b06

In general, a lot of documentation is not current. I donā€™t know which are yours but Iā€™m sure theyā€™re fine given you seem like an active user.

I followed the directions and modified my customize.yaml file, but it doesnā€™t seem to have worked. Here is my code:

sensor.washing_machine_cycle:
  icon: mdi:washing-machine
  templates:
  icon_color: >
    if (state == 'Idle') return 'rgb(68,115,158)';
    if (state == 'Cleaning') return 'rgb(253,216,53)';
    return 'rgb(73,158,68)';
    
sensor.dryer_cycle:
  icon: mdi:tumble-dryer
  templates:
    icon_color: >
      if (state == 'Idle') return 'rgb(68,115,158)';
      if (state == 'Drying') return 'rgb(253,216,53)';
      return 'rgb(73,158,68)';

Anyone have any ideas why the icon color isnā€™t changing?

Did you reload customize.yaml?

and refresh webpage or app page after reloading configuration.yaml?

Configuration / server controls / localizations and customizations

if you click on the entity, and select attributes (providing of course you have custom-ui also installed and running), can you see your customized code under attributes

also try (note === rather than == , & else before last ā€˜returnā€™)


  icon_color: >
    if (state === 'Idle') return 'rgb(68,115,158)';
    if (state === 'Cleaning') return 'rgb(253,216,53)';
    else return 'rgb(73,158,68)';

So it should be === instead of ==? Iā€™ve never seen the three instead of the two.

Not home to try it right now but I did restart after adding it originally.

And I am seeing the template under attributes, but not the individual icon color attribute.

The syntax seems to be correct.

I was facing yesterday same problem and I realized that simply it does not work for all Lovelace card. Some of them are using their own color handling and in such case custom_ui cannot help.

Eg. glances or entity cards work, button card does not.

So first try to use the sensor.washing_machine_cycle in glances card. If coloring works fine then it is an issue of used Lovelace card.

My disucssion with Marius : https://github.com/Mariusthvdb/custom-ui/issues/41

1 Like

Your templates seem fine. (Top template needs an extra indent though like the one below)
Did you correctly install custom-ui? Check at configuration/info

Marius, I need your help.

There are sensors for battery level customized by Custom UI (this repo):

homeassistant:
  customize_glob:

    sensor.battery_*:
      templates:
        icon_color: >
          if (state == 'unavailable') return 'brown';
          if (state == 'unknown') return 'brown';
          if (state <= Number(entities['input_number.battery_level_critical'].state)) return 'red';
          if (state <= Number(entities['input_number.battery_level_warning'].state)) return 'rgb(250,218,67)';
          return 'green';

But the iconā€™s color is not always correct:
image

type: vertical-stack
cards:
  - type: entity
    entity: sensor.battery_life360_ildar
  - type: entities
    entities:
      - entity: sensor.battery_life360_ildar
  - type: custom:button-card
    entity: sensor.battery_life360_ildar

It is correct in the Entities card, but not in the Entity card & custom:button-card.
What could be a reason of it?

More-info also has a right color:
image

yes, that is all correct. Not all cards have the same code to display the icon, and you found the most obvious ones.

I can help you with button-card, to ā€˜importā€™ the colors set in custom-ui, and it was made by Romrider himself, using this button_card_template:

# https://github.com/custom-cards/button-card/issues/419
support_custom_ui:
##  icon: |
##    [[[
##      if (entity && entity.attributes && entity.attributes.templates) {
##        const tpl = entity.attributes.templates;
##        if (tpl.icon) {
##          return new Function('state', `'use strict'; ${tpl.icon}`).call(this, entity.state);
##        }
##      }
##      return null;
##    ]]]
  styles:
    icon:
      - color: |
          [[[ if (entity && entity.attributes && entity.attributes.templates) {
                const tpl = entity.attributes.templates;
                if (tpl.icon_color) {
                  return new Function('state', `'use strict'; ${tpl.icon_color}`).call(this, entity.state);
                }
              }
              return null; ]]]

Note the icon is actually imported by button-card, so I commented that bit, but left it in for educational purposes.

Entity card, one shall have to resort to card-mod, as with picture-glance (which you already know as we talked extensively on that with my pond-pumps card :wink: )

btw, you can hide the templates and icon_color attributes from the more-info

in you Number() template: I believe you can simply compare the state of the input_number, the JS will figure it out and treat it as a number?
EDIT:
on the Number(): it is indeed necessary, sorry for the confusion. See in the post below for some extra comments on that.

Thank you, I will take a look with attention.

Does it mean that I may use this template whenever I need a Custom UI support for the iconā€™s color?

Mmm, I wanted to use Custom UI to get rid of card-modā€¦

Yes, that is my 2nd answer - how to do it???

Tried a lot with different approaches, only this option seemed to workā€¦