I’d like to change the color of a badge depending on the number of lightning strikes detected in the area.
My dumb code below sets the color to green when there are no strikes and yellow otherwise:
type: custom:mushroom-template-badge
content: Foudre à Clermont
icon: mdi:weather-lightning
color: |-
{% if is_state("sensor.blitzortung_lightning_counter", "0") %}
green
{% else %]
yellow
{% endif %}
label: ""
But I’d rather like to set levels (yellow from 1 to 10, orange from 11 to 50, red for 50+). I tried {% if (numeric_state("sensor.blitzortung_lightning_counter") <= 10) %} to no avail.
Hi Nick, I really encourage you to use template under development tools to try things out. One example to your question could be:
{% if states('sensor.blitzortung_lightning_counter') | int == 0 %}
green
{% elif states('sensor.blitzortung_lightning_counter') | int > 0 and states('sensor.blitzortung_lightning_counter') | int < 11 %}
yellow
{% elif states('sensor.blitzortung_lightning_counter') | int > 10 and states('sensor.blitzortung_lightning_counter') | int < 51 %}
orange
{% else %}
red
{% endif %}
As for the solution, t hree days ago I didn’t even know Jinja2 even existed and I guess I miss the right reflexes. I didn’t think of converting the state to int, although I wondered what it was since my code worked after putting 0 in quotation marks… Was is a string? Are states always strings? If not, how can you know what type of variable it is?
Why can’t I use numeric_state or entity.numeric_state or numeric_state(entity) or something along these lines?
Hopefully, that someone else will chimes up and tell me what’s up with numeric_state and if I can use it in an if() statement since I can use it as condition: numeric_state
Not only: the Template editor in the Developer tools returns “number” as well (see my previous screenshot).
I’m not trying to argue and be anal about things, it’s just that I like to understand how things work and this threw me off. If there is a mistake in the docs or whatever, maybe a pull request (from someone who understands this) would prevent newbies making mistakes as well?