Door 1 OR 2 open turn on light; door 1 AND 2 closed turn off?


(Chris C.) #1

Ok, so this sounds simple enough, but I was wondering if someone already did this in a smartapp somewhere? I have two garage doors and want to turn on the garage lights if either one is opened and then turn them back off when both are closed.
I’m kinda new to ST, and haven’t really coded anything but would give it a try if there was something I could possibly work from to learn. Any help greatly appreciated.

Chris


(Reed Taylor) #2

I have two doors into a single closet and wrote exactly this app. It’s quite simple, but does the job pretty well. Here’s the code:

/**
 *  Light Turn On Any Open
 *
 *  Copyright 2014 Reed Taylor
 *
 *  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: "Light On while Any Open",
    namespace: "schmedlock",
    author: "Reed Taylor",
    description: "Turn a light on using a logical OR of some sensors being open",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")


preferences {
	section("When any of these are open...") {
		input "contact1", "capability.contactSensor", title: "Doors", multiple: true
	}
	section("Turn these on:") {
		input "switch1", "capability.switch", title: "Switches", multiple: true
	}
}

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

	initialize()
}

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

	unsubscribe()
	initialize()
}

def initialize() {
	subscribe(contact1, "contact", contactHandler)
}

def contactHandler(evt) {
	log.trace "contactHandler: $evt ${evt.value}"
    
    def openSensor = contact1.find{it.currentValue("contact") == "open"}
    if (openSensor != null) {
    	log.debug "At least one sensor (${openSensor.label}) is open, switch on"
        switch1.on()
    } else {
    	log.debug "No sensors are open, switch off"
        switch1.off()
    }
}

(Chris C.) #3

Fantastic! I just installed your app, and it works perfectly! Thank you so much.
Now I just need to learn how to write code like this. :smile:

Chris


(Reed Taylor) #4

Glad to hear it worked well for you! :smile:

Reed


#5

This is fantastic, thank you! I only wish there was one other thing: “Turn off after __ minutes.” What do I need to add for this?


(Chris C.) #6

That’s an easy one, just use the built-in smart-app under Lights & Switches called power allowance to turn off off after a certain amount of time :slight_smile:


#7

That doesn’t quite work because this app immediately turns off the light as soon as one of the doors is closed.

Here’s what I’m doing:

I open door from garage, which should turn on the light at the basement steps. Those lights should stay on while I shut the door and climb the steps to open the kitchen door and go inside. The same should happen in reverse.

This app works exactly as it should, except I’m looking for a delay before the light is shut off so I can get from one door to the next without climbing the steps in darkness. :smiley:


(Jimmy) #8

You can do this easily with Smart Lighting. Once I get some cell signal I’ll take a screen shot and upload it.


(Jimmy) #9


#10

Aha! Didn’t realize you could include multiple sensors there. Problem solved, thanks!


(Ankit) #11

@prjct92eh2 you have mentioned 5 minutes but there might be instance when can I cannot reach second door in 5 minutes. I want my garage light to open when Garage door is opened and garage entry door is closed. And vice versa.