Notify when a device goes offline

Hello community, is there any trigger I can use with my (already configured) pushover notification system?

I would like to receive a notification when my Yeelight bulbs or Shelly devices goes offline, but I don’t know which trigger I can use.

Thank you
Lucas

1 Like

You should be able to use the state trigger, as such:

trigger:
- platform: state
  entity_id: light.example #Insert your Entity here
  to: unavailable

I hope that helps :slight_smile:

2 Likes

Thank you, but I have tons of yeelight and shelly devices and I have to do an automation for each of them. Is here any other way to check unavailability of all the devices in one automation? Like ping every 5 minutes and then notify if a specific device doesn’t reply (the ping it’s just an example :slight_smile: )

Not to sure, did a quick google and found this yaml which may help.
Unavailable Sensor Detection and Notification (github.com)

Here is a link to the thread I found it from: Sensor - Unavailable/Offline Detection - Share your Projects! - Home Assistant Community (home-assistant.io)

I personally do not detect when devices go offline, as it happens far to often, but that is do to poor WIFI which will soon be replaced with a better Access Point.

I have now Netgear Orbi RBR50 with 2 satellites and in fact I have more stable WiFi connections compared with old Asus AP. Till now I got no device offline, but just in case I would like to be prepared. Anyway, thank you for your search, I’ll try to implement an unavailable sensor

Hey there. That unavailable sensor gist is mine. I put out an updated version recently and it didn’t work well. Just wanted to make sure you try the right one - the group version. It is the one linked above. I forgot to update the gist until I saw this thread.

Anyway, you should be able to just drop the package in and go! Let me know if you have any issues.

1 Like

You can use the suggestion from the second post and just add all your entities you want to track to the list, no need for ine automation per device.

E.g. from my system

# Notify on Device Offline
  - id: notify_on_device_offline
    alias: "Benachrichtigung bei Gerät offline"
    mode: parallel
    trigger:
      - platform: state
        entity_id:
          - sensor.battery_level_remote_light_livingroom
          - sensor.battery_level_remote_light_dressroom
          - sensor.battery_level_remote_light_bedroom
          - sensor.battery_level_remote_light_office
          - sensor.battery_level_remote_sabrina
          - sensor.battery_level_remote_dimitri
          - binary_sensor.motion_livingroom
          - binary_sensor.motion_dressroom
          - binary_sensor.motion_bedroom
          - binary_sensor.motion_office
          - sensor.temperature_bathroomsmall
          - sensor.temperature_livingroom
          - sensor.temperature_storageroom
          - sensor.lux_outside
          - sensor.lux_office
          - binary_sensor.door_bathroomlarge
          - binary_sensor.door_bathroomsmall
          - binary_sensor.door_office
          - binary_sensor.door_bedroom
          - binary_sensor.door_dressroom
          - binary_sensor.door_storageroom
          - binary_sensor.door_kitchen
          - binary_sensor.door_livingroom
          - binary_sensor.door_main
          - binary_sensor.window_bathroomlarge
          - binary_sensor.window_office
          - binary_sensor.window_dressroom
          - binary_sensor.window_bedroom
          - binary_sensor.window_kitchen
          - binary_sensor.window_livingroom
          - binary_sensor.water_leak_bathroomsmall
          - binary_sensor.water_leak_bathroomlarge
          - binary_sensor.water_leak_kitchen
          - binary_sensor.water_leak_storageroom
        to: "unavailable"
        for:
          minutes: 5
    action:
      - service: notify.mobile_app_phone_dimitri
        data:
          title: "Device offline!"
          message: >
            📵 {{ state_attr(trigger.to_state.entity_id, 'friendly_name') }} is offline more than 5 minutes!
          data:
            channel: Notfall
            priority: high
            ttl: 0
            color: red
19 Likes

That’s great and simple, thank you. I added to the list all my Yeelight and Shelly devices, it works perfect!

1 Like

Where would this code go? Configuration.yml?

It’s an automation :wink:

So obviously the code goes into automations.yaml

Thank you. I’m still super green on editing the yml files vs using the GUI.

I created a more generic method. Not perfect, but better than adding each device to an automation:

  1. Create a python script “config/python_scripts/device_offline_notification.py” with the following content:
# add here device ids you wan't to ignore
ignore_entities=[
  "media_player.821378bb_0f1e4b8e",
  "light.821378bb_0f1e4b8e"
]

states=hass.states.all()
offline_devices=[]
for state in states:
  if not state.entity_id in ignore_entities:
    if state.state == "unavailable":
      offline_devices.append(state.entity_id);

if len(offline_devices) > 0:
  message = "\n".join(offline_devices)
  logger.warn(message)

  # in my example, i send a notification via telegram bot
  hass.services.call("notify", "max", {
    "title": "Devices offline",
    "message": message.replace("_", "\_")
  })
  1. Create an automation that executes the script:
  alias: Notify Device offline
  description: ''
  trigger:
  - platform: time
    at: '18:00'
  condition: []
  action:
  - service: python_script.device_offline_notification
  mode: single
4 Likes

That’s not really an alternative in my opinion, because it only runs once a day, so it would be possible that your device could in the worst case be offline for almost 24 hours before you get notified. And running it every 5 minutes with a time pattern trigger would be inefficient.

For a more generic approach you can take a look at the gist posted in one of the first posts.

Depending on your needs. For me, its good enough. Some unavailable entites, like my yeelights, which currently stops working randomly (i dont know why…), are a few minutes later available again. I personally don’t want to get notified every minute … But the gist above is definitly worth looking at.

Me neither, that’s why my automation only runs if the device has been unavailable for at least 5 minutes.

Awesome. This would make a great example for a HA blueprint!

I have tried adapting this to my setup but I’m not receiving the notification.
If I type my own message, eg device offline, then it works. But not working when I use the message body in the example. Can anyone see what’s wrong?

Here is mine

alias: Notify Device Offline
description: ''
trigger:
  - platform: state
    entity_id:
      - switch.porch_light
      - switch.bedroom_light
      - switch.kitchen_light
      - switch.living_room_light
      - cover.bedroom_blind
      - cover.living_room_blind
    to: Unavailable
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition: []
action:
  - service: notify.mobile_app_oppo_a5
    data:
      title: Device Offline!
      message: >
        📵 {{ state_attr(trigger.to_state.entity_id, 'friendly_name') }} is
        offline more than 5 minutes!
mode: parallel

Edit: I was getting the message when using run actions command but not when the device was actually unavailable. I realised it was due to having the state as Unavailable with uppercase U. I changed it to lowercase and it is working now.

Hoq would I modify the python script to use WhatsApp notifications?

nvm I figured it out. Why do you replace underscores with backslash underscore? It’s very confusion but I bet you have your reasons.

If i dont have your notify parts on i get the notification if i add yours i never get notified? Help?

I made a few edits to notify the friendly name(s)

if len(offline_devices) > 0:
  message = "Devices offline: " + "\n".join(offline_devices)
  logger.warn(message)
  names = ", ".join([str(hass.states.get(e).attributes.get("friendly_name",e)) for e in offline_devices])

  # in my example, i send a notification via telegram bot
  hass.services.call("notify", "mobile_app", {
    "title": "📵 Devices offline",
    "message": names
  })