Help with state label based on sensor value

For the life of me I cannot figure out why this isn’t working. I just need a different set of eyes on it. I’m trying to change the color of a state label based on the battery level of my vacuum. I made a sensor in my config:

  - platform: template
    sensors:
      vac_batt:
        friendly_name: 'Roborock Battery'
        value_template: "{{ state_attr('vacuum.roborock_s7_maxv', 'battery_level') | int }}"

and I’m using it in my card as follows:

 card_mod:
      style: |
        :host { 
          color: {% if is_state('sensor.vac_batt') > 75 %} green {% elif is_state('sensor.vac_batt') > 30 %} yellow {% else %} red {% endif %};
        }

The sensor I made for changing the color of the state label for the activity works fine, but this one just won’t. Any thoughts?

Try to use states('sensor.vac_batt') instead of is_state.

is_state requires 2 arguments, the first one should be the entity_id and the second one should be the value to compare to, but that is an equal comparison, while you are trying a higher than, so probably in your case the states will work better, or even the state_attrib, as in this last case you don’t need that template sensor.

Nope. Still no color change.

Ok, I don’t know if that way of setting color work on that card.

What if you try this (I would expect the state always on red):

card_mod:
      style: |
        :host { 
          color: red;
        }

[/quote]

Does that works?
I’m just trying to verify the color attribute first before looking in the if/then/else.

No, that works because I did it the exact same way with the status of the vac. There is something wrong with the way it’s testing the sensor values. I can test in Node Red and tell that the value is a number so I’m not sure what’s wrong.

One more step isolating things to find the issue:

card_mod:
      style: |
        :host { 
          color: {% if 100 > 75 %} green {% elif 100 > 30 %} yellow {% else %} red {% endif %};
        }

It should become green now.

Yes. That works fine. It’s the comparison of the sensor values that’s breaking it.

What about this one? Which color you get and which color you expected?

card_mod:
      style: |
        :host { 
          color: {% if states('sensor.vac_batt') | float(0) > 75 %} green {% elif states('sensor.vac_batt') | float(0) > 30 %} yellow {% else %} red {% endif %};
        }

That worked! It bothers me that I don’t know why though. Thanks.

1 Like

Probably the sensor is returning a string, then you are comparing to a number. By using the float(0) it forces the conversion to a number and will be 0 in case the string cannot be converted to a number.

This one might work also, without the need of that template sensor:

card_mod:
      style: |
        :host { 
          color: {% if state_attr('vacuum.roborock_s7_maxv', 'battery_level') | float(0) > 75 %} green {% elif state_attr('vacuum.roborock_s7_maxv', 'battery_level') | float(0) > 30 %} yellow {% else %} red {% endif %};
        }

And you probably wouldn’t have had this problem if you have created your vac_battery sensor using a device_class (and using the new format for template sensors):

template:
  - sensor:
    - name: Roborock Battery
      unique_id: vac_batt
      unit_of_measurement: '%'
      device_class: battery
      state: "{{ state_attr('vacuum.roborock_s7_maxv', 'battery_level') | float(0) | round(0) }}"
1 Like