Template using x< z doesn't always work

continuing from this Jinja / Template Sensor Question

I am lost.
using

          {% set km = states('input_number.lightning_strikes_near') %}
          {{ states.geo_location|selectattr('attributes.source','eq','wwlln')
             |selectattr('state','lessthan',km)|list|count }}

works fine with this input_number

input_number:
  lightning_strikes_near:
    icon: mdi:weather-lightning
    name: Lightning km
    initial: 50
    min: 0
    max: 400
    step: 10

it doesn’t however work always.
I would think, that if it shows the strikes when input_number is set to 50, it would also shows these when set to 100, since if the selection in the template is smaller than 50, it is also smaller than 100…?

its not a matter of the inout_boolean, because entering the numbers in the template directly has the same result.

please have a look?

states are always strings. You’re comparing 2 strings. When you compare strings '9' is greater than '10' because the first item in the string '9' is greater than the first item ('1') in '10'.

This is where your comparison is happening.

a yes, I had thought about that, trying to make them into |int, but that errors out in the template editor.

so would there be no way to make this template work as desired then?

thing is the input_number shows a float:

38

but even that is made a string in the jinja?

update

Since I saw no solution using the selectattr('state','lessthan',km), (state always being a string, while the template needs a number) I rewrote it like this then:

          {% set km = states('input_number.lightning_strikes_near')|int %}
          {%- for s in states.geo_location|selectattr('attributes.source','eq','wwlln')%}
            {%- if s.state|int < km %}
              {% if loop.first %}{{loop.length}} Strikes within {{km}} kilometers
              {% endif %}
            {%- endif %}
          {%- endfor %}

An eqivalent template to your original would be:

{% set km = states('input_number.lightning_strikes_near') | float %}
{% set ns = namespace(count=0) %}
{% for s in states.geo_location | selectattr('attributes.source','eq','wwlln') %}
  {% if s.state | float < km %}{% set ns.count = ns.count + 1 %}{% endif %}
{% endfor %}
{{ ns.count }}

The one you just wrote won’t work because your loop.length is based on this generator states.geo_location|selectattr('attributes.source','eq','wwlln')

thanks Petro, will test as soon as strikes happen.
because I still want tp list them also (next to count) I had hoped this would work as an alternative:

      lightning_strikes_near_alt:
        entity_id: sensor.time, input_number.lightning_strikes_near
        friendly_name: Lightning strikes near alt
        value_template: >
          {% set km = states('input_number.lightning_strikes_near')|int %}
          {% set strikes = states.geo_location|selectattr('attributes.source','eq','wwlln')%}
          {% for s in strikes if s.state|int < km %}
            {%- if not loop.first %}, {% endif -%}
            {{- s.name -}}
            {% else %} No strikes near
          {% endfor %}

would that be hampered by the same issue of the generator?
If so, I should probably best add the list creation to your last template?

probably, you have an odd one there because you’ll get a ton of No strikes near if theres recent strikes.