Smart Bathroom Light

Hello I have no clue how to write smart apps and been trying to learn, how ever I have an ideal for someone to create an SmartApp to control lights based on multiple sensors.

Bathroom Lights
okay so lets say I have a motion sensor in my hallway and bath room and the bathroom has a open/close sensor, can a app be written that allows the following to happen -

You walk to the bathroom from the hallway, M1 detects motion, enter the bathroom and M2 detects motion and lights turn on with door open or shut and lights stay on until M2 quits detecting motion and M1 quits detecting motion

okay now if you want to take a shower and all motion stops lights go off, that’s the biggest problem there is in all the smart light apps so is it feasible for M1 + M2 + C1 closed Lights stay on until C1 open + no motion M2 + M1

Key :
M1 = Hallway Motion Sensor
M2 = Bathroom Motion Sensor
C1 = Bathroom Contact Sensor

I haven’t had a chance to test it yet, but here is something I threw together:

/**
 *  Two Motion and a Contact
 *  2/10/2015
 *
 *  Copyright 2015 Eric Roberts
 *
 *  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: "Two Motion and a Contact",
    namespace: "baldeagle072",
    author: "Eric Roberts",
    description: "If there is motion on the first sensor and then the second sensor, turn on the lights. Only turn off if the motion stops and the door is open.",
    category: "My Apps",
    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("Sensors") {
        input "motion1", "capability.motionSensor", title: "Motion 1", required: true
        input "motion2", "capability.motionSensor", title: "Motion 1", required: true
        input "contact", "capability.contactSensor", title: "Contact", required: true
    }

    section("Lights") {
        input "lights", "capability.switch", title: "Lights to turn on"
    }
}

def installed() {
    log.debug "Installed with settings: ${settings}"

    initialize()
}

def updated() {
    log.debug "Updated with settings: ${settings}"

    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(motion1, "motion", "motion1Handler")
    subscribe(motion2, "motion", "motion2Handler")
}

def motion1Handler(evt) {
    if (evt.value == "active") {
        state.motion1on = true
    } else {
        state.motion1on = false
        checkOff()
    }
}

def motion2Handler(evt) {
    if (evt.value == active && state.motion1on) {
        lights.on()
    } else {
        checkOff()
    }
}

def checkOff() {
    def motion1Inactive = motion1.currentValue("motion") == "inactive"
    def motion2Inactive = motion2.currentValue("motion") == "inactive"
    def contactOpen = contact.currentValue("contact") == "open"
    if (motion1Inactive && motion2Inactive && contactOpen) {
        lights.off()
    }
}

Funny! I ended up putting motion sensors in the showers to solve this problem.

@bravenel,

Don’t tell us that you’re using a motion sensing camera… TMI

1 Like

When creating a process (or a program) it is important to understand all possible failure modes or conditions and either; except them as is, keep them from happening, or react/correct for them. So I’m going to throw out a few possible failure modes or considerations that you might want to think about when writing the occupancy program.

  1. What if two or more people go in during the initial motion sensing period?
  2. What if one person goes in and quickly goes out during the initial sensing period?
  3. What if the door was already open and stayed open while a person was inside the space?
  4. What if a person opened the door, went in, closed the door and then reopened the door to yell for TP and quickly closed it afterward? LOL
  5. Does the door sensor really play an important role is calculating if one or more people are in the space being monitored?
  6. Instead of a door open/closed sensor would a humidity or noise sensor add more value?
  7. Does the hallway motion really play an important role in determining occupancy of the bathroom?
  8. Is it possible to have a motion sensor that has total visibility of the entire bathroom space?
    9 What is the cost in $$'s of the leaving the light on during a max shower time (even if no shower) versus the customer dissatisfaction :angry: of having the lights turn off while taking a long shower? Is there an unacceptable trade-off point?
  9. What would be the simplest hardware/software setup to accomplish the goal of automating the bathroom lights.
  10. What is the cheapest?
  11. Is there a manual override or back-up plan?

HTH

2 Likes

Now that the IDE is fixed, I had a chance to test and update the code:

/**
 *  Two Motion and a Contact
 *  2/13/2015
 *
 *  Copyright 2015 Eric Roberts
 *
 *  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: "Two Motion and a Contact",
    namespace: "baldeagle072",
    author: "Eric Roberts",
    description: "If there is motion on the first sensor and then the second sensor, turn on the lights. Only turn off if the motion stops and the door is open.",
    category: "My Apps",
    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("Sensors") {
        input "motion1", "capability.motionSensor", title: "Motion 1", required: true
        input "motion2", "capability.motionSensor", title: "Motion 1", required: true
        input "contact", "capability.contactSensor", title: "Contact", required: true
    }

    section("Lights") {
        input "lights", "capability.switch", title: "Lights to turn on"
    }
}

def installed() {
    log.debug "Installed with settings: ${settings}"

    initialize()
}

def updated() {
    log.debug "Updated with settings: ${settings}"

    unsubscribe()
    initialize()
}

def initialize() {
    subscribe(motion1, "motion", "motion1Handler")
    subscribe(motion2, "motion", "motion2Handler")
}

def motion1Handler(evt) {
	log.debug("motion1 = $evt.value")
    if (evt.value == "active") {
        state.motion1on = true
    } else {
        state.motion1on = false
        checkOff()
    }
}

def motion2Handler(evt) {
	log.debug("motion2 = $evt.value : $state.motion1on")
    if (evt.value == "active" && state.motion1on) {
        lights.on()
    } else {
        checkOff()
    }
}

def checkOff() {
    def motion1Inactive = motion1.currentValue("motion") == "inactive"
    def motion2Inactive = motion2.currentValue("motion") == "inactive"
    def contactOpen = contact.currentValue("contact") == "open"
    log.debug("checkOff : $motion1Inactive : $motion2Inactive : $contactOpen :")
    if (motion1Inactive && motion2Inactive && contactOpen) {
        lights.off()
    }
}

So if I faint in the shower and my service dog goes to get help, the lights are going to go off?

2 Likes

Obviously, this app doesn’t work for your very specific situation. For most people, if they are taking a shower, the door would be closed and wouldn’t turn off the lights.

Just depends.

Young child in a hurry might not close door all the way. People, including visitors, in wheelchairs often have to leave doors open behind them. Person taking selfie in mirror (you know who you are :wink:) might leave door open for more light. Parent with young children might leave door open a crack to listen for kids waking up from nap. Adult with heart problems may have a habit of leaving door open a crack to be able to call for help even when there’s no one else home. People with arthritis may leave door open a crack because the door is hard to open. These people could be visitors, not just residents.

If the room has a tub, bathers may not register as moving, and could have open door condition for any of the previous reasons. (Worst xAF of all time: new girlfriend stays over for the first time, takes bath with door left ajar and the lights go off. :disappointed_relieved:)

On a separate note, many bathrooms have fans, which will cause the other problem, fooling the motion detector so lights stay on.

So if that particular SmartApp works for a particular use case, excellent. But I would comment in a caution that if the door is not fully closed or is opened again, lights may turn off unexpectedly.

I like motion detectors, but I hate bathroom and bedroom lights that turn off unexpectedly. Just saying…

Again, that’s all fine. The original post asked for very specific functionality and I was helping him out. Everybody’s situation requires different actions to happen.

Understood and good work!

The OP said they were new to smartapps, so just thought it good to mention that “quits detecting motion” can frequently lead to “unexpected events.” :wink:

And once good code exists, it tends to get repurposed by people with different uses cases than the original, which is why I like to see a brief comment in the code for anything that requires specific human behaviours to produce the expected result. Lots of people close the bathroom door tightly every time. Lots of people, including guests, may not.

1 Like