I want to create a list that contains the name and the battery level of all the entities that have batteries and the battery level is below 90.
For this, I made a loop that accumulates the entities that match the condition to a list. I checked it in the “template editor” in the developer tools and it works as expected.
However, in the automation, the list variable returns an empty value. Even so, the list variable (ns.devices) is defined as “namespace”.
It seems the list variable (ns.devices) behaves like a local variable in the automation. below is the code from the automation.
- Core: 2025.5.0
- Supervisor: 2025.04.1
- Frontend: 20250507.0
variables:
low_battery_devices: |
{% set list = [['aa','bb']] %}
{% for s in states
if s.attributes.device_class == 'battery'
and s.state is not none
and s.state not in ['unknown', 'unavailable']
and s.state | regex_match('^\d+(\.\d+)?$')
and (s.state | float) < 90 %}
{% set name = s.attributes.friendly_name if 'friendly_name' in s.attributes else s.entity_id %}
{% set percent = s.state | float(0) | round(0) | int %}
{% set list = list + [name ~ ' (' ~ percent ~ '%)'] %}
{% endfor %}
{{ list }}