2 * Copyright 2015 SmartThings
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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
29 section("Turn on when there's movement..."){
30 input "motion1", "capability.motionSensor", title: "Where?"
32 section("And off when there's been no movement for..."){
33 input "minutes1", "number", title: "Minutes?"
35 section("Turn on/off light(s)..."){
36 input "switches", "capability.switch", multiple: true
41 subscribe(motion1, "motion", motionHandler)
46 subscribe(motion1, "motion", motionHandler)
49 def motionHandler(evt) {
50 log.debug "$evt.name: $evt.value"
51 if (evt.value == "active") {
52 log.debug "turning on lights"
54 } else if (evt.value == "inactive") {
55 runIn(minutes1 * 60, scheduleCheck, [overwrite: false])
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"
69 log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): doing nothing"
72 log.debug "Motion is active, do nothing and wait for inactive"