Unavailable / Unknown Entity Monitoring - Template Sensor

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

So as you discovered, the issue here is that when a device is disabled device_entities() doesn’t report any entities.

If it DID, this little hacky template (there is probably a more elegant way to accomplish this) would work and we could utilize it in the unavailable entities template. It would have been a good addition to the template if it did work! But no dice. So I guess my advice above stands. You’ll have to add the entities required manually.

{%- set devices = namespace(device_id=[]) %}
{%- for item in states|map(attribute='entity_id')|list %}
  {%- if device_attr(item,'disabled_by') != none %}
    {%- set devices.device_id = devices.device_id + [device_id(item)] %}
  {%- endif %}
{%- endfor %}
{%- set disabled = devices.device_id|unique|list %}
{% for item in disabled %}
  {{ item }}
  {{ device_attr(item,'name') }}
  {{ device_entities(item) }}
{% endfor %}

Well at least you tried, thanks anyway.

How would I exclude all entity IDs from a specific Integration without adding them to a group ‘Alexa Media Player’ for example? I have over 120+ entity IDs for these.

1 Like

Template and documentation have been updated with new syntax and to exclude new stateless domains (button, scene, event etc.) by default.

3 Likes

Is there anyway just to show entity that’s are unavailable, currently its showing unavailable and unknown entities which isn’t what. I need

Replace this

{% 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', ['button', 'event', 'group', 'input_button', 'input_text', 'scene'])
    | rejectattr('last_changed', 'ge', ignore_ts) %}
{% set entities =  entities | rejectattr('entity_id', 'in', ignored) if ignored != none else entities %}
{{ entities | map(attribute='entity_id') | reject('has_value') | list | sort }}

With this

{% 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', ['button', 'event', 'group', 'input_button', 'input_text', 'scene'])
    | selectattr('state','eq','unavailable')
    | rejectattr('last_changed', 'ge', ignore_ts) %}
{% set entities =  entities | rejectattr('entity_id', 'in', ignored) if ignored != none else entities %}
{{ entities | map(attribute='entity_id') | list | sort }}
1 Like

I love this sensor however I have just added a device that has added a set of entities beginning with ‘sensor.byte’ which I do not want to know about however when I put '- sensor.byte_* ’ in my ignore entities group they still show. I obviously got the syntax wrong or do i need to add a rejectattr to the sensor code itself.