Using Motion Sensor to turn on a light, and contact sensor to turn off a light after door is opened

Hello Everyone,

Been playing around a little with Rule Machine, after switching over from Wink a few months ago, and just using some simple actions and notifications.

Trying to figure out how to do the following:
State: Bathroom Door contact is Open, Motion is Inactive, Light is off.

Someone walks into the bathroom, Motion is changed to Active, and the light is turned on, The door is then closed, and contact is changed to Closed.

The light stays on while they do their business.

Door opens, Contact sensor Changes to Open, and motion changes to inactive, light is turned off after some amount of time.

So far everything I have tried has either turned off the lights after motion goes inactive, and contact sensor is closed, or does not ever turn off the light after contact sensor goes open and motion goes inactive.

So I am starting from scratch...
My guess is that I will need 2 rules to accomplish this, do I also need to use a virtual switch?



First of all welcome.

What you are trying to do can be done with the Motion Lighting app. Have you played around with that? This can also be done via Rule Machine but there is no reason to do this in RM unless your goal is to learn how to do it.

1 Like

I do the same thing.. But just use motion lighting and a inactivity timeout..

1 Like

I looked at Motion Lighting briefly, and it didn't look like it could do exactly what I needed? Any examples?

I don't have an example and can't put one together for you right now. From what you stated this is exactly what motion lighting does unless I am missing some requirement that you have.

For your purpose the contact sensor shouldn't even come into play. There is a setting to say 'If contact is open/closed' don't turn on lights. If you wanted to add that to the mix.

If you are getting false triggers you can setup a Motion Zone with multiple motion sensors to stop false triggers.

Take a look at the Motion Lighting doc and see if this helps.

Thanks, I will read the docs and see what it says, but wouldn't motion need to stay active? In the case of someone using the toilet for an extended period of time or showering, and the motion going inactive wouldn't the light then turn off?



Yes, but.

With motion lighting you would setup a motion time out period. So if your motion sensor goes active and 30 secs later goes inactive you would setup a timeout in motion lighting of 5 mins or so. If motion becomes active again inside of that 5 mins the timer will get reset for another 5 mins. The lights will only turn off after the timeout period has expired. If your on the can for more than 5 mins not moving the lights could go off. In that case you may have to wave at the motion sensor. You could set you timeout to 30 mins if you wanted to.

Edit: I have a separate motion sensor in the shower. I use the Motion Zone app to aggregate multiple motion sensors so they appear as one. You can also use this app to minimize false motion detection as well.

I have multiple sensors in every room where motion lighting is used.. But I don’t aggregate them.. Works great for my purposes.. Lights come on with a 5 minute inactivity timeout.. It’s extremely rare for a light to turn off while still occupied..

1 Like

You absolutely don't need to to this but in my opinion it makes management easier. For example if you also want to leverage the same motion sensors in Rule Machine all you have to do is change the motion sensors that are in the motion zone which is much easier than editing a rule.

I can't recall a single time that the lights went off because of motion inactivity using motion zones.

I would just suggest looking into the Motion Zone app and decide if it is something that would benefit you. I actually use the app more for false motion detection than motion aggregation.

I use a recessed door sensor to do this with the motion lighting app.

My timeout is set to 30 seconds and the lights never turn off on anyone as long as they close the door.

Thanks, I will try replicating what you have, using my sensors and see what it gets me.

If you're going to do this in RM I don't see a need for a private boolean. You would use 'cancel delayed actions' on the light going off.

If you want some rule machine examples here is a thread that discusses this. There are a whole bunch of other discussions regarding this as well.

This should work

Trigger: Motion Active

Cancel Delay Actions
On Light
Wait for condition:  Motion Inactive
IF(Door Open) Off Light - delayed 00:00:30 (cancel)

The light will stay on as long as the door is close. I've added a 30-second off delay to match gassgs example.


I was thinking of something like this

Triggers: Door contact change

If Light is OFF Turn On lights
set private boolean true
If light ON and door is open, then
set private boolean false
If Light is On and Private Boolean is False, Then
turn light off
set Private Boolean false

So basically when you first trigger it by opening the door it will turn the lights on, then when you close it it will set the private boolean to true, when you open the door to leave it will set the private boolean to false and when you close the door again, it will turn the light off.

Edit: just added the if block at then end
Edit 2: another edit. I haven’t tried it yet but I think this will work

I have 6 motions in a room and they shut off when they go inactive. No delay and my lights rarely shut off when they're not supposed to. I only use a 3 second delay in my rules. I like living on the edge a bit. :smile:

Something like this should work...
Screen Shot 2020-03-02 at 9.27.45 PM

That is such a cool rule. I love seeing highly efficient rules like this. I'm sure if I'd done this it would have been 2 rules or 30 lines long. Bravo!

1 Like

This worked for me, but the girlfriend did not like it, as she often will apply her makeup with the door open, so the light is always turning off for her. I would rather not just set the delay higher, and was trying to do it a little smarter... I tried adding in a conditional statement at the start that checked if the light was off, but it would then never turn off the light?

Trigger: Motion Active

IF (Bathroom Light) is off THEN
Cancel Delay Actions
On Light
Wait for condition:  Motion Inactive
IF(Door Open) Off Light - delayed 00:00:30 (cancel)

Any ideas?

trigger motion active

if motion active
turn on light
cancel delayed action
else if
motion inactive
then turn off light delay 00:00:30 (cancel)

One way would be to use a virtual switch to prevent the light from turning off. If you control your light with a physical switch then you can use Button Controller to set the virtual switch. Let me know how your system is setup and we can figure out what to do.