Skip to content

Instantly share code, notes, and snippets.

@JohnNeville
Last active July 11, 2023 00:50
Show Gist options
  • Save JohnNeville/9a0e986a8e38e06adef853fc3ef09062 to your computer and use it in GitHub Desktop.
Save JohnNeville/9a0e986a8e38e06adef853fc3ef09062 to your computer and use it in GitHub Desktop.
blueprint:
name: Leak Checker and Notifier w/ Exclusions
description: |2
Checks all binary_sensors with the 'moisture' device_class for Wet Status.
Devices/entities that are not assigned to an area,
or are assigned to areas where the name begins with an underscore (_),
or are explicitly excluded, will not be monitored.
To monitor devices that are not fixed to a specific area, simply create a pseudo-area, such as "Roaming" or "Nowhere" and add the devices to that area.
If you wish to include the alarming sensor in your notification you can use the template {{friendly_message}}
domain: automation
source_url: https://gist.github.com/JohnNeville/9a0e986a8e38e06adef853fc3ef09062
input:
exclude:
name: Exclusions
description: Areas/devices/entities to exclude from monitoring.
default: []
selector:
target:
actions:
name: Actions
description: Actions to run, such as notifications. Matched devices are returned with the <code>{{items}}</code> dictionary.
selector:
action:
variables:
exclude: !input exclude
all_moisture_items: >
{% set entities = namespace(data=[]) %}
{% set devices = namespace(data=[]) %}
{# Loop through all binary_sensor states with the 'moisture' device_class. #}
{% for state in states.binary_sensor
| rejectattr('attributes.device_class', 'undefined')
| selectattr('attributes.device_class', 'eq', 'moisture')
| selectattr('state', 'eq', 'on') %}
{% set add_entity = true %}
{# Skip devices with out an area or an area that starts with '_' #}
{% if area_id(state.entity_id) == None or area_name(state.entity_id)[0:1] == '_' %}
{% set add_entity = false %}
{% endif %}
{# Manage Excluded Entities, Areas, and Devices #}
{% if exclude is defined %}
{% if exclude.entity_id is defined
and state.entity_id in exclude.entity_id %}
{% set add_entity = false %}
{% endif %}
{% if exclude.area_id is defined
and area_id(state.entity_id) in exclude.area_id %}
{% set add_entity = false %}
{% endif %}
{% endif %}
{% if add_entity == true %}
{# Add found entities to array. #}
{% set entities.data = entities.data
+ [{'entity_id': state.entity_id,
'device_id': device_id(state.entity_id),
'area_id': area_id(state.entity_id),
'state': state.state}] %}
{% endif %}
{% endfor %}
{# Loop through all binary_sensor entities with the 'moisture' device_class. #}
{% for state in states.binary_sensor
| rejectattr('state', 'eq', 'unavailable')
| rejectattr('attributes.device_class', 'undefined')
| selectattr('attributes.device_class', 'eq', 'moisture') %}
{% set add_entity = true %}
{# Skip devices with out an area or an area that starts with '_' #}
{% if area_id(state.entity_id) == None or area_name(state.entity_id)[0:1] == '_' %}
{% set add_entity = false %}
{% endif %}
{# Manage Excluded Entities, Areas, and Devices #}
{% if exclude is defined %}
{% if exclude.entity_id is defined and state.entity_id in exclude.entity_id %}
{% set add_entity = false %}
{% endif %}
{% if exclude.area_id is defined and area_id(state.entity_id) in exclude.area_id %}
{% set add_entity = false %}
{% endif %}
{% endif %}
{% if add_entity == true %}
{# Add found entities to array. #}
{% set entities.data = entities.data +
[{'entity_id': state.entity_id,
'device_id': device_id(state.entity_id),
'area_id': area_id(state.entity_id),
'state': state.state}] %}
{% endif %}
{% endfor %}
{# Loop through all unique devices from the entities array. #}
{% for entity in entities.data | unique(attribute='device_id') %}
{% set add_entity = true %}
{% if area_id(entity.device_id) == None
or area_name(entity.device_id)[0:1] == '_' %}
{% set add_entity = false %}
{% endif %}
{% if exclude is defined %}
{% if exclude.device_id is defined
and entity.device_id in exclude.device_id %}
{% set add_entity = false %}
{% endif %}
{% if exclude.area_id is defined
and area_id(entity.device_id) in exclude.area_id %}
{% set add_entity = false %}
{% endif %}
{% endif %}
{% if add_entity == true %}
{# Set default friendly_name. #}
{% set device_friendly_name = device_attr(entity.device_id, 'name') %}
{% if state_attr(entity.entity_id, 'friendly_name') %}
{# Set to entity friendly_name. #}
{% set device_friendly_name = state_attr(entity.entity_id, 'friendly_name') %}
{% endif %}
{% if device_attr(entity.device_id, 'name_by_user') %}
{# Set to user given friendly_name. #}
{% set device_friendly_name = device_attr(entity.device_id, 'name_by_user') %}
{% endif %}
{# Add device to array. #}
{% set devices.data = devices.data
+ [{'device_id': entity.device_id,
'device_name': device_attr(entity.entity_id, 'name'),
'friendly_name': device_friendly_name,
'area_id': area_id(entity.entity_id),
'state': entity.state}] %}
{% endif %}
{% endfor %}
{{devices.data}}
leak_items: >
{% set leak_items = namespace(data=[]) %} {% for item in
all_moisture_items -%}
{% if item.state == 'on' %}
{% set leak_items.data = leak_items.data
+ [item] %}
{% endif %}
{%- endfor %}
{{leak_items.data }}
friendly_message: >
The following devices have been identified as leaking:
{% for item in leak_items -%}
{{ item.friendly_name }}{%- if not loop.last %},{{- '\n' -}} {% else %}. {% endif -%}
{%- endfor %}
should_trigger: >
{{leak_items | length > 0}}
trigger:
- event_data: {}
event_type: state_changed
platform: event
condition:
- condition: template
value_template: '{{ trigger.event.data.new_state.attributes.device_class == "moisture" }}'
- condition: template
value_template: '{{ trigger.event.data.new_state.state == "on" }}'
- condition: template
value_template: '{{ should_trigger }}'
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