Update hue-mood-lighting.groovy
[smartapps.git] / official / let-there-be-dark.groovy
1 /**
2  *  Let There Be Dark!
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.
4  *
5  *  Author: SmartThings modified by Douglas Rich
6  */
7 definition(
8     name: "Let There Be Dark!",
9     namespace: "Dooglave",
10     author: "Dooglave",
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"
15 )
16
17 preferences {
18         section("When the door opens") {
19                 input "contact1", "capability.contactSensor", title: "Where?"
20         }
21         section("Turn off a light") {
22                 input "switch1", "capability.switch"
23         }
24 }
25
26 def installed() {
27         subscribe(contact1, "contact", contactHandler)
28 }
29
30 def updated() {
31         unsubscribe()
32         subscribe(contact1, "contact", contactHandler)
33 }
34
35 def contactHandler(evt) {
36         log.debug "$evt.value"
37         if (evt.value == "open") {
38         state.wasOn = switch1.currentValue("switch") == "on"
39                 switch1.off()
40 }       
41
42 if (evt.value == "closed") {
43         if(state.wasOn)switch1.on()
44 }
45 }