Sensor - Unavailable/Offline Detection

Tags: #<Tag:0x00007f7399be20d0>

Question about integration… how do I save this in my system? I have it saved as unavailable_sensors.yaml. I presently don’t have a separate sensors.yaml, but if I do name it as such, I don’t think I can start it with sensor:, correct?

When triggering for in an Automation, what would my entity_id be?

It’s not an integration. It’s just a simple (kind of…) sensor.

I’m confused about what you are asking.

If you want to you can just put it into your configuration.yaml file under the “sensor:” section just like all of your other sensors.

if you do want to split it out using an !include then the text in the file can’t start with “sensor:”

Are you asking how to use the sensor in an automation?

the entity_id will be whatever you called it in the sensor config. Most likely if you just copied the code above it will be “sensor.unavailable_entities”.

If you are asking something else then you need to clarify what you mean.

Thank you.

I renamed my yaml to sensors.yaml, made the appropriate change in my config file.

In the sensors.yaml file, I started the entry as…

platform: template
sensors:
  unavailable_entities:

Here’s my Automation…

- id: '1595647471457'
  alias: Unavailable Sensor Alerts
  description: ''
  trigger:
  - above: '0'
    entity_id: sensor.unavailable_entities
    platform: numeric_state
  condition: []
  action:
  - data:
      message: "{%- for item in states -%}\n  {%- if is_state('item', 'unavailable')\
        \ -%} {{ item.attributes.friendly_name }} unavailable  {%- endif -%} A\n{%-\
        \ endfor -%}\n"
    service: notify.mobile_app_snote8
  mode: single

I’ll need to test this out.

Edit: I do receive the following logged error when restarting HA…

Error loading /config/configuration.yaml: in "/config/configuration.yaml", line 11, column 9: Unable to read file /config/sensors.yaml.

Line 11: sensor: !include sensors.yaml

I also have the following 2 ‘Property X is not allowed’ errors in the sensors.yaml file…
PROPERTY-ERRORS

Create a subdirectory in your main configuration directory named packages.

Add the following line to your configuration.yaml file (homeassistant: should already exist, it is just there to show you where to put the rest)

homeassistant:
  packages: !include_dir_named packages

Create a new file in the packages sub directory. Copy the entire contents of the file in this updated gist (make sure you copy the RAW text - look for the button) into your new file (I just posted and updated version). The file name does not matter but I would go with something like package_unavailable_entities_sensor.yaml .

Check your config and restart home assistant. You should now have a working sensor named sensor.unavailable_entities you can add to your front end and a couple of working sample automations. This is a good base to create your own automations to work with this sensor.

2 Likes

I’m curious what you mean by this? The only difference in the two examples in your code is the sensor will only look in the sensor domain for unavailable states while the second will look through every domain. This is perfectly acceptable and is actually documented on the Templating docs page.

This is not the same as the issue discussed in the warning box where the syntax used may result in an unknown state for the sensor. This sensor should always resolve to zero.

1 Like

Thank you!

This is very helpful. Just what I was looking for to notify me when batteries go bad and devices become unavailable. Very useful.

Personally, I’d like to know about low batteries before they die. Here’s my solution for that. This isn’t anything I’ve really put out there, it’s pretty tailored to my config but it might give you some ideas how to handle it for yours. It creates a persistent notification and an alert push notification.

While you’re at it you might like this custom lovelace plugin for battery entities.

Then there is this package, which to me just seems way overkill to me, but it is kinda neat!

Although everything seems to be configured as you’ve stated, any Entity that becomes unavailable is not identified.

In addition to no notification, I don’t see anything in the logs. Log entry for unavailable entities does say ‘changed to unavailable’.

Any suggestions?

You’ll have to provide a little more info.

Do you have the sensor set up in your lovelace config? You can see it on your dashboard? Does it work for any entity? What unavailable entity is not appearing in the sensor?

If you go to developer tools on the states tab, what do you see if you check the state for sensor.unavailable_entities?

I do not have it setup in Lovelace… Did not know that was a required setting.

I certainly don’t have this setup properly as the sensor is not listed in my Entities nor does it show as an option in Dev Tools/States.

Here’s my main config:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Component files
group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
zone: !include zones.yaml
rest_command: !include rests.yaml
scene: !include scenes.yaml
input_select: !include inputs.yaml
# Home Assistant
homeassistant:
  packages: !include_dir_named packages
# URL
http:
  base_url: https://xxxxxxxxxxxxx
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
# Text to speech
tts:
  - platform: google_translate
# IFTTT
ifttt:
  key: xxxxxxxxxxxx
# MQTT
mqtt:
  broker: core-mosquitto
  username: xxxxxxxxxx
  password: xxxxxxxxxxx
  discovery: true
  discovery_prefix: homeassistant
  birth_message:
    topic: 'hass/status'
    payload: 'online'
    qos: 0
    retain: false

I have your code in a yaml file in the Packages folder, per instructions.

I’m not actually sure if this matters or not but I’ve always had the homeassistant: section and http: sections at the beginning of my configuration.yaml file. Also not sure if linux is case sensitive or not but make sure whatever your have in your config file (!include_dir_named packages) matches the directory name (you called it Packages in your comment).

Also, if you have not updated to v0.113 yet, comment out the mode: queued line in the automations in the package file.

homeassistant:
  packages: !include_dir_named packages
http:
  base_url: https://xxxxxxxxxxxxx
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:
# Component files
group: !include groups.yaml
automation: !include automations.yaml

etc. etc.

Try that first. Check your config and don’t forget to restart!

Thx! Looks like it was the ‘Packages’ name, and/or arrangement of the config file. Sensor now shows in Entities and Dev Tools/States.

In terms of use… I’m simply using the automations you have in the package; I assumed this would trigger a notification from/in my HA Android app, but sounds like I need to update Lovelace first? If so, how do I do that? Pardon for the ‘learning curve’ questions herein.

Glad you got it working!

The included automations are just simple ones to get you started with the sensor. You do not need lovelace to send notifications to your app.

Also, if a recommendation fixes your problem your should mark that comment as “Solution” so others know.

Thanks again. How do I mark a comment a solution, especially if I did not start the Question, and this is in Projects, so not sure we can even mark solutions. I would, but I don’t find the Solution icon/button.

Click this button on the comment you want to mark.

Lol, this is YOUR thread, how’s someone else going to mark it as solved? :see_no_evil:

@ScottS, don’t worry about it, I think Jason needs a lie down…

Huh. Since when does the person supplying the solution mark it solved? How do I know if it worked for him? Have I been doing this wrong all along? :thinking:

Not that I care about it here, but more good info for him in the future. Annoying when people don’t mark it.

No, what he was saying is that you started this thread. So you are the only one who can mark the thread as “solved”. No one else even has the icon to mark it as “solved”.

Scott just happened to ask you a question that you answered in your own thread. Scot can’t mark your thread as solved. Only you can.

:laughing:

Dunno what the hell I was thinking. Other things on my mind I guess.

2 Likes