Hi All,
I’m maintaining this security alarm component and recently we discussed lack of some features and sensors grouping was mentioned.
Here is the quote:
This helps prevents false alarms from triggering. All that is you would group sensors and set a time limit between them to see 8f the alarm should go off or not. For example you have two motion sensor in one room and one detects motion. With cross grouping you can set let’s say 30 seconds and if that second motion sensor detects motion the alarm would trigger otherwise it would not.
I think it’s relatively easy to implement such a concept in HA using template sensors and then use that sensor with any security alarm component.
Here is my version of PIRs grouping (actually, it works for any binary_sensor-based sensors like door contacts, smoke sensors etc):
binary_sensor.yaml
- platform: template
sensors:
pir_1_on_prolonged:
value_template: "{{ is_state('binary_sensor.pir_1', 'on') }}"
delay_off:
seconds: 30
pir_2_on_prolonged:
value_template: "{{ is_state('binary_sensor.pir_2', 'on') }}"
delay_off:
seconds: 30
pir_cross_sensor:
entity_id:
- binary_sensor.pir_1
- binary_sensor.pir_2
- group.cross_sensors_group
value_template: >-
{% set ns = namespace(found=0) %}
{% for entity_id in states.group.cross_sensors_group.attributes.entity_id if is_state(entity_id, 'on') and (ns.found < 2) %}
{% set ns.found = ns.found + 1 %}
{% endfor %}
{{ ns.found == 2 }}
group.yaml
cross_sensors_group:
name: "cross-sensor grouping"
entities:
- binary_sensor.pir_1_on_prolonged
- binary_sensor.pir_2_on_prolonged
The binary_sensor.pir_cross_sensor
will be on
from the moment there are at least 2 different on_prolonged
sensors in on
state until there is none of them.
The delay between individual sensors’ signals to make binary_sensor.pir_cross_sensor
active is set by that delay_off
and can be replaced by template so you can easily have user-configurable delays:
delay_off: "{{ states('input_number.cross_sensor_delay') }}
If you need to add sensor binary_sensor.doorcontact_1
to the group, complete the following steps:
- Create a corresponding
on_prolonged
template binary sensor by copying the declaration ofbinary_sensor.pir_1_on_prolonged
and replacingbinary_sensor.pir_1
withbinary_sensor.doorcontact_1
- Add its
entity_id
to thecross_sensors_group
group - Add
entity_id
of the original (noton_prolonged
!) sensor (binary_sensor.doorcontact_1
in this example) to theentity_id
list ofbinary_sensor.pir_cross_sensor
… and that’s it!
I’ve decided to combine all sensors in a group as I think it makes it easier to extend/maintain the config as without that we’d need an array of on_prolonged
entities in the binary_sensor.pir_cross_sensor
value_template
to iterate over.
Would love to hear your feedback and any ideas how to improve it or alternative solutions.
UPDATE: it looks like instead of
{% for entity_id in states.group.cross_sensors_group.attributes.entity_id
we can use
{% for entity_id in expand('group.cross_sensors_group')