Bonjour, j’ai un petit soucis de template pour un binary sensor.
Je voudrai créer un binary sensor pour les batteries de mes capteurs. Quand une des batteries est en dessous de 15, le binary s’active.
A priori mon code ne fonctionne pas bien.
Merci pour votre aide.
Renseigner votre problème
TemplateError('ValueError: Template error: float got invalid input ‹ unknown › when rendering template
Error while processing template:
Ma configuration
- binary_sensor:
- name: "Niveau Batterie"
unique_id: b3bcee3c-b72d-11ec-b909-0242ac120002
device_class: "battery"
state: >-
{{ states('sensor.chambre_interrupteur_battery')|float < 15
or states('sensor.chambre_interrupteur_fabien_battery')|float < 15
or states('sensor.chambre_interrupteur_sophie_battery')|float < 15
or states('sensor.salle_de_bains_interrupteur_battery')|float < 15
or states('sensor.salle_a_manger_interrupteur_battery')|float < 15
or states('sensor.salon_interrupteur_battery.')|float < 15
or states('sensor.door_and_window_sensor_battery_level')|float < 15
or states('sensor.door_and_window_sensor_battery_level_2')|float < 15
or states('sensor.door_and_window_sensor_battery_level_3')|float < 15
or states('sensor.door_and_window_sensor_battery_level_4')|float < 15
or states('sensor.door_opening_sensor_battery_level_2')|float < 15
or states('sensor.door_opening_sensor_battery_level')|float < 15
or states('sensor.netatmo_domicile_salon_chambre_battery_percent')|float < 15
or states('sensor.netatmo_domicile_salon_exterieur_battery_percent')|float < 15
or states('sensor.motion_sensor_battery_level')|float < 15
or states('sensor.motion_sensor_battery_level_2')|float < 15
or states('sensor.motion_sensor_battery_level_3')|float < 15
or states('sensor.mouvement_nuit_battery_level_4')|float < 15
or states('sensor.keypad_with_alarm_activation_rfid_tag_battery_level')|float < 15
}}
Comme déjà évoqué sur le forum, attention aux valeurs de % batterie, pour mes capteurs que j’ai depuis plus d’un an voici l’affichage.
Zigbee je précise, autres techno je ne sais pas.
Je confirme et perso j’ai du rfxcom, zwave et du zigbee, ce que je constate :
rfx mes sondes de t° (oregon) vont passer de 100% à 0% quasi instantanément.
zwave (zwavjs2mqtt / des capteurs aeotec et fibaro) vont descendre progressivement, mais en dessous de 30% je suis incapable d’anticiper le moment où le capteur va cesser de fonctionner.
zigbee (zha + capteur aqara) alors là c’est juste n’imp, d’un restart de HA à l’autre je peux avoir des valeurs assez différentes. Au final je sais que je dois changer une pile quand une courbe de t° devient « plate » sur plusieurs heures.
Perso j’utilise une notification à heure fixe me précisant les appareils concernés via un template pour ne pas avoir à les rajouter à la liste à chaque fois que j’en rajoute un.
automation:
- alias: battery_alert
trigger:
- platform: state
entity_id: input_boolean.testlvlbaterie
to: 'on'
- platform: time
at: '20:45:00'
condition:
- condition: template
value_template: >
{% macro battery_level() %}
{%- set threshold = 30 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() |trim != "" }}
action:
- service: notify.iosdevice
data_template:
title: "Batterie faible !"
message: >
{% macro battery_level() %}
{%- set threshold = 30 -%}
{% set domains = ['light', 'switch', 'sensor', 'zwave', 'lock'] %}
{% for domain in domains -%}
{% for item in states[domain] if ((item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) or ("battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown"))) -%}
{% if (item.attributes.battery_level is defined and item.attributes['battery_level'] | int < threshold) -%}
{{ item.name }} ({{ item.attributes['battery_level'] }}){%- if not loop.last %}, {% endif -%}{% endif -%}
{% if "battery" in item.name | lower and ((item.state | int < threshold and item.state|int != 0) or item.state | lower == "low" or item.state | lower == "unknown") -%}
{{ item.name }} ({{ item.state }}){% if not loop.last %}, {%- endif %} {% endif -%}
{% endfor %}
{%- endfor %}
{% endmacro %}
{{ battery_level() }}
data:
push:
thread-id: "alerte_temp-group"
Tu peux certainement le réutiliser pour ton binary_sensor