Skip to content

Commit df9bbf2

Browse files
committed
Update to v2.0
Iterate group.unavailable_entities instead of states object.
1 parent e003e80 commit df9bbf2

5 files changed

+134
-42
lines changed

README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# ATTENTION! THE STRUCTURE FOR THIS SENSOR HAS CHANGED!
2+
# DOCS HAVE NOT BEEN UPDATED YET
3+
4+
The old version is here [`package_unavailable_entities_old.yaml`](https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/package_unavailable_entities_old.yaml).
5+
16
## What does this template sensor do?
27
This sensor iterates the state object and returns entities that have a state of unknown or unavailable.
38

@@ -17,7 +22,7 @@ To enable packages in your configuation, create a folder in your config director
1722
homeassistant:
1823
packages: !include_dir_named packages
1924
### Install Without Packages
20-
To create this sensor without using packages simply copy the relevant template code to an appropriate place in your configuration.yaml file. The ignored entities group and example automation are optional.
25+
To create this sensor without using packages simply copy the relevant template code to an appropriate place in your configuration.yaml file. The ignored entities group and example automation are optional.
2126

2227
**NOTE! You must reload templates, group entities (if you are using the ignored entities group), and automations (if you are utilizing the example automation) after adding the package or code to your configuration.**
2328
## Customizing The Sensor
@@ -26,7 +31,7 @@ To change the time the sensor will ignore newly available entities that become u
2631

2732
**A value for 'ignore_seconds' less than 5 seconds may cause template loop warnings in your home assistant log, particularly when template sensors are reloaded.**
2833
### Ignore Domains
29-
Stateless domains (button, scene etc.) are excluded by default. The group domain is also excluded as many groups will always have a state of `unknown`.
34+
Stateless domains (button, scene etc.) are excluded by default. The group domain is also excluded as many groups will always have a state of `unknown`.
3035

3136
To track these domains remove them from the ignored domains filter.
3237

