Change icon color based on sensor value

Hi!

I’m trying to change the Co2 icon color based on the value. I tried everything I could but could not get it.

Here’s my code:

# CO2
- platform: template
  sensors:
      co2:
        value_template: >
          {{ state_attr('sensor.eco2_exterior','battery')|round }}
        friendly_name: 'CO2'
        unit_of_measurement: 'mmp'
        device_class: carbon_dioxide
        icon_template: >-
          {% set co2 = states('sensor.eco2_exterior')|float(0) %}
          {% if quality > 15000 %}
          red
          {% elif 15000 >= co2 > 5000 %}
          orange
          {% elif 5000 >= co2 > 2500 %}
          yellow
          {% elif 2500 >= co2 > 2000 %}
          Zeer Ongezond
          {% elif 2000 >= co2 > 1500 %}
          green
          {% elif 1500 >= co2 > 650 %}
          green
          {% elif 650 >= co2 > 400 %}
          green
          {% elif 400 >= co2 > 0 %}
          green
          {% else %}
          green
          {% endif %}

What am I doing wrong?

Where does quality come from?
Furthermore, I think the doubled greater than… in combination with greater/equal won’t work. <— rubbish, I’ve read it the wrong way.

1 Like

Sorry. That was copied from a website.

Ah, I thought that could be another but undefined variable.

You’ re writing an icon_template. That can change an icon, not its color.

For this, you either need to use card-mod, or custom-ui

1 Like

WAAAH! I’m so blind… time to go to bed…

1 Like

I’m going to take a look at custom-ui.
Thank you!