MQTT Devices Battery level, using for loop and if

Hi,

I am trying to get all sensor values with battery_level i figured out how to pull the battery with dev tools. But when I use IF I fail to make jinja2 work

Example:

For loop example:
{% for state in states.sensor if 'battery_level' in state.name -%}

{{state.name}} {{ state.attributes.value }}
------------

{%- endfor %}.

This lists all devices names (sensors) and battery Levels!

but this doesn’t work:

{%- if state.attributes.value < 40 %} Low battery {{ state.name }} {% endif %}

the state.attributes.keys() return

dict_keys(['value_id', 'node_id', 'class_id', 'type', 'genre', 'instance', 'index', 'label', 'units', 'help', 'read_only', 'write_only', 'min', 'max', 'is_polled', 'value', 'lastUpdate', 'unit_of_measurement', 'friendly_name', 'device_class'])

What Am i doing wrong?

Not sure if you already fixed it, but this works for me:

{% if state.state | int < 40 %} 
 Low battery {{ state.name }} 
{% endif %}