Jinja2 alert sensor below 15% battery level

Hello to all,

I’m trying to add a sensor to detect devices with less than 15% battery.
For that I use the jinja2 syntax.

I created a group with my sensors :


Thermo_batterie: 
  entities:
    - sensor.capteur_temp_salon_battery
    - sensor.capteur_temp_bureau_francois_battery
    - sensor.capteur_temp_sdb_battery
    - sensor.capteur_temp_chambre_battery
    - sensor.capteur_temp_bureau_elsa_battery
    - sensor.capteur_temp_buanderie_battery

And jinga function :

    batterie_alerte:
      friendly_name: "Alerte batterie"
      value_template: >- 
          {% set thermo = 
          states 
          | selectattr('entity_id', 'in', state_attr('group.thermo_batterie','entity_id'))|selectattr('state', 'lt', '15' ) | map(attribute='name')| list %}
          {%- if thermo | length == 0 -%}
            OK
           {%- else -%}
           Pile thermo - de 15% : {{ thermo[:-1] | join(', ') }}{{', ' if thermo | length > 2 else ' '}}et {{ thermo[-1]}}
           {%- endif -%}

This function gives me erroneous information (sensor with 100% of battery which goes up in spite of the " < 15 ")

image

The most surprising is that when I change the attribute = name by state, it is well displayed at 100

batterie_alerte:
      friendly_name: "Alerte batterie"
      value_template: >- 
          {% set thermo = 
          states 
          | selectattr('entity_id', 'in', state_attr('group.thermo_batterie','entity_id'))|selectattr('state', 'lt', '15' ) | map(attribute='state')| list %}
          {%- if thermo | length == 0 -%}
            OK
           {%- else -%}
           Pile thermo - de 15% : {{ thermo[:-1] | join(', ') }}{{', ' if thermo | length > 2 else ' '}}et {{ thermo[-1]}}
           {%- endif -%}

Result without attribute filter :

batterie_alerte: friendly_name: "Alerte batterie" value_template: >- Pile thermo - de 15% : <template TemplateState(<state sensor.capteur_temp_buanderie_battery=90; state_class=measurement, battery=90, humidity=77.98, linkquality=191, power_outage_count=11, pressure=1009.1, temperature=13.71, voltage=2985, unit_of_measurement=%, device_class=battery, friendly_name=capteur_temp_buanderie_battery @ 2023-01-07T00:57:01.851115+01:00>)>, <template TemplateState(<state sensor.capteur_temp_bureau_elsa_battery=97; state_class=measurement, battery=97, humidity=61.75, linkquality=255, power_outage_count=8, pressure=1009.6, temperature=17.37, voltage=2995, unit_of_measurement=%, device_class=battery, friendly_name=capteur_temp_bureau_elsa_battery @ 2023-01-06T14:27:48.198103+01:00>)>, <template TemplateState(<state sensor.capteur_temp_bureau_francois_battery=90; state_class=measurement, battery=90, humidity=62.04, linkquality=255, power_outage_count=5, pressure=1009.3, temperature=18.48, voltage=2985, unit_of_measurement=%, device_class=battery, friendly_name=capteur_temp_bureau_francois_battery @ 2023-01-06T14:27:48.264427+01:00>)>, <template TemplateState(<state sensor.capteur_temp_chambre_battery=100; state_class=measurement, battery=100, humidity=62.94, linkquality=207, power_outage_count=18, pressure=1007.9, temperature=17.53, voltage=3015, unit_of_measurement=%, device_class=battery, friendly_name=Chambre @ 2023-01-06T14:27:48.146425+01:00>)>, <template TemplateState(<state sensor.capteur_temp_salon_battery=100; state_class=measurement, battery=100, humidity=64.05, linkquality=255, power_outage_count=57, pressure=1007.8, temperature=17.75, voltage=3015, unit_of_measurement=%, device_class=battery, friendly_name=Salon @ 2023-01-07T01:14:47.007563+01:00>)>, et <template TemplateState(<state sensor.capteur_temp_sdb_battery=100; state_class=measurement, battery=100, humidity=60.15, linkquality=255, power_outage_count=5, pressure=1009.2, temperature=19.15, voltage=3005, unit_of_measurement=%, device_class=battery, friendly_name=Salle de bain @ 2023-01-06T14:27:48.194355+01:00>)

Thank’s for help !

The issue is that you are calculating on strings… I donot have a quick solution for the above but another solution I use

{% set result = namespace(sensors=[]) %} 
{% for state in states.sensor | selectattr('name', 'search', 'battery') %}
{% if 0 <= state.state | int(-1) < 88 | int %}
{% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
{% endif %}
{% endfor %} 
{{result.sensors|join(', ')}}

Or use what Classicred proposes :slight_smile:

    batterie_alerte:
      friendly_name: "Alerte batterie"
      value_template: >- 
        {% set ns = namespace(thermo = []) %}
        {% for x in expand('group.thermo_batterie') if x.state | int(100) < 15 %}
          {% set ns.thermo = ns.thermo + [x.name] %}
        {% endfor %}
        {% if ns.thermo | count == 0 %}
          OK
        {%- else -%}
          Pile thermo - de 15% : {{ ' et '.join((ns.thermo | join(', ')).rsplit(', ', 1)) }}
        {%- endif -%}

Perfect thank you !

1 Like