Check if any motion was detected in the last 5 minutes for any amount of time?

I would like to use a condition to check if motion was detected in the last 5 minutes. This motion sensor is a simple binary sensor with on and off states.

I can use the for 00:05:00 but i think that means motion had to exist without changing states for the last 5 minutes. As i’m not sure how long the motion will exist for, i just want to check if there was any kind of motion in the last 5 minutes. Something that checks if the state changed from on to off in the last 5 minutes.

Hope that makes sense. I might completely misunderstand how it works.

Thanks all,

First, check if there is a way to set something like an “occupancy timeout” for your device. I don’t know how you integrated the motion sensor but check in the docs for the integration as well as for the device itself. Many motion sensors have this since manufacturer’s understand that once motion is detected you want to consider a room/space occupied for a period of time longer then just “when the motion stops”.

If your motion sensor does not have this capability then proceed to step two - wrapping it in a trigger template sensor to add this capability. Which is done by basically copying and pasting this example from the docs. Except in this case our “event” is a state trigger, like so:

template:
- trigger:
    platform: state
    entity_id: binary_sensor.motion_detected
    state: 'on'
  binary_sensor:
    name: Motion detected with occupancy timeout
    state: 'on'
    auto_off: '00:05:00'

This binary sensor turns on when your motion sensor does and then turns off again automatically 5 minutes after the last time motion was detected. Then basically ignore binary_sensor.motion_detected entirely, don’t reference it anywhere. Instead any time you want to write an automation/script that does something based on motion in the room use binary_sensor.motion_detected_with_occupancy_timeout.

2 Likes

what is the automation that you are using now?

it seems you might think you need a no motion condition but instead you really need a no motion trigger.

2 Likes

Hmm a no motion trigger might indeed do the job. Rather then checking for motion for the last 5 minutes, i check if there was no motion. Have to think about this.

That’s great Mike, much obliged. Yeah my sonoff motion sensor does have the occupancy sensor. But isn’t that sort of the same as saying ‘no motion detected for x’ ?

I think i have a similar configuration for my binary PIR sensors, let me take a good look at what you suggested.

Thanks again.

It is yes but unlike the automation trigger it gives you a way to check that later. You asked this:

If you use a trigger in an automation then as you note that has issues. Yes you can set it up so an automation fires when motion ceased 5 minutes ago. But you have no way to randomly check if there was motion in the last 5 minutes in a template or script.

With the entity you can. If this evaluates to true:

{{ is_state('binary_sensor.motion_detected_with_occupancy_timeout', 'off' }}

Then you know there was no motion in at least the last 5 minutes. You can check that at any time from anywhere.

1 Like

Got it, that makes sense.

Setting up something similar and this is exactly what I needed - a change in my thinking. Thanks

How can we make this through the web interface? If I understand correctly helpers are similar to adding these templates in the config.yaml file.

I tried adding this as a binary sensor or sensor template, through the template helper. Replacing the entity_id with that of my motion sensor, however the state for the helper remains off.