Dynamic badge color and lightning detector

With the Blitzortung integration, I have a sensor that states the number of lightning strikes:

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.

What would be the proper syntax?

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 %}

Thanks Mats789!

When using a template in a card I generally don’t bother because I get the feedback immediately too when editing the card:

Or did I miss something?

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?

icon_color: >-
  {% if states('sensor.blitzortung_lightning_counter') | int == 0 %}
...

I got that. But it brought this series of questions:

Yes, but here someone else will chime in and tell you the truth

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

Yes.

By using commands like “is_number()” etc, see here.

Because these are not supported commands or attributes, see a link above.

But:

So shouldn’t states('sensor.blitzortung_lightning_counter') return a number as well?

It does:

But it doesn’t really:

Numbers are strings in Jinja2 ?

IMHO there is a confusion in Docs.
States are strings which may contain numbers.

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?

No idea then. For me it is a wrong implementation confusing users.
Just always convert states to numbers for math operations.