-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathpackage_unavailable_entities.yaml
More file actions
107 lines (101 loc) · 4.74 KB
/
Copy pathpackage_unavailable_entities.yaml
File metadata and controls
107 lines (101 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
###################################################################################################
## 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') }}"