Help with rule for motion and open/close sensor

I have to take stairs to get into my house. I recently setup a motion sensor at the bottom of the stairs so that if motion is detected, it would turn on the lights and then off after 2 minutes. I also added a open/close sensor to my entrance door so that if I am leaving the house, it would turn on the lights for the stairs. I also have that sensor to turn on the living room lights when the sensor state is opened.

I then realized that this will cause my living room lights to turn on when I open the door to leave. What I would like is to have a rule that will only turn on the living room if the senor in an open state but also motion with detected within the last minute. This would cause the living room to turn on when I arrive since the motion sensor would detected me first and then the open/close sensor but not turn them one when I open the door to leave since the motion sensor will not detect me before I close the door. Is there any way I can do this?

its late and “i think” i had a similar situation that i resolved by just adding another sensor. but i think rule machine would do what you want but since i have only tinkered with rule machine i am not 100% sure. I keep meaning to get back to rule machine but its just not high on the priority list. I think the community is working on a new rule machine i would follow this thread

1 Like

I have been tinkering with the idea of adding two more piston modes, And-If and Or-If. This use case convinced me of their usefulness.

I will also add two past tense comparisons: changed and did not change.

Then the rule would be:

IF
  MotionSensor 'motion' changes to 'motion'
  OR
  Door 'switch' changes to 'open'
THEN
  Turn on stairs light
  Wait 2 minures
  Turn off stairs light

AND IF
  MotionSensor 'motion' did not change in the last 2 minutes
THEN
  Turn on living room lights

That is a very interesting way to do it. Will you be creating something that can do this?

The AND IF would need to be different though.

AND IF
MotionSensor ‘motion’ detected motion within the last 2 minutes
AND
Door ‘switch’ changes to 'open’
THEN
Turn on living room lights

Yeah, the And-If condition should be “door is open” and “motion changed in the last X minutes”. And-If will only be evaluated when If is evaluated as true. I entered the reverse logic there.

Since it may be awhile before the piston code is tested and available as it’s still early in the design phase, I did just want to mention that there is a way to solve this problem without using any custom code if you’re willing to use transitional modes. Several of us Have been doing this for about a year, using various different devices.

This approach doesn’t use a timer, which can be tricky anyway because of the mesh network. Instead, it works by knowing where you started, which is based on the use of transitional modes.

So say you’re coming in from outside. The initial mode is away. So you use a routine that only runs in away mode. That routine says when the mode is away, if the motion sensor reports activity, change the mode to arriving and turn on the stair lights.

The second routine only runs in arriving mode. It says when the mode is arriving, if the door sensor opens, turn on the living room lights. It also changes the mode to whatever your usual i’m home mode would be.

That takes care of the direction where you are coming in from outside.

Now for the direction where you are starting in the living room.

A third routine says when the mode is anything except arriving (that is, it doesn’t run in arriving mode), when you open the front door, turn on the stair lights. That way the living room lights are not going to come on.

The structure may be more or less complicated depending on exactly what you are doing. You might need two transitional modes (one for leaving and one for arriving) and multiple routines, based on the number of other modes you have already. You may even include virtual switches in routines to trigger smart lighting or other smart apps so that you can do things that aren’t available in basic routines.

But the point is that it gives you a way to recognize the sequence in which devices are triggered. No custom code required.

The following thread show some of the different ways that I’ve used it, and some of the different devices I’ve used it with. The basic idea came from a different community member, and That’s also mentioned in the thread. In my case, I wanted to limit presence detection to A smaller area than even the arrival sensor covered, so using two devices allowed me to do that.

Anyway, this is a solution with a lot of different pieces, even if each piece is pretty simple. So you might prefer to wait until there are more complex custom code methods available. But I did just want to mention that you can do it now just with the existing official features. :sunglasses:

1 Like

That seems like another way to do it. The only bad thing is my significant other is not really into this as much as I am so it would not work well lol