Unavailable / Unknown Entity Monitoring - Template Sensor

Ok I am really lost, I updated my unavailable entitys to below and it will not filter out using the searches. Any help would be great.

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.entity_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
        unit_of_measurement: entities
        state: >
          {% set entities = state_attr(this.entity_id,'entity_id') %}
          {% if entities != none %} {{ entities|count }} {% endif %}
        attributes:
          entity_id: >
            {% set ignore_seconds = 60 %}
            {% set ignored = state_attr('group.ignored_entities','entity_id') %}
            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
            {% set entities = states|rejectattr('domain','eq','group')
                |rejectattr('last_changed','ge',ignore_ts)
                |selectattr('state','in',['unavailable','unknown'])
                |rejectattr('entity_id','search','^(button|switch){1}(.octoprint_)\S+')
                |rejectattr('entity_id','search','\S+tessy_\S*')
                |rejectattr('entity_id','search','^(device_tracker)\S+')
                |rejectattr('entity_id','search','^(camera\.wyze_cam)\S+')
                |rejectattr('entity_id','search','^(sensor)\S+(speaker_alarms|speaker_timers|display_alarms|display_timers)')
                %}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

Why not using only 1 rejectattr?


                |rejectattr('entity_id','search','device_tracker|master|wyze_cam')

Because some of the regedit is more complex. I just wanted to seperate them ench. But even with the rejectattr search it is still putting it in.

Set it back to the default template then add your filters one at a time, test them, them move on once you’ve got the current one working.

First thing I see is this |rejectattr('entity_id','search','^(device_tracker)\S+') Why use regex here?

You want to filter out all entities in the domain device tracker?
|rejectattr('domain','eq','device_tracker')

You want to filter out all entities that contain “camera.wyze_cam” in their entity_id?
|rejectattr('entity_id','search','camera.wyze_cam')

Etc. etc.

I just reverted, it was easier, below is my code. Now I have a question about display. What is the best way to have a card where I would click on the entity and go to it’s info page?

sensor:
  - platform: template
    sensors:
      unavailable_entities:
        friendly_name: Unavailable Entities
        unit_of_measurement: entities
        icon_template: "{{ 'mdi:check-circle' if is_state('sensor.unavailable_entities','0') else 'mdi:alert-circle' }}"
        value_template: >
          {{ states|selectattr('state','in',['unavailable','unknown','none'])|rejectattr('domain','eq','group')
          |rejectattr('entity_id','in',state_attr('group.ignored_entities','entity_id'))
          |rejectattr('domain','eq','device_tracker')
          |rejectattr('domain','eq','number')
          |rejectattr('entity_id','search','camera.wyze_cam')
          |rejectattr('entity_id','search','tessy_')
          |rejectattr('entity_id','search','_do_not_disturb')
          |rejectattr('entity_id','search','^(button|switch){1}(.octoprint_)\S+')
          |rejectattr('entity_id','search','^(sensor)\S+(speaker_alarms|speaker_timers|display_alarms|display_timers|_estimated_distance)')
          |list|count }}
        attribute_templates:
          entities: >
            {{ states|selectattr('state','in',['unavailable','unknown','none'])|rejectattr('domain','eq','group')
            |rejectattr('entity_id','in',state_attr('group.ignored_entities','entity_id'))
            |rejectattr('domain','eq','device_tracker')
            |rejectattr('domain','eq','number')
            |rejectattr('entity_id','search','camera.wyze_cam_')
            |rejectattr('entity_id','search','tessy_')
            |rejectattr('entity_id','search','_do_not_disturb')
            |rejectattr('entity_id','search','^(button|switch){1}(.octoprint_)\S+')
            |rejectattr('entity_id','search','^(sensor)\S+(speaker_alarms|speaker_timers|display_alarms|display_timers|_estimated_distance)')
            |map(attribute='entity_id')|list }}

You really need to format your code when you post. Also, if you’re posting a wall of text consider hiding it with a summary. This is way too hard to read on mobile and difficult to copy and paste without having to reformat it. Seriously, try copying your sensor definition above and pasting it back into your code editor. You’re asking people to spend their time helping your, the least you can do is make it as easy as possible for them.

