[
  {
    "path": "README.md",
    "content": "[![hacs_badge](https://img.shields.io/badge/HACS-Default-orange.svg)](https://hacs.xyz/)\n\n# Unavailable Entities Template Sensor\n## What does this package do?\nThis 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.\n\nThe sensor can be used to help you discover entities which should to be disabled or deleted and help you identify misbehaving or failed devices.\n\nThe 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)\n\n### Requirements\nHome Assistant v2024.8 is the minimum version required to use this package.\n\nThe [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.\n\n[![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)\n\nThe [fold-entity-row](https://github.com/thomasloven/lovelace-fold-entity-row) plugins is required to use the example lovelace card.\n\n[![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)\n\n## Installation\n### Install As Package\nThe easiest way to use this sensor is to install it as a [package](https://www.home-assistant.io/docs/configuration/packages/).\n\nIf 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.\n\nTo enable packages in your configuation, create a folder in your config directory named `packages` and add the following line to your `configuration.yaml` file.\n\n    homeassistant:\n      packages: !include_dir_named packages\n\n### Install Without Packages\nTo 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.\n\n**NOTE!  You must reload automations, templates, and group entities after adding this package!**\n\n## Excluding Entities\nYou 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.\n\nDisabled 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.\n\n### Customizing The Template\nIn 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.\n\n#### Ignore Domains\n\nDomains 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.\n\nExample - remove the `input_button` domain.\n\n    |rejectattr('domain', 'in', ['button', 'event', 'group', 'input_text', 'scene'])\n\nIf you wish to ignore additional domains you can also add them to the ignored domains filter.\n\nExample - add the `switch` domain.\n\n    |rejectattr('domain', 'in', ['button', 'event', 'group', 'input_button', 'input_text', 'scene', 'switch'])\n\n#### Ignore Specific Entities\n\nTo ignore specific entities, add them to the `group.ignored_unavailable_entities` declaration.\n\n#### Ignore Matching Entities\n\n**Search Test**\n\nIf 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.\n\n    |rejectattr('entity_id', 'search', 'wifi_')\n\nBe 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`.\n\n    - binary_sensor.wifi_connected\n    - sensor.wifi_downstairs\n    - sensor.wifi_upstairs\n\nYou can also use [regex pattern matching](https://regex101.com/) in a seach rejectattr (selectattr) filter.\n\n    |rejectattr('entity_id', 'search', '(_alarm_volume|_next_alarm|_alarms)')\n\nThe filter above effectively combines these three filters.\n\n    |rejectattr('entity_id', 'search', '_alarm_volume')\n    |rejectattr('entity_id', 'search', '_next_alarm')\n    |rejectattr('entity_id', 'search', '_alarms')\n\n**Contains Test**\n\nThe [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.\n\n    |rejectattr('entity_id', 'contains', 'wifi_')\n\n#### Excluding Specific Integrations\n\nYou 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).\n\n    |rejectattr('entity_id', 'in',integration_entities('hassio'))\n\n#### Excluding Specific Devices\n\nYou 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).\n\n    |rejectattr('entity_id', 'in',device_entities('fffe8e4c87c68ee60e0ae84c295676ce'))\n\n### Full Example\n\n    automation:\n      ~~~\n          action:\n            - action: group.set\n              data:\n                object_id: unavailable_entities\n                entities: >\n                  {% set ignore_seconds = 60 %}\n                  {% set ignore_label = 'ignored' %}\n                  {% set ignored_domains = ['button', 'conversation', 'event', 'group', 'image',\n                      'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt', 'update'] %}\n                  {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}\n                  {% set disabled_device_entities = state_attr('sensor.disabled_device_entities', 'entities')\n                      | regex_replace(find='\\[|\\]|\\{|\\}|\\'entity_id\\':', replace='') %}\n                  {% set ignored_devices = label_devices(ignore_label | lower) %}\n                  {% set ignored_device_entities = namespace(value=[]) %}\n                  {% for device in ignored_devices %}\n                  {% set ignored_device_entities.value = ignored_device_entities.value + device_entities(device) %}\n                  {% endfor %}\n                  {{ states\n                      | rejectattr('domain', 'in', ignored_domains)\n                      | rejectattr('entity_id', 'in', disabled_device_entities)\n                      | rejectattr('entity_id', 'in', state_attr('group.ignored_entities', 'entity_id'))\n                      | rejectattr('entity_id', 'in', ['group.unavailable_entities', 'group.ignored_entities'])\n                      | rejectattr('entity_id', 'in', ignored_device_entities.value)\n                      | rejectattr('entity_id', 'in', label_entities(ignore_label | lower))\n                      | rejectattr('last_changed', 'ge', ignore_ts)\n                      | rejectattr('entity_id', 'search', 'browser_')\n                      | rejectattr('entity_id', 'search', '_alarm_volume|_next_alarm|_alarms')\n                      | rejectattr('entity_id', 'contains', '_memory_percent')\n                      | rejectattr('entity_id', 'in', integration_entities('hassio'))\n                      | rejectattr('entity_id', 'in', device_entities('fffe8e4c87c68ee60e0ae84c295676ce'))\n                      | selectattr('state', 'in', ['unknown', 'unavailable'])\n                      | map(attribute='entity_id') | list | sort }}\n\nSee [Home Assistant Templating](https://www.home-assistant.io/docs/configuration/templating/) for additional options.\n\n### Specifing Entities to Monitor - Custom Sensors\n\nYou 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.\n\nThis 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.\n\n### Example\n\n    template:\n      - sensor:\n          - name: \"Unavailable Shelly Power Entities\"\n            unique_id: unavailable_shelly_power_entities\n            icon: \"{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}\"\n            state_class: measurement\n            state: >\n              {% set entities = state_attr('group.unavailable_shelly_power_entities', 'entity_id') %}\n              {{ entities | count if entities != none else -1 }}\n\n    automation:\n      ~~~\n          action:\n            - action: group.set\n              data:\n                object_id: unavailable_shelly_power_entities\n                entities: >\n                    {{ states.sensor\n                        | selectattr('entity_id', 'in', integration_entities('shelly'))\n                        | selectattr('entity_id', 'contains', '_power')\n                        | selectattr('state', 'in', ['unknown', 'unavailable'])\n                        | map(attribute='entity_id') | list | sort }}\n\n## Using With Automations\nThere 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.\n\n## Display in the UI\nTo display a list of unavailable entities open the more-info dialogue of group.unavailable_entities.\n\n![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/group_more_info.png)\n\n#### Using Auto Entities and Fold Entity Row\nUsing 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.\n\n[Example Entities Card](https://github.com/jazzyisj/unavailable-entities-sensor/blob/master/examples/auto_entities_card.yaml)\n\n*Closed Fold Entity Row*\n\n![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/entities_card_closed_example.png)\n\n*Open Fold Entity Row*\n\n![Example](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/images/entities_card_open_example.png)\n"
  },
  {
    "path": "examples/auto_entities_card.yaml",
    "content": "###################################################################################################\n## DESCRIPTION: Using unavailable entities sensor with an auto entities card and fold entity row\n## https://github.com/thomasloven/lovelace-auto-entities\n## https://github.com/thomasloven/lovelace-fold-entity-row\n###################################################################################################\ntype: entities\ntitle: \"Unavailable Entities Example\"\nstate_color: true\nshow_header_toggle: false\nentities:\n  - type: custom:auto-entities\n    show_empty: true\n    unique: true\n    filter:\n      include:\n        - group: group.unavailable_entities\n    sort:\n      method: state\n    card:\n      type: custom:fold-entity-row\n      padding: 0\n      head:\n        entity: sensor.unavailable_entities"
  },
  {
    "path": "examples/detailed_persistent_notification.yaml",
    "content": "###################################################################################################\n## DESCRIPTION: Detailed persistent notification message - courtesy of @ThomDietrich and @warthog9\n###################################################################################################\nautomation:\n  - id: unavailable_entities_notification\n    alias: \"Unavailable Entities Notification\"\n    description: \"Create persistent notification if unavailable entities, dismiss if none.\"\n    mode: restart\n    triggers:\n      - trigger: state\n        entity_id: group.unavailable_entities\n        attribute: entity_id\n        to: ~\n        for: 5 # throttle triggers and prevent blank notifications\n    conditions:\n      - condition: template\n        alias: \"Sensor state is a valid numerical value\"\n        value_template: \"{{ is_number(states('sensor.unavailable_entities')) }}\"\n    actions:\n      - action: persistent_notification.create\n        data:\n          notification_id: unavailable_entities\n          title: \"Unavailable Entities\"\n          message: >\n            {% set ns = namespace(result=[]) %}\n            {% for s in expand(state_attr('group.unavailable_entities', 'entity_id')) %}\n              {% set ns.result = ns.result + [\n                  device_attr(s.entity_id, \"name\") ~ \"|\" ~ device_id(s.entity_id) ~ \"|- **\" ~ s.name ~ \"**\\n\"\n                  ~ \"  - *entity_id*: \" ~ s.entity_id ~ \"\\n\"\n                  ~ \"  - *state*: \" ~ s.state ~ \"\\n\"\n                ]\n              %}\n            {% endfor %}\n            {% set ns.result = ns.result | sort %}\n            {% set lastdev = namespace( id=\"\" ) %}\n            {% set tarr = ns.result %}\n            {% set ns.result = [] %}\n            {% for item in tarr %}\n              {% set dev = namespace( id=\"\" ) %}\n              {% set entity = namespace( data=\"\" ) %}\n              {% set dev.id = item.split(\"|\")[1] %}\n              {% set entity.data = item.split(\"|\")[2] %}\n              {% if lastdev.id != dev.id %}\n                {% if dev.id != 'None' %}\n                  {% set ns.result = ns.result + [ \"**<a href=\\\"/config/devices/device/\" ~ dev.id ~ \"\\\">\" ~ device_attr(dev.id, \"name\") ~ \"</a>**\" ] %}\n                {% else %}\n                  {% set ns.result = ns.result + [ \"**Non-Device Entities**\" ] %}\n                {% endif %}\n                {% set lastdev.id = dev.id %}\n              {% endif %}\n              {% set ns.result = ns.result + [ entity.data ] %}\n            {% endfor %}\n            {{ ns.result | join('\\n') }}\n"
  },
  {
    "path": "package_unavailable_entities.yaml",
    "content": "###################################################################################################\n## PACKAGE: Unavailable Entities Sensor v2.4\n## DESCRIPTION: Count and list entities with a state of unknown or unavailable\n## REQUIREMENTS: Home Assistant v2024.8\n## USAGE: https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md\n###################################################################################################\n\n#REQUIRED - List of disabled device entities\ncommand_line:\n  sensor:\n      name: Disabled Device Entities\n      unique_id: disabled_device_entities\n      json_attributes:\n        - entities\n      value_template: \"{{ value_json.entities | length }}\"\n      command: 'jq ''.data.entities |= map(select(.disabled_by? != null) | {entity_id: .entity_id}) | del(.data.deleted_entities) | flatten | .[3]'' < .storage/core.entity_registry'\n\n#REQUIRED - Count of unavailable entities\ntemplate:\n  - sensor:\n      - name: \"Unavailable Entities\"\n        unique_id: unavailable_entities\n        icon: \"{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}\"\n        state_class: measurement\n        state: >\n          {% set entities = state_attr('group.unavailable_entities', 'entity_id') %}\n          {{ entities | count if entities != none else -1 }}\n\n#REQUIRED - Group of individually ignored entities\ngroup:\n  ignored_entities:\n    entities: []\n\n#REQUIRED - Create and update the monitored entities group. Updates once per minute.\nautomation:\n  - id: update_unavailable_entities_group\n    alias: \"Update Unavailable Entities Group\"\n    description: \"Update unavailable entities group.\"\n    mode: single\n    max_exceeded: silent\n    triggers:\n      - trigger: event\n        event_type: call_service\n        event_data:\n          domain: group\n          service: reload\n\n      - trigger: time_pattern\n        minutes: \"/1\"\n    actions:\n      - action: group.set\n        data:\n          object_id: unavailable_entities\n          entities: >\n            {% set ignore_seconds = 60 %}\n            {% set ignore_label = 'ignored' %}\n            {% set ignored_domains = ['button', 'conversation', 'event', 'group', 'image',\n                'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt', 'update'] %}\n            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}\n            {% set disabled_device_entities = state_attr('sensor.disabled_device_entities', 'entities')\n                | regex_replace(find='\\[|\\]|\\{|\\}|\\'entity_id\\':', replace='') %}\n            {% set ignored_devices = label_devices(ignore_label | lower) %}\n            {% set ignored_device_entities = namespace(value=[]) %}\n            {% for device in ignored_devices %}\n              {% set ignored_device_entities.value = ignored_device_entities.value + device_entities(device) %}\n            {% endfor %}\n            {{ states\n                | rejectattr('domain', 'in', ignored_domains)\n                | rejectattr('entity_id', 'in', disabled_device_entities)\n                | rejectattr('entity_id', 'in', state_attr('group.ignored_entities', 'entity_id'))\n                | rejectattr('entity_id', 'in', ['group.unavailable_entities', 'group.ignored_entities'])\n                | rejectattr('entity_id', 'in', ignored_device_entities.value)\n                | rejectattr('entity_id', 'in', label_entities(ignore_label | lower))\n                | rejectattr('last_changed', 'ge', ignore_ts)\n                | selectattr('state', 'in', ['unknown', 'unavailable'])\n                | map(attribute='entity_id') | list | sort }}\n\n  #OPTIONAL - Example notfication automation to demonstrate how you can utilize this sensor. (See example folder for more.)\n  - id: unavailable_entities_notification\n    alias: \"Unavailable Entities Notification\"\n    description: \"Create persistent notification if unavailable entities, dismiss if none.\"\n    mode: restart\n    triggers:\n      - trigger: state\n        entity_id: group.unavailable_entities\n        attribute: entity_id\n        to: ~\n        for: 5 # throttle triggers and prevent blank notifications\n    conditions:\n      - condition: template\n        alias: \"Sensor state is a valid numerical value\"\n        value_template: \"{{ is_number(states('sensor.unavailable_entities')) }}\"\n    actions:\n      - if:\n          - condition: numeric_state\n            entity_id: sensor.unavailable_entities\n            below: 1\n        then:\n          - action: persistent_notification.dismiss\n            data:\n              notification_id: unavailable_entities\n        else:\n          - action: persistent_notification.create\n            data:\n              notification_id: unavailable_entities\n              title: \"Unavailable Entities\"\n              message: \"{{ state_attr('group.unavailable_entities', 'entity_id') | join('\\n') }}\"\n"
  },
  {
    "path": "v1/package_unavailable_entities_v1.yaml",
    "content": "###################################################################################################\n## PACKAGE: Unavailable Entities Sensor v1.1\n## DESCRIPTION: Count and list entities with a state of unknown or unavailable\n## REQUIREMENTS: Home Assistant v2022.5\n## USAGE: https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md\n###################################################################################################\n\n#REQUIRED - This is the template sensor\ntemplate:\n  - sensor:\n      - name: \"Unavailable Entities\"\n        unique_id: unavailable_entities\n        icon: \"{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}\"\n        state_class: measurement\n        unit_of_measurement: entities\n        state: >\n          {% set entities = state_attr(this.entity_id, 'entity_id') %}\n          {{ entities | count if entities != none else none }}\n        attributes:\n          entity_id: >\n            {% set ignore_seconds = 60 %}\n            {% set ignored = state_attr('group.ignored_unavailable_entities', 'entity_id') %}\n            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}\n            {% set entities = states\n                | rejectattr('domain','in',['button', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt'])\n                | rejectattr('last_changed', 'ge', ignore_ts) %}\n            {% set entities =  entities | rejectattr('entity_id', 'in', ignored) if ignored != none else entities %}\n            {{ entities | map(attribute='entity_id') | reject('has_value') | list | sort }}\n\n#OPTIONAL - Add entities you want to ignore to this group.  Delete if not using group.\ngroup:\n  ignored_unavailable_entities:\n    entities:\n      - sensor.example_ignored_entity\n\n#OPTIONAL Example automation to demonstrate how you can utilize this sensor, other examples in examples folder\nautomation:\n  - id: unavailable_entities_notification\n    alias: \"Unavailable Entities Notification\"\n    description: \"Create persistent notification if unavailable entities, dismiss if none.\"\n    mode: restart\n    trigger:\n      - platform: state\n        entity_id: sensor.unavailable_entities\n        attribute: entity_id\n        to: ~\n    condition:\n      - condition: template\n        alias: \"Sensor state is a valid numerical value\"\n        value_template: >\n          {{ is_number(trigger.from_state.state)\n              and is_number(trigger.to_state.state) }}\n    action:\n      - if:\n          - condition: numeric_state\n            entity_id: sensor.unavailable_entities\n            below: 1\n        then:\n          - service: persistent_notification.dismiss\n            data:\n              notification_id: unavailable_entities\n        else:\n          - service: persistent_notification.create\n            data:\n              notification_id: unavailable_entities\n              title: \"Unavailable Entities\"\n              message: \"{{ state_attr('sensor.unavailable_entities', 'entity_id') | join('\\n') }}\""
  }
]