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 %}
The template you posted is for finding battery sensors whose value is below a threshold value.
What you’re asking for (no threshold value, no battery sensors, etc) has nothing to do with this template … so it’s not the best basis for creating what you want.
You said you want to report all alarm sensors whose state is on. The template’s first step is to select only the alarm sensors. What do these sensor entities have in common that makes it easy to identify them as alarm sensors? For example, do they all have a common device_class value and/or a unique name format? Are they all part of the same integration?
If there’s no way to select them by their characteristics then you will have to list them in the template.
What is important is that the design output will be identical to the current code which has been inserted in my HA dashboard with this yaml code “content: ‘{{states(’‘sensor.low_battery_devices’’)}}’” in a markdown card. Through the new template I have modified a new sensor was created with the entity name “sensor.alarm_sensor_devices”
The sensors are all classified as “doors” or “windows”, and will be the only of this so easy to pull out.
Copy-paste the following template into the Template Editor and experiment with it. In its current form it should report the friendly_names of all open door and window binary_sensors.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.
Yes, this is great however, how do I combine this code with rest of the format in my original code, which will make this work in a markdown card? Sorry to ask this additional question.
What exactly do you want to retain from the appearance of the battery template? It seems to display the name of each sensor followed by its battery level in parentheses. That’s not applicable for your alarm sensors.
Hi, I understand why you ask. The two cards will sit in my dashboard on top of each other, and based on conditions, one card will be shown vs the other. As of this, the format (visuals) needs to be the same, and I’m struggling to keep it so, as it’s not presented the same way. Hence. if it could be using same template it would be nice.