What is the value of this sensor supposed to be? Youāve set a value that Iām not sure what it does for battery in the if and elif but not the else. Then you have several seemingly unrelated if and elif statements that produce no value in the else portion. Iām really not sure what youāre trying to do with this template?
OK Iām tiredā¦, I worked out what youāre doing. Youāve just gone about it in an unusual manner (to me anyway).
This is my low battery template binary sensor maybe you can use it to figure out what youāre trying to do. Iāve based mine on the battery device class. You can set the device class for entities that arenāt automatically set in the customize section if necessary (I had to for my Nest devices).
The state is on when any battery goes below the set threshold. The entities attribute is a list of entity idās of the entities with low batteries. I filter out our mobile devices because I have a separate alert for those. āReplaceā is the state of a Nest device with a low battery.
Low Battery Template Sensor
binary_sensor:
- platform: template
sensors:
low_battery_alert:
friendly_name: Low Battery
unique_id: low_battery_alert
icon_template: "{{ 'mdi:battery-alert' if is_state('binary_sensor.low_battery_alert','on') else 'mdi:battery-90' }}"
value_template: >
{% set low_batteries = namespace(value=0) %}
{% set batteries = states|selectattr('attributes.device_class','eq','battery')|rejectattr('attributes.mobile','eq',true)|map(attribute='entity_id')|list %}
{% for item in batteries %}
{% if states(item) == 'Replace' or (states(item)|int(-1) != -1 and states(item)|int < states('input_number.battery_alert_threshold')|int) %}
{% set low_batteries.value = low_batteries.value + 1 %}
{% endif %}
{% endfor %}
{{ low_batteries.value|int > 0 }}
attribute_templates:
entities: >
{% set low_batteries = namespace(value=[]) %}
{% set low_batts = states|selectattr('attributes.device_class','eq','battery')|rejectattr('attributes.mobile','eq',true)|map(attribute='entity_id')|list %}
{% for item in low_batts %}
{% if states(item) == 'Replace' or (states(item)|int(-1) != -1 and states(item)|int < states('input_number.battery_alert_threshold')|int) %}
{% set low_batteries.value = low_batteries.value + [item] %}
{% endif %}
{% endfor %}
{{ low_batteries.value }}
with a little correction (the battery level is in the attribute, not in the state of the sensor) it works now as expected. here is the corrected version:
{%- set low_batteries = namespace(value='') %}
{%- set batteries = expand(states|selectattr('attributes.battery','ne',null)|map(attribute='entity_id')|list
+ states|selectattr('attributes.battery','ne',null)|map(attribute='entity_id')|list) -%}
{%- for battery in batteries %}
{%- if battery.attributes.battery != 'High' and battery.attributes.battery|float(-1) < 2.2 -%}
{%- if "LOWBAT" in battery.attributes.friendly_name -%}
{%- elif "TEMPERATURE" in battery.attributes.friendly_name -%}
{%- elif "HUMIDITY" in battery.attributes.friendly_name -%}
{%- elif "COUNTER" in battery.attributes.friendly_name -%}
{%- elif "POWER" in battery.attributes.friendly_name -%}
{%- else -%}
{%- set low_batteries.value = low_batteries.value ~ battery.attributes.friendly_name ~ '\n' -%}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{{ low_batteries.value }}