Help: Template Sensor Humidity - Range between

Hi, im trying to create following template sensor.

i want to know and count all humidity more than 62% but smaller than 72%.
Only this range 62-72.
im new with HA and do it by google and try & error, but can’t fix it by myself
could someone help me please.

 Humidity_62:
        friendly_name: "Luftfeuchtigkeit über62"
        value_template: >
          {% set sensor = [
            'sensor.channel2_humidity',
            'sensor.channel6_humidity',
            'sensor.channel7_humidity',
            'sensor.channel8_humidity',
            'sensor.channel9_humidity',
            'sensor.channel10_humidity',
            'sensor.duschbad_temp_display_humidity',  
            ] %}
          {{ expand(sensor) | selectattr('state','>=','62' or <=  '72') | list | count }}

thanks in advance

I would rename your variable from sensor to something like my_sensor

bad karma to use existing names.

{{ expand(my_sensor) | selectattr('state','>=','62') | selectattr('state', '<=', '72') | list | count }}

That likely will work for the given range, but for most cases you should cast the state value to an integer to avoid the oddities of string comparisons and therefore insure an accurate count. FWIW, you can avoid the expand() by using map('states') since you don’t need the entire state object.

      humidity_62:
        friendly_name: "Luftfeuchtigkeit über62"
        value_template: >
          {% set sensor = [ 'sensor.channel2_humidity', 'sensor.channel6_humidity',
            'sensor.channel7_humidity', 'sensor.channel8_humidity',
            'sensor.channel9_humidity', 'sensor.channel10_humidity',
            'sensor.duschbad_temp_display_humidity'] %}
          {{ sensor | map('states') | map('float')
          | select('>=', 62) | select('<=', 72) | list | count }}

Thanks a lot!
works perfect

@armedad : i’ll change it, good hint.