From 2f01fc90cf73ea08d648f36e1a93fad927e3e1f1 Mon Sep 17 00:00:00 2001 From: Brian Hanifin Date: Wed, 4 Oct 2023 10:25:19 -0700 Subject: [PATCH] improved contact sensor count logic --- ...brianhanifin_template_sensor_helpers.jinja | 3 +- .../contact_sensors/contact_sensors_open.yaml | 28 +++++++++++++------ .../sensors/contact_sensors/doors_open.yaml | 14 ++-------- .../sensors/contact_sensors/windows_open.yaml | 26 ++++++++++------- .../windows_open_downstairs.yaml | 19 +------------ .../windows_open_upstairs.yaml | 19 +------------ 6 files changed, 40 insertions(+), 69 deletions(-) diff --git a/custom_templates/brianhanifin_template_sensor_helpers.jinja b/custom_templates/brianhanifin_template_sensor_helpers.jinja index 023059f7..b644d0d8 100644 --- a/custom_templates/brianhanifin_template_sensor_helpers.jinja +++ b/custom_templates/brianhanifin_template_sensor_helpers.jinja @@ -6,9 +6,8 @@ | selectattr( "entity_id","in",state_attr(sensor_group,"entity_id") ) + | rejectattr("state", "in", ["unavailable","unknown"]) | selectattr("state","eq",value) - | rejectattr("state","eq","unavailable") - | rejectattr("state","eq","unknown") | map(attribute="entity_id") | list | sort diff --git a/integration_entities/templates/sensors/contact_sensors/contact_sensors_open.yaml b/integration_entities/templates/sensors/contact_sensors/contact_sensors_open.yaml index 9956f955..6fce067d 100644 --- a/integration_entities/templates/sensors/contact_sensors/contact_sensors_open.yaml +++ b/integration_entities/templates/sensors/contact_sensors/contact_sensors_open.yaml @@ -7,12 +7,22 @@ state: | {{ this.attributes.entities | default([]) | count | default(0) }} attributes: entities: | - {%- set doors_open = state_attr("sensor.doors_open","entities")|default([]) %} - {%- set windows_open = state_attr("sensor.windows_open","entities")|default([]) %} - {%- if doors_open|count > 0 and windows_open|count > 0 %} - {{ doors_open + windows_open }} - {%- elif doors_open|count > 0 %} - {{ doors_open }} - {%- elif windows_open|count > 0 %} - {{ windows_open }} - {%- endif %} + {% set result = states.binary_sensor + | selectattr( + "entity_id","in",state_attr("sensor.doors_open","entities") + ) + | rejectattr("state", "in", ["unavailable","unknown"]) + | selectattr("state","eq","on") + | map(attribute="entity_id") + | list + + + states.binary_sensor + | selectattr( + "entity_id","in",state_attr("sensor.windows_open","entities") + ) + | rejectattr("state", "in", ["unavailable","unknown"]) + | selectattr("state","eq","on") + | map(attribute="entity_id") + | list + %} + {{ result | sort }} diff --git a/integration_entities/templates/sensors/contact_sensors/doors_open.yaml b/integration_entities/templates/sensors/contact_sensors/doors_open.yaml index c99d2c57..454310fc 100644 --- a/integration_entities/templates/sensors/contact_sensors/doors_open.yaml +++ b/integration_entities/templates/sensors/contact_sensors/doors_open.yaml @@ -7,15 +7,5 @@ state: | {{ this.attributes.entities | default([]) | count | default(0) }} attributes: entities: | - {{ - states.binary_sensor - | selectattr( - "entity_id","in",state_attr("binary_sensor.house_doors","entity_id") - ) - | selectattr("state","eq","on") - | rejectattr("state","eq","unavailable") - | rejectattr("state","eq","unknown") - | map(attribute="entity_id") - | list - | sort - }} + {% from "brianhanifin_template_sensor_helpers.jinja" import get_sensors_from_group_with_value %} + {{ get_sensors_from_group_with_value("binary_sensor.house_doors","on") }} diff --git a/integration_entities/templates/sensors/contact_sensors/windows_open.yaml b/integration_entities/templates/sensors/contact_sensors/windows_open.yaml index b9011b5b..dd27c11f 100644 --- a/integration_entities/templates/sensors/contact_sensors/windows_open.yaml +++ b/integration_entities/templates/sensors/contact_sensors/windows_open.yaml @@ -7,16 +7,22 @@ state: | {{ this.attributes.entities | default([]) | count | default(0) }} attributes: entities: | - {{ - states.binary_sensor - | selectattr("attributes.battery","defined") - | selectattr("attributes.device_class","defined") - | selectattr("attributes.device_class","eq","window") + {% set result = states.binary_sensor + | selectattr( + "entity_id","in",state_attr("binary_sensor.downstairs_windows","entity_id") + ) + | rejectattr("state", "in", ["unavailable","unknown"]) | selectattr("state","eq","on") - | rejectattr("state","eq","unavailable") - | rejectattr("state","eq","unknown") | map(attribute="entity_id") - | reject("is_hidden_entity") | list - | sort - }} + + + states.binary_sensor + | selectattr( + "entity_id","in",state_attr("binary_sensor.upstairs_windows","entity_id") + ) + | rejectattr("state", "in", ["unavailable","unknown"]) + | selectattr("state","eq","on") + | map(attribute="entity_id") + | list + %} + {{ result | sort }} diff --git a/integration_entities/templates/sensors/contact_sensors/windows_open_downstairs.yaml b/integration_entities/templates/sensors/contact_sensors/windows_open_downstairs.yaml index 1aee6d4c..f8e1f054 100644 --- a/integration_entities/templates/sensors/contact_sensors/windows_open_downstairs.yaml +++ b/integration_entities/templates/sensors/contact_sensors/windows_open_downstairs.yaml @@ -7,22 +7,5 @@ state: | {{ this.attributes.entities | default([]) | count | default(0) }} attributes: entities: | - {%- macro get_sensors_from_group_with_value(sensor_group=None, value="") %} - {%- if states(sensor_group) in ["unavailable","unknown"] %} - {%- set result = [] %} - {%- else %} - {% set result = states.binary_sensor - | selectattr( - "entity_id","in",state_attr(sensor_group,"entity_id") - ) - | selectattr("state","eq",value) - | rejectattr("state","eq","unavailable") - | rejectattr("state","eq","unknown") - | map(attribute="entity_id") - | list - | sort - %} - {%- endif %} - {{ result }} - {%- endmacro %} + {% from "brianhanifin_template_sensor_helpers.jinja" import get_sensors_from_group_with_value %} {{ get_sensors_from_group_with_value("binary_sensor.downstairs_windows","on") }} diff --git a/integration_entities/templates/sensors/contact_sensors/windows_open_upstairs.yaml b/integration_entities/templates/sensors/contact_sensors/windows_open_upstairs.yaml index f9915148..7724b843 100644 --- a/integration_entities/templates/sensors/contact_sensors/windows_open_upstairs.yaml +++ b/integration_entities/templates/sensors/contact_sensors/windows_open_upstairs.yaml @@ -7,22 +7,5 @@ state: | {{ this.attributes.entities | default([]) | count | default(0) }} attributes: entities: | - {%- macro get_sensors_from_group_with_value(sensor_group=None, value="") %} - {%- if states(sensor_group) in ["unavailable","unknown"] %} - {%- set result = [] %} - {%- else %} - {% set result = states.binary_sensor - | selectattr( - "entity_id","in",state_attr(sensor_group,"entity_id") - ) - | selectattr("state","eq",value) - | rejectattr("state","eq","unavailable") - | rejectattr("state","eq","unknown") - | map(attribute="entity_id") - | list - | sort - %} - {%- endif %} - {{ result }} - {%- endmacro %} + {% from "brianhanifin_template_sensor_helpers.jinja" import get_sensors_from_group_with_value %} {{ get_sensors_from_group_with_value("binary_sensor.upstairs_windows","on") }}