Hello all,
Could a friendly person be so kind and help me convert this code to support a fixed state “on”, and not a numeric state referred to in top "(the input helper). I would like to use the same template code for alarm sensors (on/off) rather than for numeric battery level sensors, but after 5 hours of attempting to learn templates, I have to give up for now
Also, there is no need for the threshold state as this refers to the input helper state.
{% set threshold = states('input_number.helper_low_battery') | int %}
{%- set ns = namespace(sensors=[]) -%}
{%- for state in states.sensor
| selectattr('attributes.device_class', 'defined')
| selectattr('attributes.state_class', 'defined')
| selectattr('attributes.device_class', '==', 'battery')
| selectattr('attributes.state_class', '==', 'measurement')
| selectattr('state', 'is_number') -%}
{%- if state.state | int <= threshold -%}
{% set ns.sensors = ns.sensors + [dict(name = state.name | replace(' battery', '') | replace(' Battery', ''), state = state.state | int)] %}
{%- endif -%}
{%- endfor -%}
{%- set batt = ns.sensors | sort(attribute='state') %}
{%- set ns = namespace(batt='') -%}
{%- for state in batt -%}
{% set ns.batt= ns.batt + (state.name ~ ' (' ~ state.state ~'%)' ~ "\n") %}
{%- endfor -%}
{% if ns.batt | count > 0 %}
{{ ns.batt | truncate(255, true, '...') }}
{% else %}
{{ 'unavailable' }}
{% endif %}