ESPHome devices status - need help with improvement

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.

  1. 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
  1. 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 }}
  1. 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.
       
  1. 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!

Check out THIS template sensor. It does exactly that. I get notification when device goes online and also when all monitored devices are back online - for this i added additional automation to above pack. Note that difference between automation when device is offline and “all are back online” is in the line:


below: "1"

while in automation for one or more offline devices it’s


"above "0"

my automation (2nd part):

- id: wifi_devices_online_notification
    alias: "WiFi devices ONLINE"
    description: "All modules are back online"
    mode: restart
    trigger:
      - platform: state
        entity_id: sensor.unavailable_wifi_devices
        for:
          hours: 0
          minutes: 2
          seconds: 0
    condition: []
    action:
      - choose:
          - conditions:
              - condition: numeric_state
                entity_id: sensor.unavailable_wifi_devices
                below: "1"
            sequence:
              - service: notify.mobile_app_sm_g950f
                data:
                  title: OFFLINE naprave
                  message: All devices are back online.
                  data:
                    ttl: 300
                    priority: high