Enhanced Toggle Light When Contact Open

I wanted to automate the lighting in my bathrooms so that the lights would turn on when the door was opened, and turn off when the door was opened a second time, but couldn’t find a way to do this with Smart Lighting. I tried a CoRE piston, but it was a little too slow. So I decided to write a simple smart app!

Note that this also handles the use cases where:

  • the user opens the door, pokes their head in, and turns off the light manually and closes the door
  • the user opens the door, enters the room, closes the door, turns off the light manually while inside, and opens the door again
  • the user opens the door, enters the room, closes the door, then opens the door again while turning the light off manually.

I can’t think of any other use cases, but let me know if there are any!

 *  Copyright 2016 Zach Spencer
 *  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.
 *  Enhanced Toggle It When It Opens
 *  Author: Zach Spencer
	name: "Enhanced Toggle It When It Opens",
	namespace: "zcspencer",
	author: "Zach Spencer",
	description: "Toggle something when an open/close sensor opens.",
	category: "Convenience",
	iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
	iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"

preferences {
	section("When the door opens..."){
		input "contact1", "capability.contactSensor", title: "Where?"
	section("Toggle a light..."){
		input "switches", "capability.switch", multiple: false

def installed()
	subscribe(contact1, "contact.open", contactOpenHandler)
	subscribe(switches, "switch", switchHandler)

def updated()
	subscribe(contact1, "contact.open", contactOpenHandler)
	subscribe(switches, "switch", switchHandler)

def switchHandler(evt) {
	// if the switch was turned off by the user and the contact is closed then don't automatically
	// turn off switch next time contact is opened
	state.lastSwitchPhysical=(evt.physical && evt.value == "off" && contact1.currentContact == "closed")

def contactOpenHandler(evt) {
	log.debug "$evt.value: $evt, $settings"
	if (switches.currentSwitch == "off") {
		if (!state.lastSwitchPhysical) {
			log.trace "Turning on switch: $switches"
		} else {
			log.trace "User turned off switch manually, skipping turning on $switches"
	} else {
		log.trace "Turning off switch: $switches"

Is there a time limit or lux limit setting? This sounds perfect for toggling the deck light when letting the dog out, but would only need to happen when it’s dark.

@Automated_House when you install it, you can set it for specific modes - set it for the ‘night’ mode :slight_smile:

I only turn on night mode when we go to bed. Maybe i’ll make my first foray into coding and try to add it.

This sounds awesome. Saving for later. Need to buy more bulbs!

Thanks for posting.