Need help with sensor icon color based on state

Sorry i wasnt clear that is in sensors.yaml, just wanted to show you i already changed the mdi to cast

anayway as i replaced that piece of code in sensors.yaml with:

chromecast:
  friendly_name: ChromeCast
  value_template: "{% if is_state('device_tracker.chromecast', 'home') %}Connected{% else %}!no-reply, please check!{% endif %}"
  icon_template: "{% if is_state('device_tracker.chromecast', 'home') %} mdi:cast{% else %}mdi:lan-disconnect{% endif %}"

it works, the icon does get updated based on status.

ok cool, just follow the guidelines of which option is available in which case, in this case sensor template, and your example now should be ok.

you could use the device_class, and take out the icon template.
If you’d wish to color the icon, use the customize on the real state of the sensor you’d wish to change.

Thank you much for youre dedicated support on this!, as i understand now i just need to go for lovelace to get the colouring working and skip the custom_ui, thats not supported. are binary sensors always yellow by its default? if you saw my screenshot above, its internet ping google = yellow

Sorry to warm up this threat, but I read the whole thing and still can’t get my icon to change (itself and colour) depending on the state of a sensor…

This is the Sensor:

  - platform: template
    sensors:

      wohnzimmer_harmony_hub:
        value_template: '{{ states.remote.wohnzimmer.attributes.current_activity }}'
        friendly_name: 'Wohnzimmer Harmony'

Right now it can report ‘ShieldTV’, ‘AUX’ and ‘OFF’

I would like to change the icon and colour depending on the state…

This is my customize.yaml:

sensor.wohnzimmer_harmony_hub:
  templates:
  icon: >
    if (state === 'ShieldTV') return 'mdi:television';
    if (state === 'AUX') return 'mdi:speaker';
    return 'mdi:television-off';
  icon_color: >
    if (state === 'ShieldTV') return 'rgb(251, 210, 41)';
    if (state === 'AUX') return 'rgb(251, 210, 41)';
    return 'rgb(54, 95, 140)';

I also tried to remove one of the ‘if’ parts

sensor.wohnzimmer_harmony_hub:
  templates:
  icon: >
    if (state === 'ShieldTV') return 'mdi:television';
    return 'mdi:television-off';
  icon_color: >
    if (state === 'ShieldTV') return 'rgb(251, 210, 41)';
    return 'rgb(54, 95, 140)';

to try if that’s the problem, but then the icon just disappears …

What am I doing wrong?!

try correct spacing:

sensor.wohnzimmer_harmony_hub:
  templates:
    icon: >
      if (state === 'ShieldTV') return 'mdi:television';
      if (state === 'AUX') return 'mdi:speaker';
      return 'mdi:television-off';
    icon_color: >
      if (state === 'ShieldTV') return 'rgb(251, 210, 41)';
      if (state === 'AUX') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

also, just to be sure, you’ve got custom-ui installed?

as a side note, for a template sensor, you can set the icon in the template sensor itself.
icon_color always needs custom-ui though.

No, I don’t think so :flushed:

What is this, and where can I get it?! Do you have a link?!

EDIT:

This way I wouldn’t have to install anything, can change the icon, but not the colour?! That would actually do it

EDIT2:

That’s what I did now - not perfect, but looks decent enough :slight_smile:

        value_template: '{{ states.remote.wohnzimmer.attributes.current_activity }}'
        friendly_name: 'Wohnzimmer Harmony'
        icon_template: >
          {% if is_state('sensor.wohnzimmer_harmony_hub', 'ShieldTV') %}
              {{ 'mdi:television' }}
          {% elif is_state('sensor.wohnzimmer_harmony_hub', 'AUX') %}
              {{ 'mdi:speaker' }}
          {% else %}
              {{ 'mdi:television-off' }}
          {% endif %}

and yes your template is fine, could be a bit cleaner:

        value_template: >
          {{ state_attr('remote.wohnzimmer', 'current_activity' }}
        friendly_name: 'Wohnzimmer Harmony'
        icon_template: >
          {% if is_state('sensor.wohnzimmer_harmony_hub', 'ShieldTV') %}
              mdi:television
          {% elif is_state('sensor.wohnzimmer_harmony_hub', 'AUX') %}
              mdi:speaker
          {% else %}
              mdi:television-off
          {% endif %}

you could even create a mapper, but that might be a bit much for such a small template…

btw, just to be sure: Your value_template is pointing to another entity than the icon_template. This is what you want?

hi, I have this binary_sensor with icon change based on the state. However, I can’t change the color of the icon when it’s on.

binary_sensor:
  - platform: template
    sensors:
    #Stato PDC
      stato_pompa:
        friendly_name: "Stato PDC"
        value_template: "{{ states('sensor.consumo_attuale_pdc')|float > 150 }}"
        device_class: power
        delay_on:
          seconds: 10
        delay_off:
          seconds: 10
        icon_template: >-
          {% if is_state("binary_sensor.stato_pompa", "on") %}
            mdi:fan
          {% else %}
            mdi:fan-off
          {% endif %}

did u make it?