@@ -101,18 +106,18 @@ You can exclude entities from a specific integration by using an `in` test for t
101106
{% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
102107
{% set entities = states
103108
|rejectattr('domain','in',['button','event','group','input_button','input_text','scene'])
104-
|rejectattr('entity_id','search','browser_')
109+
|rejectattr('entity_id','search','browser_')
105110
|rejectattr('entity_id','search','_alarm_volume|_next_alarm|_alarms')
106111
|rejectattr('entity_id','contains','_memory_percent')
107112
|rejectattr('entity_id','in',integration_entities('hassio'))
108-
|rejectattr('entity_id','in',device_entities('fffe8e4c87c68ee60e0ae84c295676ce'))
113+
|rejectattr('entity_id','in',device_entities('fffe8e4c87c68ee60e0ae84c295676ce'))
109114
|rejectattr('last_changed','ge',ignore_ts) %}
110115
{% set entities = entities|rejectattr('entity_id','in',ignored) if ignored != none else entities %}
111116
{{ entities|map(attribute='entity_id')|reject('has_value')|list|sort }}
112117

113118
See [Home Assistant Templating](https://www.home-assistant.io/docs/configuration/templating/) additional options.
114119
### Specifing Entities to Monitor
115-
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, select filters are cumlative and entities may be already excluded by previous filters.
120+
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.
116121

117122
This example monitors only the `sensor` domain from the Shelly integration that contain the string "_power" in their entity_id.
118123
## Example

examples/auto_entities_card.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ entities:
1212
show_empty: true
1313
unique: true
1414
filter:
15-
template: "{{ state_attr('sensor.unavailable_entities','entity_id') }}"
15+
include:
16+
- group: group.unavailable_entities
1617
sort:
1718
method: state
1819
card:

examples/detailed_persistent_notification.yaml

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ automation:
77
description: "Create persistent notification if unavailable entities, dismiss if none."
88
mode: restart
99
trigger:
10-
- platform: state
11-
entity_id: sensor.unavailable_entities
10+
- trigger: state
11+
entity_id: group.unavailable_entities
1212
attribute: entity_id
1313
to: ~
14+
for: 5 # prevent blank notifications
1415
condition:
1516
- condition: template
1617
alias: "Sensor state is a valid numerical value"
17-
value_template: >
18-
{{ is_number(trigger.from_state.state)
19-
and is_number(trigger.to_state.state) }}
18+
value_template: "{{ is_number(states('sensor.unavailable_entities')) }}"
2019
action:
2120
- service: persistent_notification.create
2221
data:
@@ -51,4 +50,4 @@ automation:
5150
{% endif %}
5251
{% set ns.result = ns.result + [ entity.data ] %}
5352
{% endfor %}
54-
{{ ns.result | join('\n') }}
53+
{{ ns.result | join('\n') }}

package_unavailable_entities.yaml

+47-30
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,87 @@
11
###################################################################################################
2-
## PACKAGE: Unavailable Entities Sensor
2+
## PACKAGE: Unavailable Entities Sensor v2.0
33
## DESCRIPTION: Count and list entities with a state of unknown or unavailable
44
## REQUIREMENTS: Home Assistant v2022.5
55
## USAGE: https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md
66
###################################################################################################
77

88
# REQUIRED - This is the template sensor
9-
109
template:
1110
- sensor:
1211
- name: "Unavailable Entities"
1312
unique_id: unavailable_entities
1413
icon: "{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}"
1514
state_class: measurement
16-
unit_of_measurement: entities
1715
state: >
18-
{% set entities = state_attr(this.entity_id, 'entity_id') %}
19-
{{ entities | count if entities != none else none }}
20-
attributes:
21-
entity_id: >
22-
{% set ignore_seconds = 60 %}
23-
{% set ignored = state_attr('group.ignored_unavailable_entities', 'entity_id') %}
24-
{% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
25-
{% set entities = states
26-
| rejectattr('domain','in',['button', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt'])
27-
| rejectattr('last_changed', 'ge', ignore_ts) %}
28-
{% set entities = entities | rejectattr('entity_id', 'in', ignored) if ignored != none else entities %}
29-
{{ entities | map(attribute='entity_id') | reject('has_value') | list | sort }}
30-
31-
# OPTIONAL - Add entities you want to ignore to this group. Delete if not using group.
16+
{% set entities = state_attr('group.unavailable_entities', 'entity_id') %}
17+
{{ entities | count if entities != none else -1 }}
3218
19+
# REQUIRED - Add individual entities to ignore to this group.
3320
group:
34-
ignored_unavailable_entities:
35-
entities:
36-
- sensor.example_ignored_entity
37-
38-
# OPTIONAL Example automation to demonstrate how you can utilize this sensor, other examples in examples folder
21+
ignored_entities:
22+
entities: []
3923

24+
# REQUIRED - This is required to create and update the monitored entities group. Sensor updates once every minute.
4025
automation:
26+
- id: update_unavailable_entities_group
27+
alias: "Update Unavailable Entities Group"
28+
description: "Update unavailable entities group."
29+
mode: single
30+
max_exceeded: silent
31+
trigger:
32+
- trigger: homeassistant
33+
id: startup
34+
event: start
35+
36+
- trigger: event
37+
event_type: call_service
38+
event_data:
39+
domain: group
40+
service: reload
41+
42+
- trigger: time_pattern
43+
minutes: "/1"
44+
action:
45+
# IMPORTANT - This is the template to edit to exclude entities with filters.
46+
- action: group.set
47+
data:
48+
object_id: unavailable_entities
49+
entities: >
50+
{{ states
51+
| selectattr('state', 'in', ['unknown', 'unavailable'])
52+
| rejectattr('domain', 'in', ['button', 'conversation', 'event', 'group', 'image',
53+
'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt'])
54+
| rejectattr('entity_id', 'in', state_attr('group.ignored_entities', 'entity_id'))
55+
| map(attribute='entity_id') | list | sort }}
56+
57+
# OPTIONAL - Example automation to demonstrate how you can utilize this sensor, see example folder for more.
4158
- id: unavailable_entities_notification
4259
alias: "Unavailable Entities Notification"
4360
description: "Create persistent notification if unavailable entities, dismiss if none."
4461
mode: restart
4562
trigger:
46-
- platform: state
47-
entity_id: sensor.unavailable_entities
63+
- trigger: state
64+
entity_id: group.unavailable_entities
4865
attribute: entity_id
4966
to: ~
67+
for: 5 # prevent blank notifications
5068
condition:
5169
- condition: template
5270
alias: "Sensor state is a valid numerical value"
53-
value_template: >
54-
{{ is_number(trigger.from_state.state)
55-
and is_number(trigger.to_state.state) }}
71+
value_template: "{{ is_number(states('sensor.unavailable_entities')) }}"
5672
action:
5773
- if:
5874
- condition: numeric_state
5975
entity_id: sensor.unavailable_entities
6076
below: 1
6177
then:
62-
- service: persistent_notification.dismiss
78+
- action: persistent_notification.dismiss
6379
data:
6480
notification_id: unavailable_entities
6581
else:
66-
- service: persistent_notification.create
82+
- action: persistent_notification.create
6783
data:
6884
notification_id: unavailable_entities
6985
title: "Unavailable Entities"
70-
message: "{{ state_attr('sensor.unavailable_entities', 'entity_id') | join('\n') }}"
86+
message: "{{ state_attr('group.unavailable_entities', 'entity_id') | join('\n') }}"
87+

package_unavailable_entities_old.yaml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
###################################################################################################
2+
## PACKAGE: Unavailable Entities Sensor v1.1
3+
## DESCRIPTION: Count and list entities with a state of unknown or unavailable
4+
## REQUIREMENTS: Home Assistant v2022.5
5+
## USAGE: https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md
6+
###################################################################################################
7+
8+
# REQUIRED - This is the template sensor
9+
10+
template:
11+
- sensor:
12+
- name: "Unavailable Entities"
13+
unique_id: unavailable_entities
14+
icon: "{{ iif(states(this.entity_id)|int(-1) > 0, 'mdi:alert-circle', 'mdi:check-circle') }}"
15+
state_class: measurement
16+
unit_of_measurement: entities
17+
state: >
18+
{% set entities = state_attr(this.entity_id, 'entity_id') %}
19+
{{ entities | count if entities != none else none }}
20+
attributes:
21+
entity_id: >
22+
{% set ignore_seconds = 60 %}
23+
{% set ignored = state_attr('group.ignored_unavailable_entities', 'entity_id') %}
24+
{% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
25+
{% set entities = states
26+
| rejectattr('domain','in',['button', 'event', 'group', 'image', 'input_button', 'input_text', 'remote', 'tts', 'scene', 'stt'])
27+
| rejectattr('last_changed', 'ge', ignore_ts) %}
28+
{% set entities = entities | rejectattr('entity_id', 'in', ignored) if ignored != none else entities %}
29+
{{ entities | map(attribute='entity_id') | reject('has_value') | list | sort }}
30+
31+
# OPTIONAL - Add entities you want to ignore to this group. Delete if not using group.
32+
33+
group:
34+
ignored_unavailable_entities:
35+
entities:
36+
- sensor.example_ignored_entity
37+
38+
# OPTIONAL Example automation to demonstrate how you can utilize this sensor, other examples in examples folder
39+
40+
automation:
41+
- id: unavailable_entities_notification
42+
alias: "Unavailable Entities Notification"
43+
description: "Create persistent notification if unavailable entities, dismiss if none."
44+
mode: restart
45+
trigger:
46+
- platform: state
47+
entity_id: sensor.unavailable_entities
48+
attribute: entity_id
49+
to: ~
50+
condition:
51+
- condition: template
52+
alias: "Sensor state is a valid numerical value"
53+
value_template: >
54+
{{ is_number(trigger.from_state.state)
55+
and is_number(trigger.to_state.state) }}
56+
action:
57+
- if:
58+
- condition: numeric_state
59+
entity_id: sensor.unavailable_entities
60+
below: 1
61+
then:
62+
- service: persistent_notification.dismiss
63+
data:
64+
notification_id: unavailable_entities
65+
else:
66+
- service: persistent_notification.create
67+
data:
68+
notification_id: unavailable_entities
69+
title: "Unavailable Entities"
70+
message: "{{ state_attr('sensor.unavailable_entities', 'entity_id') | join('\n') }}"

0 commit comments

Comments
 (0)