Notify when any sensor is unavailable / offline

Hello, my first topic here.

Is there a way to send a notification when any sensor (15+ sensors, mostly Xiaomi) goes ‘unavailable’ or ‘unknown’, that is for whichever reason it goes offline, without writing automations for each individual sensor?

I searched the forum and tried to find a solution with for loops in template sensor, but I didn’t really get anywhere.

Any help appreciated.

1 Like

Probably not the slickest way to do it, but here is how I am notified when a device goes offline.

#########################################################
#                                                       #
#             NOTIFY IF YEELIGHT OFFLINE                #
#                                                       #
#########################################################

- alias: "Light Offline"
  id: 98cad77f-83a0-4b95-b45b-fddec6342025
  trigger:
    - platform: state 
      entity_id:
        - light.yeelight1
        - light.yeelight2
        - light.yeelight3
        - light.yeelight_4
        - light.yeelight_5
        - light.anthom1
        - light.wled
        - light.notification_light
      to: 'unavailable'
      for:
        seconds: 180
  action:
    service: notify.home_assistant
    data_template:
      title: "A Light is Offline"
      message: "{{ trigger.to_state.attributes.friendly_name }} is offline"
3 Likes

Thanks for the link, that looks good, I now understand I would probably need only a couple of automations.
I think Xiaomi sensors go ‘unavailable’, but my MQTT sensors go ‘unknown’ for example.
And do binary sensors also have an ‘unavailable’ state?

It would still be good to have a general way to do it, so when I add a new sensor I don’t need to remember to add it to the automation list.

Something like this pseudocode:

for sensor in sensors:
  if sensor.state == 'unavailable' or sensor.state == 'unknown':
    notify sensor.name 'is' sensor.state

did you ever get further with this?

I’ve just noticed that one of my two xiaomi hubs has been offline for a day.

I’m using the nmap device tracker to track various devices, but the downside with that is it only checks every 10 minutes. And I can’t find a state for the actual hub integration itself…

I’ve setup a ping binary sensor for my two xiaomi hubs to check on them every 30 seconds.

An example of one in my binary_sensors.yaml file:

- platform: ping
  host: 192.168.1.30
  name: xiaomi_gateway
  count: 2
  scan_interval: 30

This appears as binary_sensor.xiaomi_gateway in Home Assistant and you can create automations to check if they’re online or not (state is either ‘on’ or ‘off’). Or just check the UI periodically.

not sure why I didnt’ think of this.

moved it over - will be faster. And hopefully less false positives

For anyone using appdaemon, I made this script for the same purpose:

I haven’t had any devices run out of power yet, but I tested it with a light, and it seems to work as intended.

All the self.battery_levels are template sensors (search for “# Sensor Batteries” in my sensors.yaml file if you want to copy those) getting the battery state of the corresponding entity in self.battery_powered_devices.

I have it create a sensor (sensor.unavailable_devices) where the state shows if any on that list are unavailable/unknown, and puts a list of all in the attribute “devices” (if there’re a lot, it’s probably some other issue; they are unlikely to all run dry at the same time). You could use that sensor in the frontend if you want. I also have it send a notification.

Would it be possible to re-host this? Showing a 404 unavialable.

Fixed. Thanks for the heads up.