A sensor to list unavailable devices

This is great. I use the same workaround with identifiers. I liked your discussion post. I hope this will get added!

Kind regards,
- Ingrid

{{ states 
| map(attribute='entity_id') 
| map('device_attr', 'identifiers') 
| select('ne', None)
| map('first')
| select('ne', undefined)
| map('first')
| unique
| sort }}

1 Like

Thanks for your vote!

I have been unsuccessful in implementing this for myself and I was hoping you could point me in the right direction. I’ve added the macro to a file here: /homeassistant/custom_templates/tools.jinja . When I attempt to call the macro by entering {{ unavailable_devices()|from_json }} in the developer tools template area I get “‘unavailable_devices’ is undefined”. I’ve restarted Home Assistant. It’s probably something stupid but I am trying to learn more about advanced templating so any help is appreciated.

If you are talking about my version then it is a YAML file, not JSON. I put it in /config/templates.yaml (create it if you don’t have it already) .

In /config/configuration.yaml include the line

template: !include templates.yaml

You then need to reload the configuration to activate it. Clear the log file first and if it does not work check the log file afterwards.

To use macro 's one needs to import them:

{% for ‘tools.jinja’ import unavailable_devices %}
{{ unavailable_devices()}}

Sorry, I have not used macros in HA and don’t know anything about them. Why do you want to?

This allows to use the same template code on multiple places in you cards and template sensors

1 Like

Thanks for responding! Your solution with the auto-entities card and custom-template-entity-row was exactly what I was trying to achieve: getting only the device (no repeat entities) that was unavailable into a dynamic dashboard card and have the tap action go to that device page. I was able to get everything working.
Firstly, I was getting errors initially because I was accidentally commenting out a necessary line when editing the ignore_list portion of the macro. After figuring that out, I just needed to edit your macro call in the developer tools area to this:

{% from 'tools.jinja' import unavailable_devices %}
{{ unavailable_devices()|from_json }}
{{ unavailable_devices()|from_json|count }}

to make the code give me any results. I’ve now got the template working in an auto-entities card. Thanks again!

2 Likes

I have a question, can you have a group of entities that you use as an exclusion group to this sensor?

You can do it with a filter like this where exclusions is a list (e.g. an expanded group):

| rejectattr('entity_id', 'in', exclusions)

I like this solution.

Unfortunately I do not get the expected result from integration_entities.
For example HACS creates an update sensor for each custom integration, which has the value “unavailable” when no updates are available. Yet,

{{ integration_entities(‘hacs’) }}

returns just [‘sensor.hacs’] despite the UI showing more entities under HACS. All unavailable except the single one returned.

Same with UniFi.

If integration_entities does not report unavailable entities, it may not not be a good method of excluding devices.

@Jan4 You are right, integration_entities() is not returning all entities. I created an issue a while ago for that

when i paste this into my templates.yaml file i get the following

if i remove the template: portion i no longer see any errors but when i restart home assistant and check for an entity called sensor.unavailable_devices i cant find it.

here is what i get in my logs when i restart HA

Logger: homeassistant.config
Source: config.py:592
First occurred: 10:21:00 AM (1 occurrences)
Last logged: 10:21:00 AM

Invalid config for ‘template’ at templates.yaml, line 70: ‘sensor’ is an invalid option for ‘template’, check: sensor->1->sensor Invalid config for ‘template’ at templates.yaml, line 70: required key ‘state’ not provided

Delete the remakes :wink:

And test the configuration before restarting

I get this working by doing this:

{{ integration_entities('mqtt') | expand

This is a really useful template.

Just one question - how can I exclude devices or more specifically entities that have been disabled by me?

Ask the author of the Solution post.

Do you know if there is a way of excluding “disabled” entities/devices from the list? These seem to show up as unavailable, but I’ve manually disabled them as they are unused or out of action.
ta

Read through this. Unavailable / Unknown Entity Monitoring - Template Sensor - Share your Projects! - Home Assistant Community (home-assistant.io)

Thanks - this was just what I needed. Worked great. Was also able to use the auto-entities to list battery % as well.