Icon templates color stopped working

Hey,

So I did an update to 0.78.3 and my icon colors stopped working. I had them setup to change color when online or offline but that has stopped

Does anyone know why?

here is a snipped of the config

sensor.googlewifi_upstairs:
  templates:
    icon_color: > 
      if (state === "online") {return "green";}
      else {return "red";}

i donā€™t see anything wrong with the code.

you could try this:

sensor.googlewifi_upstairs:
  templates:
    icon_color: > 
      return (state === "online") ? "green" : "red";

The outcome is the same but maybe the interpreter will handle it better?

Hi Petro,

Thanks for the reply, I gave it a shot no difference Iā€™m afraid. Canā€™t see any errors either, so not sure how to provide more info :frowning:

Not sure either. Iā€™ve never done this but I know code pretty well. @Mariusthvdb have any suggestions?

try:

sensor.googlewifi_upstairs:
  templates:
    icon_color: > 
      if (state === 'online') return 'green';
      return 'red';
    icon: >
      if (state === 'online') return 'mdi:wifi';
      return 'mdi:wifi-off';

added a little bonus :wink:
assuming you have custom-ui installed? be sure to check the quotes are true single quotes, the forum software sometimes makes these funny curly quotes here. And, be sure to check the state (ā€˜onlineā€™ seems unlikely) is the real state. case-sensitive.

Thanks for the help guys I will give this a try, also now that you mention custom-ui, I think I am going to ā€œre-installā€ that also. Will let you guys know what happens.

@Mariusthvdb - you are correct the state ā€œonlineā€ is a custom config, what I did was use nmap device tracker which as you know returns home or not_home, but I was not happy with those states. so i created template sensors instead as below:

googlewifi_upstairs:
        friendly_name: 'Upstairs AP'
        value_template: >-
          {% if is_state("device_tracker.googlewifi_upstairs", "home") %}
            online
          {% else %}
            offline
          {% endif %}
        icon_template: >
          {% if is_state("device_tracker.googlewifi_upstairs", "home") %}
            mdi:wifi
          {% else %}
            mdi:wifi-off
          {% endif %}

ok, thats what I suspected. I that case, ditch the icon template I added, cause that wont work with template sensors, since they have their own icon_template as you already use.

the icon_color should be good then.

orā€¦

you could do away with the template sensor, and use my above customization on the device_tracker, and add:

device_tracker.googlewifi_upstairs:
  show_last_changed: true
  templates:
    icon: >
      if (state === 'home') return 'mdi:wifi';
      return 'mdi:wifi-off';
    icon_color: >
      if (state === 'home') return 'green';
      return 'red'; #or use 'grey' which is better on the eye unless u really want an alert...
    _stateDisplay: >
      if (state === 'home') return 'online';
      return 'offline';

@Mariusthvdb - Thanks for the config examples I have not seen _stateDisplay for the device tracker, where can I find out more on those kind of configs? Double checked the HA page on device tracker and it doesnā€™t mention that, probably because its under an advanced page somewhere. :slight_smile:

OK so its working again, turns out its not a config issue but a browser issue, came across a post with custom ui issues and HA 0.7+, honestly didnā€™t think to look for custom ui posts around this issue. Anyway turns out I just needed to flush my browser cache, in chrome specifically I just flushed ā€œcached images and filesā€ and everything came back.

Sorry wasting your time with config help, but I did learn new things, thanks so much for the help guys @petro and @Mariusthvdb , very much appreciated. :slight_smile:

1 Like

see: home-assistant-custom-ui/docs/features.md at master Ā· andrey-git/home-assistant-custom-ui Ā· GitHub
and home-assistant-custom-ui/docs/templates.md at master Ā· andrey-git/home-assistant-custom-ui Ā· GitHub

so you say this:

sensor.googlewifi_upstairs:
  templates:
    icon_color: > 
      if (state === "online") {return "green";}
      else {return "red";}

works?

Yah, that should work. same with this:

    icon_color: > 
      return (state === "online") ? "green" : "red";

well, its a first for me, I never new template sensors could have their icon_color set through customize like this, native HA, without custom-ui installed Itā€™s undocumentedā€¦

strangest thing is he uses the custom-ui format of

templates:
  icon_color:

without having custom-ui installed. So I am a bit baffledā€¦


no Icon_color mentioned.

not that I donā€™t welcome it, not at all!

even better. shorter!

in need for a little help myself here.

have this template sensor using this entity_picture_template:

entity_picture_template: >
  {{ '/local/tiles/family/w_{}.png'.format(states('sensor.w_life360_state')) }}

I now need that same template to work in a customization of another sensor, in the format

templates:
  entity_picture:
     '/local/tiles/family/w_{}.png'.format(states('sensor.w_life360_state'))

but it wont work, no matter what I try. {{ }}, no quotes, what have you. Any thoughts I could test?,

nevermind. duh. missed the return ā€¦

in case anyone needs it heres how to customize an entityā€™s icon, based on the state of another entityā€™s state:

sensor.m_places:
  templates: 
    entity_picture: >
      return '/local/tiles/family/m_' + entities['sensor.m_life360_state'].state + '.png';

provided you have pictures for these states, and custom-ui installed

Thats jinja.

Thatā€™s JS.

They arenā€™t the same syntax, so those methods are not interchangeable.

Yes I know.
The Jinja was from a template sensor , and because I needed the same logic now in another sensor, needed to use the custom-ui, that uses JavaScriptā€¦

Hence my search. But I found it.:wink:

Both are doing their job now just fine!

fyi this is eqivalent to pythons .format().

sensor.m_places:
  templates: 
    entity_picture: >
      return `/local/tiles/family/m_${entities['sensor.m_life360_state'].state}.png`;

yes, great that you mention that! ive used this before without truly understanding the syntax.
not really sure what the $ does.

its on the documentation of custom-ui too, but rather unexplainedā€¦

Itā€™s just an identifier for a variable inside a string.

Did anyone figure out what happened to icon_color?

itā€™s missing from sensor template, so hence itā€™s not registering,

what is everyone doing now to change the state color of the icon?

at the moment the below works if I remove the icon_color portion:

  - platform: template
    sensors:
        entry_motion:
          value_template: >-
            {% if states.binary_sensor.entry_multi_18.state == 'on' %}
              Detected
            {% elif states.binary_sensor.entry_multi_18.state == 'off' %}
              Normal
            {% endif %}
          icon_template: >-
            {% if states.binary_sensor.entry_multi_18.state == 'on' %}
              mdi:motion-sensor
            {% elif states.binary_sensor.entry_multi_18.state == 'off' %}
              mdi:walk
            {% endif %} 
          icon_color: >-
            {% if states.binary_sensor.entry_multi_18.state == 'on' %}
              red
            {% elif states.binary_sensor.entry_multi_18.state == 'off' %}
              blue
            {% endif %} 

icon_color isnā€™t a valid entry for a template sensor. Not sure where you got this idea from. Everything in this thread is for custom_ui and it goes in the customize section. Always consult the documents. If the documents do not contain a entry/field, then you canā€™t use that entry/field.

@Mariusthvdbā€™s post has the documents for custom ui