Notify on every device with battery becoming unavailable because of empty battery

Hi guys,
I like to generate an automation with a template trigger for every device with a battery when one becomes unavailable.
This should fire a notification with the name of the device :slight_smile:

Any ideas to solve this?

I started with this, to generate the trigger.


{{ states.sensor | selectattr('state', 'eq', 'unavailable') 
                 | selectattr('entity_id', 'match', '.*battery*')
                 | map(attribute='entity_id')
                 | list }}

Perhaps something like this?

{% set devices = states.sensor | selectattr('state', 'eq', 'unavailable') 
                 | selectattr('entity_id', 'match', '.*battery*')
                 | map(attribute='entity_id')
                 | list %}
{% for device in devices %}
{{ device_attr(device, "name_by_user") }}
{%- endfor %}