Adding more official apps gotten from https://github.com/SmartThingsCommunity/Code
[smartapps.git] / official / light-follows-me.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  *  in compliance with the License. You may obtain a copy of the License at:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11  *  for the specific language governing permissions and limitations under the License.
12  *
13  *  Light Follows Me
14  *
15  *  Author: SmartThings
16  */
17
18 definition(
19     name: "Light Follows Me",
20     namespace: "smartthings",
21     author: "SmartThings",
22     description: "Turn your lights on when motion is detected and then off again once the motion stops for a set period of time.",
23     category: "Convenience",
24     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
25     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch@2x.png"
26 )
27
28 preferences {
29         section("Turn on when there's movement..."){
30                 input "motion1", "capability.motionSensor", title: "Where?"
31         }
32         section("And off when there's been no movement for..."){
33                 input "minutes1", "number", title: "Minutes?"
34         }
35         section("Turn on/off light(s)..."){
36                 input "switches", "capability.switch", multiple: true
37         }
38 }
39
40 def installed() {
41         subscribe(motion1, "motion", motionHandler)
42 }
43
44 def updated() {
45         unsubscribe()
46         subscribe(motion1, "motion", motionHandler)
47 }
48
49 def motionHandler(evt) {
50         log.debug "$evt.name: $evt.value"
51         if (evt.value == "active") {
52                 log.debug "turning on lights"
53                 switches.on()
54         } else if (evt.value == "inactive") {
55                 runIn(minutes1 * 60, scheduleCheck, [overwrite: false])
56         }
57 }
58
59 def scheduleCheck() {
60         log.debug "schedule check"
61         def motionState = motion1.currentState("motion")
62     if (motionState.value == "inactive") {
63         def elapsed = now() - motionState.rawDateCreated.time
64         def threshold = 1000 * 60 * minutes1 - 1000
65         if (elapsed >= threshold) {
66             log.debug "Motion has stayed inactive long enough since last check ($elapsed ms):  turning lights off"
67             switches.off()
68         } else {
69                 log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms):  doing nothing"
70         }
71     } else {
72         log.debug "Motion is active, do nothing and wait for inactive"
73     }
74 }