Skip to content

Instantly share code, notes, and snippets.

@TheQue42
Last active October 15, 2022 09:03
Show Gist options
  • Save TheQue42/2f007f3ef4d7f4029861dc2f14bd878d to your computer and use it in GitHub Desktop.
Save TheQue42/2f007f3ef4d7f4029861dc2f14bd878d to your computer and use it in GitHub Desktop.
Report/Detect unavailable sensors and binary sensors (non)matching a regular expression
blueprint:
name: Unavailability Detector
description: Regularly test all sensors for unavailability or "unknown"-ness.
domain: automation
input:
when_hours:
name: The time pattern (hour) of when to run.
description: >
https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger
default: '*'
selector:
text:
multiline: false
when_minutes:
name: The time pattern (minute) of when to run.
description: >
https://www.home-assistant.io/docs/automation/trigger/#time-pattern-trigger
default: '30'
selector:
text:
multiline: false
exclude:
name: Excluded Sensors
description: >
There are some weird sensors out there, we might want to ignore.
Only entities are supported, devices must be expanded
default: []
selector:
entity:
multiple: true
domain:
- binary_sensor
- sensor
exclude_regexp:
name: Excluded Sensors, RegExp
description: >
There are some weird sensors out there, we might want to ignore.
default: 'DontMatchAnySensorsAtAll'
selector:
text:
multiline: false
actions:
name: Actions
description: 'Notifications or similar to be run. {{sensors}} is replaced with the names[] of devices indicating a problem.'
selector:
action: {}
variables:
exclude: !input 'exclude'
exclude_regexp: !input 'exclude_regexp'
sensors: >-
{%- set result = namespace(sensors=[]) -%}
{%- for s in states.sensor -%}
{%- if s.state is search("unavail|unknown") and (not s.entity_id in exclude and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{% endif -%}
{%- endfor -%}
{%- for s in states.binary_sensor -%}
{%- if s.state is search("unavail|unknown") and (not s.entity_id in exclude and not s.entity_id is search(exclude_regexp)) -%}
{%- set result.sensors = result.sensors + [s.entity_id] -%}
{%- endif -%}
{%- endfor -%}
{{result.sensors}}
trigger:
- platform: time_pattern
minutes: !input 'when_minutes'
hours: !input 'when_hours'
condition:
- '{{ sensors|length != 0 }}'
action:
- choose: []
default: !input 'actions'
mode: single
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment