Trigger something when a device is unavailable?

Hey,

is it easily possible to trigger something when a device / sensor (any sensor) is unavailable? I found examples that require to check specific sensors but I’d like to keep it dynamic, without having to add / change something anytime I add something new.

This will get you a count of how many sensors are in a given state:

{{ 
  states.sensor 
    | selectattr("state", "in", ["unavailable", "unknown", "none"]) 
    | map(attribute="entity_id") 
    | list 
    | count 
}}

You could use it in a template trigger. Not sure what your end goal is, but here is a whole topic discussing the difficulties around trying to do too much dynamically.

Basically, I just want to get a notification when something is unavailable, so I can fix it soonish to not loose too much data in the graphs and so forth. I have already setup some telegram notifications but I wasn’t able to properly do something like that by now

I see. That should be fine with variations of that template I posted above.

Hm, that returns 6 here in my case. Need to figure out how to get the names of those 6, or is it a ID?
I want something like “Sensor/Device xyz is unavailable”.

I actually slightly over-complicated the template, by including that map, to give you a hint. Play around with the template in the template editor in Developer Tools a bit and see what you can pull out.

A couple tips:

  • count is good for triggers. 0 evaluates to false, while any other number will evaluate to true.
  • Remove count and you should see a list of IDs. Replace entity_id with attributes.friendly_name to get a more readable name.
  • If trying to build a list of names, you can use the join filter. You can supply a separator, like join(', ').

Try it out for yourself, and feel free to post your attempt here if it doesn’t work out and I can give you a hand.

Thanks! Will try to play a bit around with it.

1 Like

Hello, I know this thread has been silent for a long time. But I’ve used these examples to catch missing devices.

Once a day I publish a notification to my phone with this template:

service: notify.mobile_app_cph2449
data:
  message: >-
    There are {{ states.sensor  | selectattr("state", "in", ["unavailable",
    "unknown", "none"])  | map(attribute="entity_id")  | list  | count }} errors
    with the following devices{{ 
      states.sensor 
        | selectattr("state", "in", ["unavailable", "unknown", "none"]) 
        | map(attribute="attributes.friendly_name") 
        | list 
    }}

It has helped me catch a few problems already. But it doesn’t catch one of my better-thermostat integration devices regularly becoming unavailable, thus failing to control my radiator. How can I catch that one too? Here’s the entity being unavailable:

I’ve read about sensors, devices, entities and attributes for hours, but I still don’t see how to tackle this.

Thanks for helping me out!

A late answer is better than no answer at all :slight_smile:
You can try just writing « states » instead of « states.sensor » at the beginning of the template :wink:

It sure is! Thank you! Somehow I wasn’t notified of your reply, so mine is late too.

The weird thing is, that this spontaneously started working with my old template. Maybe a HA update changed how this worked?