Notify when devices are online again

Hi all,
I use this automation which notifies me when devices are offline for 5 minutes.

alias: Unavailable devices
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.sonoff_1000a599d5
      - switch.sonoff_1000a19285
      - switch.sonoff_1000a02d17
      - switch.sonoff_1000a09286
      - switch.sonoff_1000f19f91
      - switch.sonoff_1000a189ef
      - switch.sonoff_1000f98edd
    to: unavailable
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: notify.mobile_app_sm_g996b
    data:
      title: Unavailable devices
      message: >
        {{ state_attr(trigger.to_state.entity_id, 'friendly_name') }} is offline
        for 5 min!
mode: parallel
max: 50

I would like to create another automation which:

  1. Notifies me when device is online again and eliminates short-term unavailable states. It sometimes happens that a device is unavailable for several seconds and then goes online again. These I would like to ignore. It means to notify me when devices are unavailable for 5 minutes and then go online again.
  2. Then I would like to replace the list of entities by a group of entities (approx. 50 devices). But then I don’t know how to set the template to be able to notify me with the specific name of a device.

Thanks for help.

1 Like

@Tomm206
I use the Ping service to monitor all my devices. Below you’ll find my code for OFFLINE and ONLINE events for my mission critical devices. If you have any further questions about my code, just ask.

##########################################################
## Notify if a critical device goes offline
##########################################################
- id: a_critical_device_goes_offline
  alias: A Critical Device goes Offline
  initial_state: true
  mode: restart
  trigger:
    - platform: state
      entity_id: binary_sensor.ping_cloud_key
      to: 'off'
      for:
        minutes: 1
    - platform: state
      entity_id: binary_sensor.ping_vera_secure
      to: 'off'
      for:
        minutes: 1
    - platform: state
      entity_id: binary_sensor.ping_xfinity_router
      to: 'off'
      for:
        minutes: 1
  action:
## Event Log start
  - service: script.turn_on
    entity_id: script.notify_logs_event
    data_template:
      variables:
        var_type: "automation.a_critical_device_goes_offline"
        var1: "Trigger {{ trigger.to_state.attributes.friendly_name }}"
        var2: "{{ trigger.to_state.attributes.friendly_name }} has gone OFFLINE."
## Event Log End
  - service: script.turn_on
    entity_id: script.notify_hass_a_critical_device_goes_offline
    data:
      variables:
        var_device_name: "{{ trigger.to_state.attributes.friendly_name }}"
  - service: script.turn_on
    entity_id: script.notify_push_a_critical_device_goes_offline
    data:
      variables:
        var_device_name: "{{ trigger.to_state.attributes.friendly_name }}"
#
##########################################################
## Notify when a critical device returns online
##########################################################
- id: a_critical_device_returns_online
  alias: A Critical Device returns Online
  initial_state: true
  mode: restart
  trigger:
    - platform: state
      entity_id: binary_sensor.ping_cloud_key
      to: 'on'
      for:
        minutes: 1
    - platform: state
      entity_id: binary_sensor.ping_vera_secure
      to: 'on'
      for:
        minutes: 1
    - platform: state
      entity_id: binary_sensor.ping_xfinity_router
      to: 'on'
      for:
        minutes: 1
  action:
## Event Log start
  - service: script.turn_on
    entity_id: script.notify_logs_event
    data_template:
      variables:
        var_type: "automation.a_critical_device_returns_online"
        var1: "Trigger {{ trigger.to_state.attributes.friendly_name }}"
        var2: "{{ trigger.to_state.attributes.friendly_name }} has returned ONLINE."
### Event Log End
  - service: script.turn_on
    entity_id: script.notify_hass_a_critical_device_returns_online
    data:
      variables:
        var_device_name: "{{ trigger.to_state.attributes.friendly_name }}"
  - service: script.turn_on
    entity_id: script.notify_push_a_critical_device_returns_online
    data:
      variables:
        var_device_name: "{{ trigger.to_state.attributes.friendly_name }}"
#

You may notice that the times are very short. This was by design, as I wanted a much quicker response for these devices. All other online devices are on a 10 minute duration (bulbs, tvs and other non critical devices).