Trigger if motion active for more than X minutes

I am sure this has been asked I've the forum in past, but wanted to ask my question so that people might suggest answers using latest rule machine features of needed.

I need to automate bathroom fan with below logic. Please suggest how to write the rule:

  • If bathroom motion is triggered
  • After 5 minutes, if motion is still there
  • Then turn on fan
  • Turn off fan after 10 minutes of the motion becoming inactive

Others are probably best placed to comment on the specifics of the rule. And I wouldn't want you to limit it to RM from the start, we should be open to other solutions, but I can understand if you want to explore how best to use RM more generally, using this as an example.

The point I wanted to clarify is that if motion is re-activated during the 10 minute delay for turning off the fan, do you still want to wait another 5 minutes?

Good question from sburke781. I've assumed the answer is no since that make sense to me and the rule is simple enough to modify if my assumption is wrong. Here's an option with another assumption that motion is still there means duration.

Trigger:
Bathroom Motion *changed*

Actions:
If (Bathroom Motion is active) THEN
    IF (Bathroom Fan is off) THEN
        Wait for Expression: Bathroom Motion active --> duration 0:05:00
    END-IF
    On: Bathroom Fan
ELSE
    Wait for Expression: Bathroom Motion inactive --> duration 0:10:00
    Off: Bathroom Fan
END-IF

Just a note that I wanted the Bathroom Fan is off a simple condition with a Wait for Expression but keep getting errors so I'm using IF-THEN instead.

I think the first wait for 5 mins doesn't need the motion active condition... Not sure what to put in it's place...

Any I think the wait for inactive in the ELSE block might not work... If the rule has already been triggered on inactivity.

One could put Wait for 5 minutes elapsed time then check for active motion to turn on fan.

It'll work and needs something to turn off the light. I'd be interested to hear more on why you don't think it'll work.

That could work. For this an a couple of other situations you may need to include some cancel actions, but again, we can look past that for now and others can probably comment on that better than me....

If you follow the logic through the trigger and actions to get to the ELSE block, the rule will have to have been triggered by motion moving from active to inactive, based on the IF condition. So inside the ELSE block waiting for motion to become inactive will be waiting for the motion sensor to have gone through an active then inactive cycle again.

All that said, I could be wrong, happy to be corrected....

Also feel bad for not offering an alternate solution...

Ok, I see your concern but we need to look at the actual wait action. There are two types of Wait action, Wait for Event and Wait for Expression. In simple terms, Wait for Event is looking for a discreet event while Wait for Expression is looking at that state. In this case I'm suggesting to use Wait for Expression with a duration meaning the state of the motion sensor is inactive for a specified time. When the motion sensor has been inactive for 10 minutes, the Wait will be satisfied and proceed to the next action.

Then you could probably just do a wait for time period like we talked about in the IF block, given we know the sensor is already in the inactive state, due to the IF condition checking that before we enter the ELSE block.

You could but what about the case where you walk out and the motion sensor goes inactive for 1 minute, then you walk back in for 8 minutes, then out for 1 minute. You'll meet the wait for time period event though total inactive time in the 10 minute period was only 2 minutes. That may meet the OP needs but my preference would be for 10 minutes of continuous inactivity.

Edit: The rule retriggers so a Wait for elapsed time would work fine.

I'll need to process the situations you listed, but I expect a cancel actions entry will be needed, potentially for what you are referring to. My understanding is, and maybe someone can correct me on this, most conditions are evaluated when the rule is triggered, not throughout execution. Apart from wait for event type scenarios of course...

I think that the changes to the motion sensor state may also cancel the wait for events for existing executions of the rule, I think....

With the definition of your rule I would expect the fan to be turned off straight away. The rule will trigger because of a change in the sensor state. The IF condition will be false, as motion was inactive at the time the rule triggered. Depending on how the expression is treated, if it is evaluated at the time and my concern over it waiting for the condition to be true is right, then I would still expect it to evaluate as true straight away then the fan would be turned off.

In the end my speculation probably doesn't help, testing the rule and seeing how it behaves is probably best, as interesting as I find these conversations. That or others can chime in with examples or knowledge of how these constructs in RM work.

particularly when it is likely to be wrong.... Sorry @pseudonym, I can see what you mean now and how the expression needs to be true for the 10 mins, my bad. Should leave those who know this stuff better to comment.... Like you :slight_smile:

@sburke781 Ugh, my 1-8-1 minutes response was wrong. Changing states retriggers the rule. It will work as written be can also be written with just Wait for elapsed times.

1 Like

@pseudonym Just for my understanding, why making the rule so complicated? Wouldn't it be way easier with:

Trigger: Motion changed

IF Motion active THEN
Wait for expression motion active ---> Duration 5 minutes
On: Fan
ELSE IF (Motion inactive) THEN
Wait for expression motion inactive ---> Duration 10 minutes
Off: Fan
END IF

1 Like

@Vincent_The_Staat Yeah, I was trying to be too cute with the rule and eliminate the wait if the motion was active and the light was already on. Why wait 5 minutes to turn on the light (or at least get to the On action line) when the light is already on? When the simple condition didn't work for me I should have dropped that idea. I can think of at least three other ways to write this rule with fewer lines to achieve the same result. I probably should have gone with one of those. Lesson learned!

1 Like

I am testing this now. I seems, that sometimes it does not work as intended. There is motion in the bath for long time, but the fan does not start a few times. I am trying to find what might be causing this. Check today's instance. I was in the bath for more than 8 mins I think, but fan did not turn on:

@saurabh9 Your logs show the shared both motion active at 7:46:58 then inactive at 7:48:23. You only got 0:01:25 of active motion so you didn't meet the 0:08:00 duration. The inactive retriggered the rule and the active Wait was canceled. Your logs support the fan not turning on.

I agree with @pseudonym. I would check two things:

  1. Is steam or anything else disturbing your motion sensor, so that it can't be triggered again.

  2. If this is not the case: How long is the cool of time of your sensor. If, for any reason it is longer then 8 minutes, you need to change your rule accordingly.

OK, so the motion sensor I have are Sonoff. I tried by reducing the wait time to 5 minutes, before the fan should turn on. In the next example from today, I think the motion started on 8:18 and ended at 8:24...which seems to be more than 5 mins, but the fan still didn't turn on.

Download the Hubitat app