The low_batt_list variable you created contains a list of entity_ids.
You’re using for_each to iterate through that list of entity_ids.
The for_each's repeat.item contains an entity_id and nothing more. Therefore you can’t use repeat.item.attributes.friendly_name to get the entity’s friendly_name because all that repeat.itme contains is an entity_id like sensor.whatever.
You would have to use the expand filter to get all of the entity_id’s details.
The error message is correct, a list doesn’t have a split method.
The first example just needed expand(sensors) in low_batt_list whereas the second one contains multiple syntax errors. Are you scrapping the first example entirely and now intend to use the dramatically different second example?
Hi @123, sticking with the 1st example (am just trying to figure this out). Realise now that should have had expand(sensors) in low_batt_list as you suggested.
So now have:
- alias: device_low_batt
trigger:
- platform: time
at: '19:00:00'
mode: parallel
action:
- variables:
# List of devices with low battery
low_batt_list: >-
{% set sensors = states.sensor | selectattr('attributes.device_class', 'defined')
| selectattr('attributes.state_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| rejectattr('state', 'in', ['unavailable', 'unknown']) | list %}
{{ expand (sensors) | selectattr("state", "is_number")
| selectattr("state", "le", "20")
| rejectattr("state", "==", "100")
| rejectattr("entity_id", "search", ".*_zwave")
| rejectattr("entity_id", "search", ".*iphone*")
| map(attribute="entity_id") | list }}
- choose:
- conditions: '{{ low_batt_list | length > 0 }}'
sequence:
- repeat:
for_each: '{{ low_batt_list }}'
sequence:
- service: persistent_notification.create
data_template:
title: Replace Battery
message: The {{ repeat.item.attributes.friendly_name.split(" battery level")[0] }} sensor battery is at {{ repeat.item.state }}%, the battery type is {{ repeat.item.attributes.battery_type }}.
notification_id: '{{ repeat.item.split(".")[1].split("_battery_level")[0] }}_low_batt_notification'
Am still getting this error in the repeat for each step: Error: Error rendering data template: UndefinedError: 'str object' has no attribute 'attributes'
I had to make some changes because the for_each didn’t accept what I had originally proposed. This version expands each repeat.item individually so it can get to the entity’s name, state, and battery_type attribute.
Hmmm, I tried that, but it doesn’t seem to make any difference.
I think it’s the template, the current state of sensor.cat_flap_battery_level is 4 (%)
If I change this line…
| selectattr("state", "le", "20")
…in the template to “40”…
| selectattr("state", "le", "40")
…then the sensor is included in the result. That kind of defeats the purpose though as I’m looking for a list of sensors with less than 20% battery remaining.
I’m not sure why the state of sensor.cat_flap_battery_level is 4 but is being interpreted by the template as 40.