If template for sensor state calculate if greater then and smaller then

Hi All,

For a mushroom card I want create a if template so icon color will change. This must change for a temperature sensor. But not how example is shown. But if sensor is in between -10 and -5, -5 and 0, etc
The last one must be 40 or more.

      {% if is_state('sensor.temperature', '-10') %} 
        dark blue
      {% elif is_state('sensor.temperature', '-5') %} 
        blue
      {% elif is_state('sensor.temperature', '0') %} 
        light blue
      {% elif is_state('sensor.temperature', '5') %} 
        light green
      {% elif is_state('sensor.temperature', '10') %} 
        green
      {% elif is_state('sensor.temperature', '15') %} 
        dark green
      {% elif is_state('sensor.temperature', '20') %}
        orange
      {% elif is_state('sensor.temperature', '25') %} 
        light red
      {% elif is_state('sensor.temperature', '30') %} 
        red
      {% elif is_state('sensor.temperature', '35') %} 
        dark red
      {% elif is_state('sensor.temperature', '40') %} 
        purple
      {% endif %}

you mean smth like this:

      - unique_id: feel_temperature_color_name
        state: >
          {% set temp = states('sensor.buienradar_feel_temperature')|float(0) %}
          {% if temp < -20 %} black
          {% elif temp < -15 %} navy
          {% elif temp < -10 %} darkblue
          {% elif temp < -5 %} mediumblue
          {% elif temp < 0 %} blue
          {% elif temp < 5 %} dodgerblue
          {% elif temp < 10 %} lightblue
          {% elif temp < 15 %} turquoise
          {% elif temp < 20 %} lightgreen
          {% elif temp < 25 %} darkgreen
          {% elif temp < 30 %} orange
          {% elif temp < 35 %} crimson
          {% else %} firebrick
          {% endif %}

? (didnt copy your colors… this is what lives in my config :wink: )

Ohh yes. This is an other way to do. Was forgotten this one. haha Thanks!!!
Look like this is a sensor part. Are you use the sensor into a template in UI so no lots of code goes into the UI Lovelace code?

exactly…
I use custom_ui to set colors on icons. this way I only have to create that template once, and next, use the sensor name in the frontend config.

in js this would be:

homeassistant:

# create some node_anchors
    node.anchors:

      temp_color: &temp_color
        hide_attributes: templates
        templates:
          icon_color: >
            if (state === 'unknown') return 'grey';
            if (state < -20) return 'black';
            if (state < -15) return 'navy';
            if (state < -10) return 'darkblue';
            if (state < -5) return 'mediumblue';
            if (state < 0) return 'blue';
            if (state < 5) return 'dodgerblue';
            if (state < 10) return 'lightblue';
            if (state < 15) return 'turquoise';
            if (state < 20) return 'lightgreen';
            if (state < 25) return 'darkgreen';
            if (state < 30) return 'orange';
            if (state < 35) return 'crimson';
            return 'firebrick';

  customize_glob:

# and use that node_anchor ;-)
      sensor.*temperature: *temp_color

but dont let me lure you to custom_ui :wink:

1 Like

Seems nice… Will check it. Have also some custom ui stuff. Funny that ‘navy’ and ‘crimson’ gives me black icon. Or something with latest HA issue maybe. Have to check

those colors on mushroom are prefixed names, you cant use all of theme. Or so I seem to recall