Unavailable / Unknown Entity Monitoring - Template Sensor

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

same here, plus with 2023.4 it doesn’t show the attributes in the dialog menu

See: HA 2023.4.0 no longer shows my template sensor attribute list - #3 by pedolsky

1 Like

Thank you for the nice sensor and the automation!
I also really like the detailed notification, this is also working except the devices names of ZHA devices are not schown correctly

Can anyone help to fix this?

@jazzyisj This Template sensor is awesome, saved me a couple times in the past, as I have it display unavailable sensors on the home dashboard.
I upgraded to HA 2023.4.6 and my sensor for unavailable_entities went ‘unavailable’!
I upgraded to 2023.5.4 and still remains unavailable. Not sure why.
I appreciate any pointers in what to look for to fix this

Can you post your template?

It is in the Config/Packages folder and I have the include statement in the configuration.yaml

homeassistant:
  packages: !include_dir_named packages
  customize: !include customize.yaml```

###################################################################################################

Package - Unavailable Entities Sensor

Count and list entities with a state of unavailable or unknown

Home Assistant v2022.5 required. See README for customization options.

https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md

###################################################################################################

REQUIRED - This is the template sensor

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 = 300 %}
      {% set ignored = state_attr(‘group.ignored_unavailable_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)
      |rejectattr(‘entity_id’,‘search’,‘alarm_volume|next_alarm|alarms’)
      |rejectattr(‘entity_id’,‘search’,'keymaster
      |identify|echo|next|media_player’)
      |rejectattr(‘entity_id’,‘search’,'button.ikea
      |button.dimmer
      |button.in
      ’)
      |rejectattr(‘entity_id’,‘search’,’_tablet|_pc|_ping|browser|rx|rx|tx|tx|2nd_bath|_switch’)
      |rejectattr(‘entity_id’,‘search’,‘device_tracker|uptime|uptime|_electrical|_consumption’)
      |rejectattr(‘entity_id’,‘search’,’_power|humidity|pressure|weatheralerts|sengled|protection’)
      |rejectattr(‘entity_id’,‘search’,'sensor.pi_hole
      ’)
      |selectattr(‘state’,‘in’,[‘unavailable’,‘unknown’]) %}
      {% if ignored != none %}
      {% set entities = entities|rejectattr(‘entity_id’,‘in’,ignored) %}
      {% endif %}
      {{ entities|map(attribute=‘entity_id’)|list }}

OPTIONAL - Uncomment and add entities you want to ignore to this group.

group:
ignored_unavailable_entities:
entities:
- sensor.1e0de372_f503aec8
- automation.keymaster_garage_changed_code
- automation.keymaster_garage_decrement_access_count
- binary_sensor.0937395f_3ad1290e
- binary_sensor.9d68a265_ade68988
- binary_sensor.dc13e6e8_9c97a4af
- binary_sensor.e3397bb5_ef3397f8
- sensor.network_throughput_in_eth0
- sensor.network_throughput_out_eth0
- sensor.fox_news
- sensor.hacs
- person.admin
- person.tablets
- scene.i_m_home
- automation.announce_arrival
- sensor.nortek_security_and_control_f_adt_wtr_1_b8d50b4c_device_temperature
- sensor.nortek_security_and_control_f_adt_wtr_1_b8d50b4c_device_temperature_2
- light.watch_sony_tv
- light.watch_sony_tv_2
- button.sony_xr_65a80j_reboot
- button.sony_xr_65a80j_terminate_apps
- button.syn920_reboot
- button.syn920_shutdown

OPTIONAL Example automation to demonstrate how you can utilize this sensor```

I don’t see anything wrong with a quick look, but you’ll need to format your comment correctly before I can help you. You only need to provide the template section.

Let me try again. I thought I had it in a code block
And again, it was working up until i sent to 2023.4.6. :thinking:

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 = 300 %}
            {% set ignored = state_attr('group.ignored_unavailable_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)
                |rejectattr('entity_id','search','_alarm_volume|_next_alarm|_alarms')
                |rejectattr('entity_id','search','keymaster_|_identify|_echo|next_|media_player')
                |rejectattr('entity_id','search','button.ikea_|button.dimmer_|button.in_')
                |rejectattr('entity_id','search','_tablet|_pc|_ping|_browser|_rx|rx_|_tx|tx_|2nd_bath_|_switch')
                |rejectattr('entity_id','search','device_tracker|_uptime|uptime_|_electrical|_consumption')
                |rejectattr('entity_id','search','_power|_humidity|_pressure|weatheralerts_|sengled_|_protection')
                |rejectattr('entity_id','search','sensor.pi_hole_')
                |selectattr('state','in',['unavailable','unknown']) %}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

I don’t recall any change in HA that would cause it not to work and I don’t see anything wrong here. I’m assuming you didn’t alter the template at all?

There is a slightly updated version on the git. I would start by removing the package (just move the file out of the directory), then reload your templates and delete the orphaned entity. Then redo the package with the newest version (you should be able to copy and paste your entity_id section from the old file). Reload your templates and then see what happens. Let me know how you make out.

1 Like

Thanks for looking into it, as i was scrolling up to et the GIT link, i saw a post from apr 6 with the same problem. It was solved by simply removing the unit of measure line :upside_down_face:

Ah yeah. Forgot about that. You can either delete that or add the following if you want to keep the unit of measurement.

state_class: measurement

1 Like

Hey I have been using my below template and need to add one little piece. I want to filter out entities of disabled devices. What do I need to add to the filter?

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','none'])%}
            {% if ignored != none %}
              {% set entities =  entities|rejectattr('entity_id','in',ignored) %}
            {% endif %}
            {{ entities|map(attribute='entity_id')|list }}

The in documentation is pretty thorough and there are several examples in the git issues and discussions.

When you disable a device the entities become “unavailable”. Add those entities to the ignored_entities group or add a filter in the template.

If there are unique common elements to the in the device entitiy id’s you can add search filters. Examples are provided in the documentation.

If the devices are all part of one integration you can ignore the integration entirely.

If you still need assistance, please provide a list of the entity id’s you wish to filter out.

I know I could do this, but I was hoping to just add all the entities of disabled devices.

If I do this

{{device_entities(device_attr('',''))}}

I can at least get all the devices but not if disabled

I was thinking about your request and I’m actually fooling around with some templates right now to determine disabled devices and automatically excluded them from the sensor.

I’ll get back to ya.

Awesome, thank you