Custom UI - icon color change

card-mod did the trick. thanks again @Mariusthvdb. Although Iā€™m not too acquainted yet to JavaScript and CSS so I more or less copied examples.

I believe there is a way to better optimize my code as it mostly duplicates three times :

    badges:
      - entity: person.anne_laure
        style:
          ha-state-label-badge:
            $:
              ha-label-badge:
                .: |
                  :host {
                    {% if is_state('person.anne_laure','home') %} --label-badge-red: green;
                    {% else %} --label-badge-red: red;
                    {% endif %}
                   }
      - entity: person.emmanuel
        style:
          ha-state-label-badge:
            $:
              ha-label-badge:
                .: |
                  :host {
                    {% if is_state('person.emmanuel','home') %} --label-badge-red: green;
                    {% else %} --label-badge-red: red;
                    {% endif %}
                   }
      - entity: person.valentine
        style:
          ha-state-label-badge:
            $:
              ha-label-badge:
                .: |
                  :host {
                    {% if is_state('person.valentine','home') %} --label-badge-red: green;
                    {% else %} --label-badge-red: red;
                    {% endif %}
                   }

exactly, hence my request above if we could use entity instead of the entity name in the template. If so, we could simply use a yaml anchor:

    badges:
      - entity: person.anne_laure
        style: &badge_style
          ha-state-label-badge:
            $:
              ha-label-badge:
                .: |
                  :host {
                    {% if is_state(entity,'home') %} --label-badge-red: green;
                    {% else %} --label-badge-red: red;
                    {% endif %}
                   }

      - entity: person.emmanuel
        style: *badge_style

      - entity: person.etc
        style: *badge_style

we might have to ask Thomas Loven or Kendell about this, since they wont be reading along here :wink:
see šŸ”¹ Card-mod - Super-charge your themes! for the relevant thread

NM! Found it already: use config.entity

even 1 line shorter:

badges:
  - entity: person.me
    <<: &badge
      style:
        ha-state-label-badge:
          $:
            ha-label-badge:
              $: |
                .label-badge .label span{
                  {% if is_state(config.entity,'home') %} color: purple;
                  {% else %} color: black;
                  {% endif %}
                }
          .: |
            :host {
              {% if is_state(config.entity,'home') %} --label-badge-red: gold;
              {% else %} --label-badge-red: grey;
              {% endif %}
            }

  - entity: person.she
    <<: *badge

depending on what you want to define under the anchor.

I understand the syntax (never used the &myvariable/*myvariable scheme before) and discover the config.entity possibility. Have to read a bit about that config.entity way of doing.

HA is an infinite montain of knew knowledge for me :wink:

yeah these are Yaml Anchors, and can be defined ā€˜on the flyā€™ so to speak. Anything written after the definition character <<: &anchor_name will be copied (ā€˜mergedā€™ in anchor lingo) when you put <<: *anchor_name

anchors are defined per yaml file. Meaning, if you need the same anchor in another file you need to recreate it first.

hereā€™s a Yaml primer by Thomas, read all the way down: http://www.thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

since examples can be easier to understand than the above primer, (there are 2 different ways to create Anchors) have a look at more examples in https://github.com/Mariusthvdb/custom-ui/blob/2ba4168e9554e9290bc3f2c872316a522639f2e4/examples.md

thanks, Iā€™ll read that.

the anchor syntax is simple yet powerful. Iā€™ll probably reuse it as I hate repeating code.

Change the ICON Color dont work for me :frowning:

customize:

binary_sensor.office_window_xiaomi_1_openclose:
  friendly_name: BĆ¼ro Fenster
  device_class: window
  templates:
    rgb_color: "if (state === 'on') return [200, 100, 30]; else return [10, 20, 30];"

But this Example dont work :frowning:

if i open the Window, the icon color is the Frontend Style:

ha-icon[data-domain="binary_sensor"][data-state="on"]
color: var(--paper-item-icon-active-color, #fdd835);

default Color.

What I need, is when the window is open, then change the color: --color-red-error-1
or RGB

i hope you an help me! Thanks all

You should use icon_color: and not rgb_color:

Also, check the correct color format to use, you can find many examples in the examples ;-)-

yes, i test so many ways, but nothing works.

here the new Test in customize File:

binary_sensor.office_window_xiaomi_1_openclose:
  friendly_name: BĆ¼ro Fenster
  device_class: window
  icon_color: "{% if is_state('binary_sensor.office_window_xiaomi_1_openclose', 'on') %}rgb(255, 214, 10){% else %}rgb(255, 255, 255){% endif %}"

icon_color can only be set using custom-ui, which uses javascript templates, and not jinja templates like you use.

did you install custom-ui?

yes i install custom-ui

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

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

is that okay?

the yaml in customize:

binary_sensor.office_window_xiaomi_1_openclose:
  friendly_name: BĆ¼ro Fenster
  device_class: window
  templates:
    icon_color: '{% if is_state("binary_sensor.office_window_xiaomi_1_openclose", "on") %}rgb(255, 214, 10){% else %}rgb(255, 255, 255){% endif %}'

yaml office:

- type: glance
        show_name: true
        style: !include ../includes/style_entities.yaml
        entities:
          - entity: light.light1test
            name: Licht
          - sensor.buro_dect301_1_temperature
          - entity: binary_sensor.office_window_xiaomi_1_openclose
            name: Fenster

window

window2

this is not ok. As I said before.

please check the many examplesā€¦ you started out well Custom UI - icon color change - #159 by basti4k but somehow changed from a JS template to a jinja templateā€¦ why?

try:

  templates:
    icon_color: >
      if (state == 'on') return 'rgb(200, 100, 30)'; 
      return 'rgb(10, 20, 30)';

and you should be set

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