How to map state to correct type (|float)

for an auto-entities card I need something like the template below, so I can replace the fixed state: '>30' I had originally to an input_numbers state:

{{states('input_number.power_threshold')}}
{% set threshold = states('input_number.power_threshold') %}
{{states.sensor
 |selectattr('entity_id', 'search', '_actueel')
 |selectattr('state','>',threshold)
 |map(attribute='state')
  |list}}

results in
Schermafbeelding 2022-01-06 om 14.24.02

because state is a string, and the number is a number obviously…

I can not use |map(‘float’,0) anywhere, so how do I fix this comparison to be correct…

      - type: custom:auto-entities
        card:
          type: entities
          card_mod: &mod
            style: |
              ha-card {
                box-shadow: none;
                margin: 0px -16px 0px -16px;
              }

        filter:
          template: >
            {{states.sensor
             |selectattr('entity_id', 'search', '_actueel')
             |selectattr('state','>',states('input_number.power_threshold'))
             |map(attribute='entity_id')
              |list}}
#          include:
#            - entity_id: sensor.*_actueel
#              state: '>30'

please have a look? thanks!

__ edit__

Wait, as always Order of Operations… !

If I change the order of operations to:

{{states('input_number.power_threshold')}}
{% set threshold = states('input_number.power_threshold')|float %}
{{states.sensor
 |selectattr('entity_id', 'search', '_actueel')|map(attribute='state')
 |map('float',default=0)
 |select('>',threshold)
  |list}}

Schermafbeelding 2022-01-06 om 14.47.27

so this selects the correct entities, but now I need to map those back to their entity_id…

            {% set threshold = states('input_number.power_threshold')|float %}
            {{states.sensor
             |selectattr('entity_id', 'search', '_actueel')|map(attribute='state')
             |map('float',default=0)
             |select('>',threshold)
             |map(attribute='entity_id')
             |list}}

wont do this,

ok, getting closer:

{% set threshold = states('input_number.power_threshold')|float %}
{% for s in states.sensor
 |selectattr('entity_id', 'search', '_actueel')
 if s.state|float > threshold %}
{{s.entity_id}}
{% endfor %}

fear I need another namespace template though, because above wont render a correct list. right, finally I made it…

        filter:
          template: >
            {% set threshold = states('input_number.power_threshold')|float %}
            {% set ns = namespace(above_threshold=[]) %}
            {% for s in states.sensor
             |selectattr('entity_id', 'search', '_actueel')
             if s.state|float > threshold %}
            {% set ns.above_threshold = ns.above_threshold + [s.entity_id] %}
            {% endfor %}
            {{ns.above_threshold}}
1 Like