Hi there,
I am trying to create an alert that pops up when a battery powered sensor needs attention.
I used a naming convention with all my zigbee sensors:
example: the kitchen motion sensor has 3 entities, 1 for each sub-sensor:
binary_sensor.kitchen_motion_sensor
sensor.kitchen_motion_sensor_battery_level
sensor.kitchen_motion_sensor_temperature
The strict naming convention enabled me to create many useful automations but I am struggling with creating an alert that pops up whenever a sensor becomes unavailable or not responsive.
I managed to create a template with all the relevant information calculated:
{% set vars = namespace(
numberOfBatteryDevices = 0,
numberOfEmptyBatteryDevices = 0,
emptyBatteryDevices = '',
numberOfNonResponsiveDevices = 0,
nonResponsiveDevices = '',
numberOfLowBatteryDevices = 0,
lowBatteryDevices = '',
goodBatteryDevices = ''
) -%}
{%- for curr_entity in (states |selectattr('entity_id')| list) -%}
{%- if curr_entity.entity_id.endswith('_battery_level') -%}
{%- set _batteryDevice=curr_entity.entity_id -%}
{%- set _sensorName= curr_entity.entity_id.split('.')[1].split('_battery_level')[0] -%}
{%- set _batteryLevel = states[_batteryDevice].state -%}
{%- set _temperatureSensor = 'sensor.'~_sensorName~'_temperature' -%}
{%- set _lastChanged = states[_temperatureSensor].last_changed -%}
{%- if _lastChanged!=null -%} {#probably a phone, not a sensor!#}
{%- set vars.numberOfBatteryDevices = vars.numberOfBatteryDevices+ 1-%}
{%- set _hoursPassed = (((as_timestamp(now()) - as_timestamp(_lastChanged)) | int) % 86400)//3600 %}
{%- if _batteryLevel=='unavailable' -%}
{%- set vars.numberOfEmptyBatteryDevices = vars.numberOfEmptyBatteryDevices+ 1 -%}
{%- set vars.emptyBatteryDevices = vars.emptyBatteryDevices~',' if vars.emptyBatteryDevices!='' -%}
{%- set vars.emptyBatteryDevices = vars.emptyBatteryDevices~_sensorName -%}
{%- elif _hoursPassed>=2 -%}
{%- set vars.numberOfNonResponsiveDevices = vars.numberOfNonResponsiveDevices+ 1 -%}
{%- set vars.nonResponsiveDevices = vars.nonResponsiveDevices~',' if vars.nonResponsiveDevices!='' -%}
{%- set vars.nonResponsiveDevices = vars.nonResponsiveDevices~_sensorName -%}
{%- elif _batteryLevel|int <40 -%}
{%- set vars.numberOfLowBatteryDevices = vars.numberOfLowBatteryDevices+ 1 -%}
{%- set vars.lowBatteryDevices = vars.lowBatteryDevices~',' if vars.lowBatteryDevices!='' -%}
{%- set vars.lowBatteryDevices = vars.lowBatteryDevices~_sensorName -%}
{%- else -%}
{%- set vars.goodBatteryDevices = vars.goodBatteryDevices~',' if vars.goodBatteryDevices!='' -%}
{%- set vars.goodBatteryDevices = vars.goodBatteryDevices~_sensorName -%}
{%- endif -%}
{#'battery device:'~ _batteryDevice}}
{{'sensor name:'~ _sensorName }}
{{'battery level:'~ _batteryLevel }}
{{'temperature sensor:'~ _temperatureSensor}}
{{'last changed:'~ _hoursPassed~'|'~_lastChanged #}
{%- endif -%}
{%- endif %}
{%- endfor -%}
{%- set _totalBatteryIssues = vars.numberOfEmptyBatteryDevices + vars.numberOfNonResponsiveDevices %}
Number Of Battery Devices: {{vars.numberOfBatteryDevices}}
Number Of Empty Battery Devices: {{vars.numberOfEmptyBatteryDevices}}
Empty Battery Devices: {{vars.emptyBatteryDevices}}
Number Of Non Responsive Devices: {{vars.numberOfNonResponsiveDevices}}
Non Responsive Devices: {{vars.nonResponsiveDevices}}
Number Of Low Battery Devices: {{vars.numberOfLowBatteryDevices}}
Low Battery Devices: {{vars.lowBatteryDevices}}
Total Battery Issues: {{ _totalBatteryIssues }}
Good Battery Devices: {{vars.goodBatteryDevices}}
The output is as follows:
Number Of Battery Devices: 17
Number Of Empty Battery Devices: 4
Empty Battery Devices: bedroom_bath_button,closet_room_motion_sensor,kids_room_motion_sensor,main_yard_button
Number Of Non Responsive Devices: 2
Non Responsive Devices: living_room_button,main_bath_motion_sensor
Number Of Low Battery Devices: 1
Low Battery Devices: bedroom_button
Total Battery Issues: 6
Good Battery Devices: bedroom_bath_motion_sensor,bedroom_motion_sensor,den_motion_sensor,front_door_sensor,kitchen_motion_sensor,kitchen_yard_left_door_sensor,kitchen_yard_right_door_sensor,living_room_back_motion_sensor,main_yard_door_sensor,vault_sensor
Can someone help me to create a binary_sensor
using this template that is on if _totalBatteryIssues>0
but also stores a list of the sensors that I created in the template somehow.
If we had support for global variables, this would have been easy, but since we don’t, I can’t think of a good way to manage this information.
Any ideas?