forked from jazzyisj/unavailable-entities-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackage_unavailable_entities.yaml
108 lines (99 loc) · 4.77 KB
/
package_unavailable_entities.yaml
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
108
###################################################################################################
## Package - Unavailable Entities Sensor
## Count and list entities with a state of unknown or unavailable
## 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(-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','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 }}
# OPTIONAL - Add entities you want to ignore to this group.
group:
ignored_unavailable_entities:
entities:
- sensor.example_ignored_entity
# OPTIONAL Example automation to demonstrate how you can utilize this sensor
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') }}"
# OPTIONAL Detailed persistent notification message - courtesy of @ThomDietrich and @warthog9
# - service: persistent_notification.create
# data:
# notification_id: unavailable_entities2
# title: "Unavailable Entities"
# message: >
# {% set ns = namespace(result=[]) %}
# {% for s in expand(state_attr('sensor.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 + [ "**<a href=\"/config/devices/device/" ~ dev.id ~ "\">" ~ device_attr(dev.id, "name") ~ "</a>**" ] %}
# {% 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') }}