A binary_sensor that stays on for 1min after one of multiple motion sensors were triggered

Hi, I am trying to have a sensor “motion_north” if any one of the North facing motion sensors were triggered, then to stay on for 1min if any of them were.

Steps I took:

a) Created the binary_sensor as follows: (this works)

- platform: template
  sensors: 
    motion_north:
      unique_id: motion_north 
      friendly_name: Motion North
      device_class: motion
      value_template: >-
         {{ states.input_boolean.motion_north.state }}

b) Created the automation if one of the North facing motion sensors are triggered: (example only uses one of the North facing motion sensors).

alias: Motion North - on 2min
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.out_north_1
    to: 'on'
condition: []
action:
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.motion_north
      state: 'on'
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: python_script.set_state
    data_template:
      entity_id: binary_sensor.motion_north
      state: 'off'
mode: single

This does not work - any other approaches?

Hey there,

first and foremost, try avoiding states.xx.yy.zz whenever possible.
That’s not what’s causing your issue but avoids some unnecessary errors during startup.

Since you have multiple sensors facing north, I’d create a simple group with all binary_sensors on the north.

Then, create an automation similar to yours.
Hint: Instead of using a python_script to overwrite a state, just use the input_boolean directly, there’s no need to create a binary_sensor.

Alternatively, there’s another solution:
Create a template binary_sensor and have a value template like this

{% if as_timestamp(now()) - as_timestamp(states.group.north_sensors.last_changed) >= 60 and is_state('group.north_sensors', 'off')  }}
  off
{% else %}
  on
{% endif %}

Here you can’t avoid using the state object.
This template will update once the state of the group changes.
The logic is: If the state of the north group hasn’t changed for 60 or more seconds, the sensor will be off, otherwise (because the last time movement got detected is <60s ago), it’s on.

Thank you so much, will try it over the weekend.

@fedot - thank you for pitching in a solution so far. I am going with your Group option. The 4 x groups works well, and the input_boolean as well.

The problem is that when the event of one of the alarm eyes goes off, it then switch off the automation, thus the automation does not work. Any way I can ‘suppress’ the off that comes in before the 2 min wait is over in the automation?

alias: Alarm Motion North - on 2min
description: ''
trigger:
  - platform: state
    to: 'on'
    entity_id: group.alarm_outside_north
    from: 'off'
condition: []
action:
  - service: input_boolean.set_state
    data_template:
      entity_id: group.alarm_outside_north
      state: 'on'
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: input_boolean.set_state
    data_template:
      entity_id: group.alarm_outside_north
      state: 'off'
mode: single

You can’t switch on the group, why do you want that? It’s already on, that’s what triggered the automation…
Same for turning it off, the group is just a reading of all sensors, why do you want to modify that?
Please try the other solution I’ve posted above. The 60 is the delay in seconds, so for 2min, use 120 instead.

What is the purpose of this 1-minute persistence?

Why not simply stay on for as long as there is any motion and immediately turn off when there’s none (in which case, a group of motion sensors is all that is needed).

Okay, giving the other method a try.

The purpose is - if an alarm eye triggers the alarm, say from a moving person, which then stands still - and the alarm eye goes off, one will not be able to see from which direction the movement was triggered.

How does a sensor that stays on for a minute tell you the person’s direction?

I have several IR eyes & Hikvision cameras in each of the wind-directions, by grouping them in the wind directions, I can get a quick glance of where the danger is. (Living in South Africa…).

Ahhh, I am learning, makes sense ! I have another idea, gonna create a “dummy” binary_sensor - which I add to each of the groups, one each, that I will then switch on / off, which will keep the appropriate group on and then off.

I think what you want is commonly known as “occupancy”.

If a person moves then the area they’re in is considered to be occupied. If they stop moving momentarily, there may be no motion but the area is still considered to be occupied.

If there’s no motion detected for at least one uninterrupted minute (or whatever amount you want) then, and only then, the area is considered to be unoccupied.

Exactly right… Did not realise that this is the same - I have seen some posts on doing that - let me get googling again, thank you !

I think it might be as simple as using a Template Binary Sensor with the group you created:

binary_sensor:
  - platform: template
    sensors:
      north_occupancy:
        friendly_name: "North Occupancy"
        value_template: "{{ is_state('group.alarm_outside_north', 'on') }}"
        delay_off: 60

The key here is delay_off: 60 which means the group must be off for at least 60 seconds before this binary_sensor reports off.

3 Likes

Binary sensors are sensors. You don’t just overwrite them or create dummies, that’s what Helpers are for.

  • Create a group with all motion binary_sensors of one side
  • Create a template binary sensor like the one I’ve posted (don’t add this to the group of motion sensors!)

The code of @123 will do basically the same, all be it more clean.

As the FAQ/rules say, please tell us what you want to do rather than what isn’t working

Thanks both, will try last 2 posts. Let me explain what I think I want. The ‘occupancy’ expains it well. By ‘dummy’ - I meant template binary_sensor. still struggling with the concepts.

  • Several alarm IR eyes / doors to outside open states / IP cameras that can detect motion and report on it are on my HASS system already.
  • but, as a motion is picked up, and say it triggered the alarm, when one looks to determine from which direction…, and say
  • the intruder stopped moving, I want 4 wind-direction groupings that will stay on for say 120s after one or more of the events in that wind direction occurred,
  • to then show this on a mobile, so one can be ready for action from that direction,
  • next step is to switch the garden lights on - if at night, in that direction.

I think the above 2 posts will solve it. Let me report back here.

Sorry guys, newbee to HASS, I come from Node-RED for the last 4 yrs, busy ramping up on how all this works.

Thank you @fedot and @123 - I want to mark several of your posts as “the Solution” - they all contributed to my understanding. It’s working now !!!