Help to convert this sensor template code

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 :frowning:

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 = | 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.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.

Hi Taras,

Thanks for helping and your advise.

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.

  {{ states.binary_sensor
    | selectattr('attributes.device_class', 'defined') 
    | selectattr('attributes.device_class', 'in', ['door', 'window']) 
    | selectattr('state', 'eq', 'on') 
    | map(attribute='name')
    | list | join('\n') }}

We’ll get to formatting its appearance in a Markdown card after we’ve established that the template is reporting the information you want.

This code works perfect.

Glad to hear it meets your requirements!

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.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

Hi Taras

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.

Br Daniel

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.

Do you think it’s too hard getting to this?

Obviously, I really appreciate your assiatance :pray:

So what do you want to display in parentheses?