Repository: jazzyisj/unavailable-entities-sensor Branch: main Commit: 089bb2f6bb32 Files: 5 Total size: 22.1 KB Directory structure: gitextract_lntpn485/ ├── README.md ├── examples/ │ ├── auto_entities_card.yaml │ └── detailed_persistent_notification.yaml ├── package_unavailable_entities.yaml └── v1/ └── package_unavailable_entities_v1.yaml ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ [![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg)](https://hacs.xyz/) # Unavailable Entities Template Sensor ## What does this package do? This package creates a group of entities that have no value (a state of unknown or unavailable) and a sensor that provides a count of the entities in this group. The sensor can be used to help you discover entities which should to be disabled or deleted and help you identify misbehaving or failed devices. The structure for this sensor has changed from the original version. [You can read about the changes here.](https://github.com/jazzyisj/unavailable-entities-sensor/discussions/57) ### Requirements Home Assistant v2024.8 is the minimum version required to use this package. The [auto-entities](https://github.com/thomasloven/lovelace-auto-entities) and [fold-entity-row](https://github.com/thomasloven/lovelace-fold-entity-row) plugins is required to use the example lovelace card. [![auto-entities](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thomasloven&repository=lovelace-auto-entities) The [fold-entity-row](https://github.com/thomasloven/lovelace-fold-entity-row) plugins is required to use the example lovelace card. [![auto-entities](https://my.home-assistant.io/badges/hacs_repository.svg)](https://my.home-assistant.io/redirect/hacs_repository/?owner=thomasloven&repository=lovelace-fold-entity-row) ## Installation ### Install As Package The easiest way to use this sensor is to install it as a [package](https://www.home-assistant.io/docs/configuration/packages/). If you already have packages enabled in your configuration, download [`package_unavailable_entities.yaml`](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/package_unavailable_entities.yaml) to your packages directory. To enable packages in your configuation, create a folder in your config directory named `packages` and add the following line to your `configuration.yaml` file. homeassistant: packages: !include_dir_named packages ### Install Without Packages To create this sensor without using packages simply copy the relevant code to an appropriate place in your configuration.yaml file. The example notification automation is optional. **NOTE! You must reload automations, templates, and group entities after adding this package!** ## Excluding Entities You will likely have many devices and/or entities which you do not need to monitor or that often have states of unknown or unavailable. These devices and entities can be excluded from the sensor. Excluding a device excludes all of it's entities. Disabled devices and entities and devices and entities labeled `ignored` in the UI are excluded from the sensor by default. To use a different label name, change the `ignore_label` value in the group template to the desired value. The label is not case sensitive. This is the recommended way to exclude entities. ### Customizing The Template In addition to excluding entities using labels in the UI, entities can also be excluded with additional filters in the template, or by adding them to the `group.ignored_entities` definition. #### Ignore Domains Domains that are "stateless" (button, scene, event etc) or that don't make sense to monitor are excluded by default. To monitor these domains remove them from the ignored domains filter. Example - remove the `input_button` domain. |rejectattr('domain', 'in', ['button', 'event', 'group', 'input_text', 'scene']) If you wish to ignore additional domains you can also add them to the ignored domains filter. Example - add the `switch` domain. |rejectattr('domain', 'in', ['button', 'event', 'group', 'input_button', 'input_text', 'scene', 'switch']) #### Ignore Specific Entities To ignore specific entities, add them to the `group.ignored_unavailable_entities` declaration. #### Ignore Matching Entities **Search Test** If you have multiple entities to ignore that share a common uniquly identifiable portion of their entity_id name you can exclude them without having to add each individual sensor to the ingore_entities group by adding filters using a search test to the template. |rejectattr('entity_id', 'search', 'wifi_') Be as specific as possible in your filters so you don't exclude unintended entities! For example, if you have the following sensors in your configuration and just want to exclude just the wifi signal strengh sensors, rejecting a search of 'wifi_' will also exclude `binary_sensor.wifi_connected`. - binary_sensor.wifi_connected - sensor.wifi_downstairs - sensor.wifi_upstairs You can also use [regex pattern matching](https://regex101.com/) in a seach rejectattr (selectattr) filter. |rejectattr('entity_id', 'search', '(_alarm_volume|_next_alarm|_alarms)') The filter above effectively combines these three filters. |rejectattr('entity_id', 'search', '_alarm_volume') |rejectattr('entity_id', 'search', '_next_alarm') |rejectattr('entity_id', 'search', '_alarms') **Contains Test** The [contains](https://www.home-assistant.io/docs/configuration/templating/#contains) test can also be used if required. Regex matching is not available for the contains test. |rejectattr('entity_id', 'contains', 'wifi_') #### Excluding Specific Integrations You can exclude entities from a specific integration by using an `in` test for the entity_id and the [integration_entities() function](https://www.home-assistant.io/docs/configuration/templating/#integrations). |rejectattr('entity_id', 'in',integration_entities('hassio')) #### Excluding Specific Devices You can exclude entities from a specific integration by using an `in` test for the entity_id and the [device_entities() function](https://www.home-assistant.io/docs/configuration/templating/#devices). |rejectattr('entity_id', 'in',device_entities('fffe8e4c87c68ee60e0ae84c295676ce')) ### Full Example automation: ~~~ action: - action: group.set data: object_id: unavailable_entities entities: > {% set ignore_seconds = 60 %} {% set ignore_label = 'ignored' %} {% set ignored_domains = ['button', 'conversation', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt', 'update'] %} {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %} {% set disabled_device_entities = state_attr('sensor.disabled_device_entities', 'entities') | regex_replace(find='\[|\]|\{|\}|\'entity_id\':', replace='') %} {% set ignored_devices = label_devices(ignore_label | lower) %} {% set ignored_device_entities = namespace(value=[]) %} {% for device in ignored_devices %} {% set ignored_device_entities.value = ignored_device_entities.value + device_entities(device) %} {% endfor %} {{ states | rejectattr('domain', 'in', ignored_domains) | rejectattr('entity_id', 'in', disabled_device_entities) | rejectattr('entity_id', 'in', state_attr('group.ignored_entities', 'entity_id')) | rejectattr('entity_id', 'in', ['group.unavailable_entities', 'group.ignored_entities']) | rejectattr('entity_id', 'in', ignored_device_entities.value) | rejectattr('entity_id', 'in', label_entities(ignore_label | lower)) | rejectattr('last_changed', 'ge', ignore_ts) | rejectattr('entity_id', 'search', 'browser_') | rejectattr('entity_id', 'search', '_alarm_volume|_next_alarm|_alarms') | rejectattr('entity_id', 'contains', '_memory_percent') | rejectattr('entity_id', 'in', integration_entities('hassio')) | rejectattr('entity_id', 'in', device_entities('fffe8e4c87c68ee60e0ae84c295676ce')) | selectattr('state', 'in', ['unknown', 'unavailable']) | map(attribute='entity_id') | list | sort }} See [Home Assistant Templating](https://www.home-assistant.io/docs/configuration/templating/) for additional options. ### Specifing Entities to Monitor - Custom Sensors You can configure the sensor to only monitor entities you specify instead of monitoring all entities and specifing the entities to ignore by using select or selectattr filters instead of reject and rejectattr filters. Remember, filters are cumlative and entities may be already excluded by previous filters. This is useful to create sensors that monitor specific domains, integrations etc. You can create as many groups and related sensors as you need. The following example monitors only the `sensor` domain from the Shelly integration that contain the string `_power` in the entity_id. ### Example template: - sensor: - name: "Unavailable Shelly Power Entities" unique_id: unavailable_shelly_power_entities icon: "{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}" state_class: measurement state: > {% set entities = state_attr('group.unavailable_shelly_power_entities', 'entity_id') %} {{ entities | count if entities != none else -1 }} automation: ~~~ action: - action: group.set data: object_id: unavailable_shelly_power_entities entities: > {{ states.sensor | selectattr('entity_id', 'in', integration_entities('shelly')) | selectattr('entity_id', 'contains', '_power') | selectattr('state', 'in', ['unknown', 'unavailable']) | map(attribute='entity_id') | list | sort }} ## Using With Automations There is an example automation provided in the package that will display unavailable entities as a persistent notification. You can change this automation to meet your requirements. This automation is enabled by default. You can comment it out or delete it if not required. See the examples folder for more automations. ## Display in the UI To display a list of unavailable entities open the more-info dialogue of group.unavailable_entities. ![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/group_more_info.png) #### Using Auto Entities and Fold Entity Row Using the [auto-entities](https://github.com/thomasloven/lovelace-auto-entities) and [fold-entity-row](https://github.com/thomasloven/lovelace-fold-entity-row) plugins is an excellent way to display your unavailable entities sensor in your UI. [Example Entities Card](https://github.com/jazzyisj/unavailable-entities-sensor/blob/master/examples/auto_entities_card.yaml) *Closed Fold Entity Row* ![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/entities_card_closed_example.png) *Open Fold Entity Row* ![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/entities_card_open_example.png) ================================================ FILE: examples/auto_entities_card.yaml ================================================ ################################################################################################### ## DESCRIPTION: Using unavailable entities sensor with an auto entities card and fold entity row ## https://github.com/thomasloven/lovelace-auto-entities ## https://github.com/thomasloven/lovelace-fold-entity-row ################################################################################################### type: entities title: "Unavailable Entities Example" state_color: true show_header_toggle: false entities: - type: custom:auto-entities show_empty: true unique: true filter: include: - group: group.unavailable_entities sort: method: state card: type: custom:fold-entity-row padding: 0 head: entity: sensor.unavailable_entities ================================================ FILE: examples/detailed_persistent_notification.yaml ================================================ ################################################################################################### ## DESCRIPTION: Detailed persistent notification message - courtesy of @ThomDietrich and @warthog9 ################################################################################################### automation: - id: unavailable_entities_notification alias: "Unavailable Entities Notification" description: "Create persistent notification if unavailable entities, dismiss if none." mode: restart triggers: - trigger: state entity_id: group.unavailable_entities attribute: entity_id to: ~ for: 5 # throttle triggers and prevent blank notifications conditions: - condition: template alias: "Sensor state is a valid numerical value" value_template: "{{ is_number(states('sensor.unavailable_entities')) }}" actions: - action: persistent_notification.create data: notification_id: unavailable_entities title: "Unavailable Entities" message: > {% set ns = namespace(result=[]) %} {% for s in expand(state_attr('group.unavailable_entities', 'entity_id')) %} {% set ns.result = ns.result + [ device_attr(s.entity_id, "name") ~ "|" ~ device_id(s.entity_id) ~ "|- **" ~ s.name ~ "**\n" ~ " - *entity_id*: " ~ s.entity_id ~ "\n" ~ " - *state*: " ~ s.state ~ "\n" ] %} {% endfor %} {% set ns.result = ns.result | sort %} {% set lastdev = namespace( id="" ) %} {% set tarr = ns.result %} {% set ns.result = [] %} {% for item in tarr %} {% set dev = namespace( id="" ) %} {% set entity = namespace( data="" ) %} {% set dev.id = item.split("|")[1] %} {% set entity.data = item.split("|")[2] %} {% if lastdev.id != dev.id %} {% if dev.id != 'None' %} {% set ns.result = ns.result + [ "**" ~ device_attr(dev.id, "name") ~ "**" ] %} {% else %} {% set ns.result = ns.result + [ "**Non-Device Entities**" ] %} {% endif %} {% set lastdev.id = dev.id %} {% endif %} {% set ns.result = ns.result + [ entity.data ] %} {% endfor %} {{ ns.result | join('\n') }} ================================================ FILE: package_unavailable_entities.yaml ================================================ ################################################################################################### ## PACKAGE: Unavailable Entities Sensor v2.4 ## DESCRIPTION: Count and list entities with a state of unknown or unavailable ## REQUIREMENTS: Home Assistant v2024.8 ## USAGE: https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md ################################################################################################### #REQUIRED - List of disabled device entities command_line: sensor: name: Disabled Device Entities unique_id: disabled_device_entities json_attributes: - entities value_template: "{{ value_json.entities | length }}" command: 'jq ''.data.entities |= map(select(.disabled_by? != null) | {entity_id: .entity_id}) | del(.data.deleted_entities) | flatten | .[3]'' < .storage/core.entity_registry' #REQUIRED - Count of unavailable entities template: - sensor: - name: "Unavailable Entities" unique_id: unavailable_entities icon: "{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}" state_class: measurement state: > {% set entities = state_attr('group.unavailable_entities', 'entity_id') %} {{ entities | count if entities != none else -1 }} #REQUIRED - Group of individually ignored entities group: ignored_entities: entities: [] #REQUIRED - Create and update the monitored entities group. Updates once per minute. automation: - id: update_unavailable_entities_group alias: "Update Unavailable Entities Group" description: "Update unavailable entities group." mode: single max_exceeded: silent triggers: - trigger: event event_type: call_service event_data: domain: group service: reload - trigger: time_pattern minutes: "/1" actions: - action: group.set data: object_id: unavailable_entities entities: > {% set ignore_seconds = 60 %} {% set ignore_label = 'ignored' %} {% set ignored_domains = ['button', 'conversation', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt', 'update'] %} {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %} {% set disabled_device_entities = state_attr('sensor.disabled_device_entities', 'entities') | regex_replace(find='\[|\]|\{|\}|\'entity_id\':', replace='') %} {% set ignored_devices = label_devices(ignore_label | lower) %} {% set ignored_device_entities = namespace(value=[]) %} {% for device in ignored_devices %} {% set ignored_device_entities.value = ignored_device_entities.value + device_entities(device) %} {% endfor %} {{ states | rejectattr('domain', 'in', ignored_domains) | rejectattr('entity_id', 'in', disabled_device_entities) | rejectattr('entity_id', 'in', state_attr('group.ignored_entities', 'entity_id')) | rejectattr('entity_id', 'in', ['group.unavailable_entities', 'group.ignored_entities']) | rejectattr('entity_id', 'in', ignored_device_entities.value) | rejectattr('entity_id', 'in', label_entities(ignore_label | lower)) | rejectattr('last_changed', 'ge', ignore_ts) | selectattr('state', 'in', ['unknown', 'unavailable']) | map(attribute='entity_id') | list | sort }} #OPTIONAL - Example notfication automation to demonstrate how you can utilize this sensor. (See example folder for more.) - id: unavailable_entities_notification alias: "Unavailable Entities Notification" description: "Create persistent notification if unavailable entities, dismiss if none." mode: restart triggers: - trigger: state entity_id: group.unavailable_entities attribute: entity_id to: ~ for: 5 # throttle triggers and prevent blank notifications conditions: - condition: template alias: "Sensor state is a valid numerical value" value_template: "{{ is_number(states('sensor.unavailable_entities')) }}" actions: - if: - condition: numeric_state entity_id: sensor.unavailable_entities below: 1 then: - action: persistent_notification.dismiss data: notification_id: unavailable_entities else: - action: persistent_notification.create data: notification_id: unavailable_entities title: "Unavailable Entities" message: "{{ state_attr('group.unavailable_entities', 'entity_id') | join('\n') }}" ================================================ FILE: v1/package_unavailable_entities_v1.yaml ================================================ ################################################################################################### ## PACKAGE: Unavailable Entities Sensor v1.1 ## DESCRIPTION: Count and list entities with a state of unknown or unavailable ## REQUIREMENTS: Home Assistant v2022.5 ## USAGE: 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(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}" state_class: measurement unit_of_measurement: entities state: > {% set entities = state_attr(this.entity_id, 'entity_id') %} {{ entities | count if entities != none else none }} 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',['button', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt']) | 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 }} #OPTIONAL - Add entities you want to ignore to this group. Delete if not using group. group: ignored_unavailable_entities: entities: - sensor.example_ignored_entity #OPTIONAL Example automation to demonstrate how you can utilize this sensor, other examples in examples folder automation: - id: unavailable_entities_notification alias: "Unavailable Entities Notification" description: "Create persistent notification if unavailable entities, dismiss if none." mode: restart trigger: - platform: state entity_id: sensor.unavailable_entities attribute: entity_id to: ~ condition: - condition: template alias: "Sensor state is a valid numerical value" value_template: > {{ is_number(trigger.from_state.state) and is_number(trigger.to_state.state) }} action: - if: - condition: numeric_state entity_id: sensor.unavailable_entities below: 1 then: - service: persistent_notification.dismiss data: notification_id: unavailable_entities else: - service: persistent_notification.create data: notification_id: unavailable_entities title: "Unavailable Entities" message: "{{ state_attr('sensor.unavailable_entities', 'entity_id') | join('\n') }}"