Template question with numeric state

Hey all

I’m using a Mushroom template card to change the accent color of an mdi icon based on the charge of my EV. However, when I use the code below, when the EV is charged to 100% the icon color is orange

{% if states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') > "80" %}
green
{% elif states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') > "60" %}
orange
{% elif states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') < "60" %}
red
{% endif %}

I would expect that 100 is higher than 80, because from 81 to 99 it’s green, when it becomes 100 it turns orange for some strange reason.

What am I doing wrong here?

Thanks!

You are comparing strings.

{% if states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') | int > 80 %}
green
{% elif states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') | int > 60 %}
orange
{% elif states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') | int < 60 %}
red
{% endif %}
1 Like

In addition to what @Hellis81 is saying, your logic is missing the value 60. If your battery level is ever exactly 60, the code will output nothing resulting in an unavailable sensor. Or if this is used in the frontend, the color black for the icon/text.

{% if states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') | int > 80 %}
green
{% elif states('sensor.volvo_yv1xzept1r2369964_battery_charge_level') | int > 60 %}
orange
{% else %}
red
{% endif %}
2 Likes

Ah gotcha, thanks for the help! Sometimes I’m still a bit off with Jinja :slight_smile:

I think Petros answer is more correct since it takes care of the ==60 issue

1 Like