Don't turn off bathroom lights after no motion if someone's inside


1) Give a description of the problem
Hey all, this is my first piston and I need a little help! I’m using a SmartThings motion and multipurpose sensor to turn on bathroom lights and have these scenarios:

2) What is the expected behaviour?

  1. Person enters bathroom, motion is detected, door is not closed. Light(s) turn on (100% daytime or 1% nighttime), then turn off after 1 minute of inactivity. This is as far as I’ve got.
  2. Person enters bathroom, motion is detected, door gets closed. Light(s) turn on but should not turn off again until the person leaves the bathroom. This part I can’t figure out.

I thought of checking whether the door was opened after motion detected, but if my girlfriend was to walk in while I’m showering, then close the door again on her way out, that’d trigger the turn off sequence.

I think the best way is checking if new motion has been detected after door has been closed (i.e. door was closed from inside, not outside) but unsure how to do it, especially as the motion sensor is still showing as “motion” for 15 seconds or so after I’ve left the room.

3) What is happening/not happening?
Unsure on how to write part 2 above. Lines 29-49 and 59-67 are my solutions for part 1.

4) Post a Green Snapshot of the pistonimage

5) Attach logs after turning logging level to Full

Thank you in advance!


I can tell you can code;) and I know you are asking for coding solution but here is my two cents…
When I fisrt started webcore i had all those amazing ideas of doing things… Short 6 months later i realized that the best solution is the simplest one…
Get 3 more motion sensors, all covering the whole bathroom and that’s it…


I’ve often pondered this one myself. How about this:

Variable: int OffDelay /* Time to delay before auto-off */
If Bathroom Door changes to Shut:
    Set OffDelay to 20
If Bathroom Door changes to Open:
    Set OffDelay to 3
If Bathroom Motion changes to Active:
    Turn on light
If Bathroom Motion stays inactive for $OffDelay minutes:
    Turn off light

How do you feel about something like this? It’s not what you asked for, but might work anyway.

Otherwise, I’ve made an Occupied variable. You could set the variable to True if the door is shut, and motion is detected. If Occupied, change the OffDelay to a greater number.

There is a function, age(), that will give you the timestamp of when a device status was set. Age([Motion Sensor: motion]) should return a timestamp. So, you should need to keep track of the timestamps yourself. Here’s what it looks like in the evaluation console:

Here’s my piston that I used to use in the bathroom:

This is a difficult one. You also have the issue of the motion sensor picking up the motion before the person enters the room. Perhaps something like this might work:

if bathroom door changes to closed:
    wait 60 seconds
    if Bathroom Motion Sensor is Active:
        Set Occupied to True