Help: Template: Map string to float

I have created a helper group called “HelperGroupSensorBatteryDriven” which contains all of my battery driven devices.
For testing purposes, based on that group, I’m trying to create a template to be used for later telegram notifications. The notification should include the battery state of all battery driven devices inside that group which have a battery state below 20%. In addition the notification should also include the “area_alias” (which is mapped within the “tools.jinja” file) for every according battery driven device.
This is the code I have for the template currently:

The following devices/areas have devices on low battery state: {{" \n "}} {% from
'tools.jinja' import area_alias %} {% set batterydrivendevices =
expand('sensor.helpergroupsensorbatterydriven') |
selectattr('state', 'lt', '20') | list %} {%- for batterydrivendevice in
batterydrivendevices %} {{-" \n -"}} {{ (batterydrivendevice.name) }}: {{
area_alias(area_name(batterydrivendevice.entity_id)) }} {%- endfor %}

This almost works, it does list the devices from the helper goup with the correct “area_alias” per device but the problem is that there are also devices being listed that are obove the 20% battery state. It seems that the “lowerthan” operation cannot handle string values the way I want it to be with this code:

selectattr('state', 'lt', '20')

How would the correct code look like to map the ‘state’ string value into a float value so that the “lowerthan” operation works as intended?

Thanks for any help in advance!

This post might give you a solution: Jinja / Template Sensor Question

Thanks, using this code for a script did the trick:

service: telegram_bot.send_message
data:
  message: >
    The following devices are on low battery state (less than
    20%):{{-"\n"-}}{{-"\n"-}} {% from 'tools.jinja' import area_alias %}{% set
    entities = expand('sensor.helpergroupsensorbatterydriven') %}{% for x in
    entities if x.state|int < 20 %}{%- if not loop.first %}{% endif -%}
      {{- " - " + x.name | replace('Battery','') | replace('battery','') -}}
      {{- "(" -}}
      {{- area_alias(area_name(x.entity_id)) -}}
      {{- ")" -}}
      {{- "\n" -}}
    {% endfor %} {{- "\n" -}}
    Please change batteries as soon as possible!
  target: -XXXXXXXX
enabled: true