Using a motion sensor is great for turning ON the lights upon entry to a bathroom, but they’re not so good for turning the lights off at the appropriate time.
Perhaps the answer is to augment the setup with a door open/close sensor. In my house people tend to close the bathroom door when they want privacy (toilet, bath, shower). So, we just need to prohibit the motion sensor from turning off the light if the door is closed, and then have the lights go off a minute or two after motion goes inactive when the door is open…
All that said, I don’t think there is a standard app that can handle this. This might do the trick:
'''groovy
/**
* Occupancy Manager
*
* Copyright 2015 Barry A. Burke
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "Occupancy Manager",
namespace: "SANdood",
author: "Barry A. Burke",
description: "Occupancy monitor & switch controller",
category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
section("Title") {
// TODO: put inputs here
}
section("Occupancy sensors..."){
input "motionSensor", "capability.motionSensor", title: "Monitor this motion sensor", required: true
input "doorSensor", "capability.contactSensor", title: "Monitor this door contact", required: true
input "theSwitch", "capability.switch", title: "Operate this switch", required: true
input "minutes", "number", title: "Turn off after (minutes)?"
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
state.occupied = false
if (motionSensor.currentMotion == "active") {
lightsOn()
}
subscribe(motionSensor, "motion", motionHandler)
subscribe(doorSensor, "contact", doorHandler)
}
def motionHandler(evt) {
if (state.occupied && (evt.value == "inactive")) {
if (doorSensor.currentContact == "open") { // motion sensor can't turn off lights if door is closad
Integer lightsOffDelay = settings.minutes *60
runIn(lightsOffDelay, lightsOff, [overwrite: false]) // Don't overwrite doorOpened lightsOut() schedule
}
}
else if (!state.occupied && (evt.value == "active")) { // somebody just came into the room
unschedule( lightsOff )
lightsOn()
}
}
def doorHandler(evt) {
if (evt.value == "closed") {
unschedule( lightsOff ) // just in case
}
}
def lightsOn() {
if (doorSensor.currentContact == "open") { // Only turn lights on if door is open (they might be sleeping)
(theSwitch.currentSwitch != "on") {
theSwitch.on()
}
state.occupied = true
}
}
def lightsOff() { // We can only turn off the lights when the door is Open
if (doorSensor.currentContact == "open") {
if (theSwitch.currentSwitch != "off") {
theSwitch.off()
}
state.occupied = false
}
}
'''