Trigger automation when PIR detects motion after PIR clear for at least 15 minutes

I a PIR in my office I want to trigger a switch group when motion is detected, but only if it has not detected motion in the last 15 or more minutes. I feel like this should be obvious but I have not been able to figure it out. I have tried using a template in the conditions
{{ now() - states.binary_sensor.office_esp_pir.last_changed >= timedelta(seconds=900) }}
but it seems like the trigger of the PIR detecting motion is updating the last_changed state to now and then the template fails (its like I need a last_last_change state).

Is there a way in the trigger to have something to the effect of: from: clear, for: >=15 minutes, to: detected?

The only other way I can think to do it is to have one automation that watches for clear for 15 minutes activate the turn on motion detection automation and then have the motion detection automation turn itself off at the end.

Thanks for any help, I have been away from HA for a couple years and so much has changed (and gotten wayyy better)

I think this would be the simplest way of doing it:

trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor
    from: 'on'
    to: 'off'
    for:
      hours: 0
      minutes: 15
      seconds: 0

Maybe I misunderstand something, but, wouldn’t that trigger when motion is detected and then clear for 15 minutes. I want it to trigger when clear for 15 minutes and then motion is detected…I need the for:15 to be in between the state changes. I can’t say from clear to detected for 15 minutes because the detection signals only last 1-2 seconds. Am I misunderstanding how the from: to: for: is processed?

Yes, you are correct, I misunderstood, so lets rework it to what you are looking for, lets try this:

trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor
    from: 'off'
    to: 'on'
condition:
  - condition: template
    value_template: "{{((now()-trigger.from_state.last_changed).seconds) > 900 }}"
action:
  ...

I think this would work for you, at least it does in my head : )

2 Likes

I have something that I think is the same effect as your template but it doesn’t seem to be working.
{{ now() - states.binary_sensor.office_esp_pir.last_changed >= timedelta(seconds=900) }}

My thought was that when the pir fires to trigger the automation the time that it fires gets written to the state.last_changed. Is that now how it works? Also, I am very new to templates is yours doing something different? (Also, thanks so much for the help!)

Yes, @TinyDoT 's answer above is using the value of last_changed from the sensor’s previous state object which is stored in the trigger variable. When you use states.binary_sensor.office_esp_pir.last_changed, you are getting the current last_changed which will be when it turned “off”… essentially a few microseconds ago when the automation was triggered.

Just be aware that last_changed is not entirely reliable since it will change after restarts or reload.

Thank you very much, is there a place I can find a list of the built in variables so I can see the tools available (my google-fu was too weak)?

There are basically 2 built-in variables for automation trigger and this:

You can check the values of the variable for previous runs of an automation by looking at its debug trace.

@Didgeridrew, hey first off sweet name, but also thanks for jumping in and explaining that. @dgrosenberger, were you able to try this, and is it what you are looking for?

Why is this obvious? Most common problem with PiR’s is that they do not register you if you sit too still. So people wave their arms to get light again. What you suggest is that they need to wait, no - sit perfectly still - for 15 mins in the dark before being seen again. Also, if some one enters the room 12 minutes after some one else left, it does not respond. Why would you want this at all?

I wish, Im currently on my fourth 13 hour day in a row at work…Ill have a few hours off Monday to give it a shot. Thanks again for all the help.

Thanks for taking such an interest, I feel you passion!

First, I meant that I thought the process of how to do it should be obvious, I assumed I was missing something.

Second the use case: The PIR is controlling a template switch that controls my office. It’s looking at my mouse pad and chair from behind a monitor. I sit down, it detects motion and turns on the office lights, desk backlight, 3d printer and sends a WoL packet to my computer. As to the no motion timeout: sometime I turn various things off while I am in there (buttons for everything on my stream deck too), and I don’t want it to turn them back on for me during a session. I figure if I haven’t moved the mouse in 15 minutes its probably safe to assume I have left and relighting everything up again is fair game.

I’m aware there are a couple other ways to accomplish the goal, but this was one I didn’t know how to do and I like learning.

I hope you feel satisfied and fulfilled by my explanation. =)

edit: Included a picture cause I love my office. (Its not the cleanest pic but all I had on my phone).

1 Like

If you are trying to make the rest of us Jelly about your office, you have done a good job. Gotta love that screen : ) Also side note, just to throw this out there. Have you thought about a pressure sensor on you chair, to accomplish this. This is one thing I love about this hobby, is that there are so many ways to do one thing, and everyone will approach things differently.

I got a chance to try out your suggested template and it works like a charm, thanks very much! I like the novelty of the pressure switch but I roll my chair around a fair bit and powering the esp or whatever I used would become a problem, I don’t want to have to change batteries.

Thanks for the comments on my desk, its one of my favorite spots and I built the whole thing myself, the computer, then desk it is on and even the room it is in (I finished the basement). That little grid of frames in the upper right is every computer processor I have ever owned all the way back to an 8088 that ran at 7.16mhz. (The 49 inch monitor is pretty sweet as well =)

Thanks again for all the help!

I’m glad it worked out. And I did not realize the processors on the wall , that is a cool idea. I am going to leave you with something to entice you. The pressure sensor that I was thinking of is kind of a DIY sensor, and seeing what is on your wall, I don’t think you would have a problem creating one. And no need to worry about the battery cause the battery in this sensor lasts for several years. I have not change a battery yet in the ones that I have created, so take a look:

Thanks, I appreciate it, Ill check them out!

Here is today’s project: My son overslept this morning and I had to wake him up for work. From now on, if it is Monday through Friday, and his phone is home and the first floor motion sensor doesn’t detect motion in the 15 minutes before 6:05am the very very loud siren goes off in his room. It’s a little sloppy but I built it pretty quick with parts I had laying around and it fits inside the box for the orig siren.

That is a pretty sweet project, I like it a lot.