Here's my sensor config as an example
  sensor:
    - name: "Unavailable Sensors"
      unique_id: unavailable_sensors 
      icon: "{{ iif(states(this.entity_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
      unit_of_measurement: entities
      state: >
        {% set entities = state_attr(this.entity_id,'entity_id') %}
        {% if entities != none %} {{ entities|count }} {% endif %}
      attributes:
        entity_id: >
          {% set ignore_seconds = 60 %}
          {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
          {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
          {% set entities = states|rejectattr('domain','in',['group','geo_location'])
              |rejectattr('entity_id','in',expand(integration_entities('browser_mod'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('gdacs'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('github'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('glances'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('hassio'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('openuv'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('utility_meter'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','in',expand(integration_entities('waqi'))|map(attribute='entity_id')|list)
              |rejectattr('entity_id','search','jlaptop_chrome|jlaptop_edge|jwork_chrome|jphone_app|sphone_app')
              |rejectattr('entity_id','search','jason_laptop_media_player|jason_phone_media_player|sheri_phone_media_player')
              |rejectattr('entity_id','search','_timers|_alarms|_do_not_disturb|_alarm_volume')
              |rejectattr('entity_id','search','sensor.daily_energy_consumption')
              |rejectattr('entity_id','search','_next_alarm')
              |rejectattr('last_changed','ge',ignore_ts)
              |selectattr('state','in',['unavailable','unknown']) %}
          {% if ignored != none %}
            {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
          {% endif %}
          {{ entities|map(attribute='entity_id')|list }}

Anyway the simplest way is to display the sensor with clickable entities is to use an auto-entities card.

type: custom:auto-entities
show_empty: false
filter:
  template: "{{ state_attr('sensor.unavailable_entities','entity_id') }}"
sort:
  method: state
card:
  type: entities
  title: "Unavailable Entities"
  show_header_toggle: false

image

1 Like

First thank you for this. It is great.

Second I did what you did, and it worked. Sorry for the formatting earlier.

This is much nicer. BTW is there anyway to get a count at the top of the card?

Add the unavailable entities sensor to the card config.

type: custom:auto-entities
filter:
  template: "{{ state_attr('sensor.unavailable_entities','entity_id') }}"
sort:
  method: state
card:
  type: entities
  title: "Unavailable Entities"
  show_header_toggle: false
entities:
  - sensor.unavailable_entities

image

Or if you want to get fancy use a fold entities row in your card to hide the list until you click the chevron.

type: entities
title: "Unavailable Entities"
show_header_toggle: false
entities:
  - type: custom:auto-entities
    show_empty: true
    unique: true
    filter:
      template: "{{ state_attr('sensor.unavailable_entities','entity_id') }}"
    sort:
      method: state
    card:
      type: custom:fold-entity-row
      padding: 0
      head:
        entity: sensor.unavailable_entities

image

image

I like that idea. I’m also toying with using the card to filter while having the ability to see anything I want filtered out. This way I don’t need to restart, its all part of the card.
Thank you so much for the help.

1 Like

Could you please edit that post and fix it?

how add the code?

sensor:
    - name: "Unavailable Sensors"
      unique_id: unavailable_sensors 
      icon: "{{ iif(states(this.entity_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
      unit_of_measurement: entities
....
....

i need to start like this:

- platform: template
  sensors:
    unavailable_entities:

thanks

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.entity_id)|int(0) > 0,'mdi:alert-circle','mdi:check-circle') }}"
        unit_of_measurement: entities
        state: "{{ entities|count if entities != none else none }}"

You’re trying to use the old yaml template configuration. No longer recommended.

thanks, i found here a good solution:

ohh i see you have here a nice example:

i take this :wink:

i see now how many entities have a state = Unavailable
But how can i output a “name” List or other List from the items?

There is an example automation in the package that creates a persistent notification with either a simple or detailed list of unavailable/unknown entities.

Have you looked at that? If persistent notifications aren’t your bag you can use the code used there in an HTML template card or something.

Hi, First of all, thanks for the code of this wonderful sensor.
I’m trying to send a notification to a Telegram group modifying a little the code you wrote for the detailed notification, but… it’s not working as expected.

The sensor is working fine, just when i try to fomat it using (using it in developers tab):

- service: notify.telegram
  data:
    title: "Entidades no disponibles"
    message: >
       {% set ns = namespace(result=[]) %}
       {% for s in expand(state_attr('sensor.unavailable_entities', 'entity_id')) %}
           {% set ns.result = ns.result + [
                   s.name ~ "\n"
                      ~ "  - Entity_id: " ~ s.entity_id ~ "\n"
                      ~ "  - Estado: " ~ s.state ~ "\n"
                 ]
            %}
        {% endfor %}                                                
        {{ ns.result | join('\n') }}

the result code is:

- service: notify.telegram
  data:
    title: "Entidades no disponibles"
    message: >
       
       
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
        
           
                                                        
         Controlinputselect2
  - Entity_id: automation.controlinputselect2
  - Estado: unavailable

loc2
  - Entity_id: automation.loc2
  - Estado: unavailable

login
  - Entity_id: automation.login
  - Estado: unavailable

Why i have a lot of blank / spaces at the beggining ?

Not sure. Your code works fine for me.

Can you do a screenshot like I did of the actual developer tools template?

Tested.

Using the code as you did, it works too.

just when i use:

- service: notify.telegram
  data:
    title: "Entidades no disponibles"
    message: >

Is when it creates a lot of blank space, and i receive a message at the logs with:

Let me try to check again, to see how i can solve this.

Thanks !!!

I get this error message in the logs since the update to 2023.03. Is there something about the template sensor that should be adjusted?

Logger: homeassistant.components.sensor
Source: components/sensor/__init__.py:597
Integration: Sensor (documentation, issues)
First occurred: 14:15:07 (1 occurrences)
Last logged: 14:15:07

Sensor sensor.unavailable_entities has device class None, state class None and unit entities thus indicating it has a numeric value; however, it has the non-numeric value: (<class 'str'>); Please update your configuration if your entity is manually configured, otherwise create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+template%22