Hi,
This is a solution that is helping me track the online status of all ESPHome devices (or anything with a status sensor) and I get a notification when anything goes offline. Here is my set up.
- Step-1: created a group for all the sensors that needs to be tracked:
# Boards that are connected and needs to be monitored
all_monitored_boards:
name: All boards monitored for online status
entities:
#alarm
- binary_sensor.alarm_a_status
- binary_sensor.alarm_b_status
- binary_sensor.alarm_c_status
- binary_sensor.alarm_d_status
# Sirens
- binary_sensor.d1_siren_1_living_status
- binary_sensor.d1_siren_2_garage_status
- binary_sensor.d1_siren_3_loft_status
# Appliances
- binary_sensor.switchs3101_status
- binary_sensor.switchs3102_status
- binary_sensor.switchs3103_status
- Step-2: Created an integration that tracks the offline boards:
# Moniroring boards for offline or online status
template:
- sensor:
- name: All Offline Boards
icon: >-
{{ 'mdi:check-circle' if is_state('sensor.all_offline_boards','0') else 'mdi:alert-circle' }}
state: >-
{{ expand('group.all_monitored_boards') | selectattr('state', 'eq', 'off') | list | count }}
attributes:
entities: >-
{{ expand('group.all_monitored_boards') | selectattr('state', 'eq', 'off') | map(attribute='name') | list }}
- Step-3: Created a script to generate details of offline boards in the outgoing message:
# Lists all the offline boards and send a notification to PM
offline_boards:
alias: "Script: Offline Boards Notification - SMS"
sequence:
- service: notify.sms_notification_pankaj
data:
title: HA Boards Offline Alert!
message: >-
{% set offline = expand('sensor.all_offline_boards') | selectattr('state', 'eq', 'off') | map(attribute='name') | list | join(", ") %}
{% set x = ', and ' if offline.split(', ') | count > 2 else ' and ' %}
{{x.join(offline.rsplit(', ', 1))}} {{'are' if offline | count > 1 else 'is' }} offline right now.
- Step-4: Automate the script:
# Send notification to PM when a board goes offline
- id: '1643608574886'
alias: 'Automation: Offline Boards Notification - SMS'
description: Send a text notification to PM if any of the boards goes offline
trigger:
- platform: state
entity_id: group.all_monitored_boards
to: 'off'
for:
hours: 0
minutes: 2
seconds: 0
condition: []
action:
- repeat:
count: '2'
sequence: []
- service: script.offline_boards
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
mode: single
It is working till this point in time, so pulled out power for few boards and got the notification (with baord names) that went offline.
Now I am looking for v2.0 improvement i.e. get the notification when the offline board comes back online. But I am struggling to get the previous state of the sensor (i.e. sensor.all_offline_boards) once the board comes online.
Any pointers will be appreciated!