Skip to content

Commit

Permalink
improved contact sensor count logic
Browse files Browse the repository at this point in the history
  • Loading branch information
brianhanifin committed Oct 4, 2023
1 parent 9137b7c commit 2f01fc9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 69 deletions.
3 changes: 1 addition & 2 deletions custom_templates/brianhanifin_template_sensor_helpers.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Original file line number Diff line number Diff line change
Expand Up @@ -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") }}
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Original file line number Diff line number Diff line change
Expand Up @@ -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") }}
Original file line number Diff line number Diff line change
Expand Up @@ -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") }}

0 comments on commit 2f01fc9

Please sign in to comment.