Display multiple unavailable devices in a card

Is there any way with minimal or no YAML to display all switches that are unavailable. I can do this one by one for each device with the code below, but I was hoping for a simpler way to do all switches at once. These is some YAML at
Blueprint Link but I could not get it to import to see if it would do what I wanted.

Any suggestions?

type: entity
entity: switch.athom_smart_plug_v2_ec747b_smart_plug_v2
visibility:
  - condition: or
    conditions:
      - condition: state
        entity: switch.athom_smart_plug_v2_ec747b_smart_plug_v2
        state: unavailable
      - condition: state
        entity: switch.athom_smart_plug_v2_ec747b_smart_plug_v2
        state: unknown
grid_options:
  columns: 9
  rows: 2

I use this card to figure out what ESL tags are offline in a markdown card.
Try it first in the template tools.

Not sure if the integration is called athom or not, I don’t have any such devices.

{% for state in  integration_entities("athom") |select('match','switch\..*')|list  %}

{%- if states(state) == "unavailable" %}
{{device_name(state)}} {{ relative_time(states[state].last_changed)  }}
{%- endif %}
{%- endfor %}

If it fails then just see if this is correct.

{{ integration_entities("athom") |select('match','switch\..*') | list }} 

It should give you a list of all switches.

Thx heaps, will do

Thanks, after messing around for an hour, (I did not even know what a template was or that it existed) I realised that the integration name must be lower case ‘esphome’ not ‘ESPHome’. Do you have an example of how to use this in a card? Unfortunately the ‘Templates’ docs don’t mention cards, and the ‘Cards’ docs don’t seem to have a generic type that does not need an entity or similar, so I am having troubles combining them.
I tried a conditional card but just got errors. Note that I only have a basic understanding of the syntax, but anything you have might help.

Oh… so it’s ESP-Home devices.

Well just add a markdown card and add the template:

I’d probably use the above approach now, but when I first setup my ‘devices offline page’, I simply enabled the LQI entity on all my zigbee devices and use an auto entities card to list all LQI’s in an unavailable state, works fine:-

type: custom:auto-entities
card:
  type: entities
  title: Offline ZHA Devices
  show_header_toggle: false
filter:
  include:
    - entity_id: "*lqi*"
      state: unavailable
      options:
        secondary_info: last-changed
  exclude:
    - name: "*NOT-USED*"
    - name: "*Tradfri Switch*"
sort:
  method: state
  numeric: true

Thx, Markdown card worked like a charm. Think I’ll play around and see if I can make this more generic for all devices.

I anyone needs it this will list all unavailable states from all devices - as far as I can tell.

{# List all offline states #}
Offline:
{%- for entity in states -%}
{% if entity.state == "unavailable" %}
{{ entity.entity_id -}}
{%- endif -%}
{%- endfor -%}

I suggest you use the device name as I did in my template.
Using only entities like this will list all entities of the same device.
So a ESP device with lots of entities will clutter the list with all entities.
Using device_name() means you can reduce it to unique devices.

I did it that way which I keep on an error page as I wanted an itemised list as well as just the devices. You code works for a specific integration so I wrote the following to find devices across all integrations.
Note that the exclude id found in device url (I have a couple that I know are unavailable that I did not want notification)

### Offline:
{%- set exclude = [
  'f7b3ace4cca80ff364d43a63855fa4b4',
  '2778683f71793a0a90ae49a8e50b97e7'
] -%}
{%- set ns = namespace(seen=[]) %}
{%- for entity in states if entity.state == 'unavailable' %}
  {%- set dev_id = device_id(entity.entity_id) %}
  {%- if dev_id and dev_id not in exclude %}
    {%- set dev_name = device_attr(dev_id, 'name') %}
    {%- if dev_name not in ns.seen %}
{{ dev_name }}
      {%- set ns.seen = ns.seen + [dev_name] %}
    {%- endif %}
  {%- endif %}
{%- endfor %}

1 Like