This feels like it should be super simple and I just can’t get my head around how you would do this naturally in HA.
I want to have an item on my dashboard with an ha-alert with the number of sensors at low battery, with an optional attribute listing the devices and their levels. I had looked at the brilliant sbyx Blueprint as reference but want a persistent sensor value that runs regularly (even realtime if not too intensive?), rather than a one-off notification each day.
I got a version of the Jinja code working in the Developer Tools:
{% set threshold = 10 %}
{% set exclude = namespace(entity_id=[]) %}
{% set result = namespace(sensors=[]) %}
{%- for state in states.sensor | selectattr('attributes.device_class','defined') | selectattr('attributes.device_class', '==', 'battery') %}
{%- if 0 <= state.state | int(-1) < threshold | int and not state.entity_id in exclude.entity_id %}
{%- set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ '%)'] %}
{% endif %}
{% endfor %}
{% for state in states.binary_sensor | selectattr('attributes.device_class','defined') | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
{% if not state.entity_id in exclude.entity_id %}
{% set result.sensors = result.sensors + [state.name] %}
{% endif %}{% endfor %}
{{result.sensors|join(', ')}}
{{result.sensors|length}}
…but I’d like a sensor with the state as the count and an attribute as the full-length text. It seems relatively simple, but I just can’t wrap my head around the “HA Way” of doing this. Automations seems like the way I’d get it to run at a specific time, but I can’t just execute Jinja code. This is a template, but doesn’t get executed. A blueprint seems an inefficient way to do it and I’m not sure how it’d then populate a state and attribute and a helper only takes 255 characters!
How would an HA pro tackle this?
Thanks!