3 * Turn your lights off when a Contact Sensor is opened and turn them back on when it is closed, ONLY if the Lights were previouly on.
5 * Author: SmartThings modified by Douglas Rich
8 name: "Let There Be Dark!",
11 description: "Turn your lights off when a Contact Sensor is opened and turn them back on when it is closed, ONLY if the Lights were previouly on",
12 category: "Convenience",
13 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet.png",
14 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_contact-outlet@2x.png"
18 section("When the door opens") {
19 input "contact1", "capability.contactSensor", title: "Where?"
21 section("Turn off a light") {
22 input "switch1", "capability.switch"
27 subscribe(contact1, "contact", contactHandler)
32 subscribe(contact1, "contact", contactHandler)
35 def contactHandler(evt) {
36 log.debug "$evt.value"
37 if (evt.value == "open") {
38 state.wasOn = switch1.currentValue("switch") == "on"
42 if (evt.value == "closed") {
43 if(state.wasOn)switch1.on()