/
HallLightWelcomeHome.txt.groovy
77 lines (62 loc) · 1.82 KB
/
HallLightWelcomeHome.txt.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* Hall Light: Welcome Home
*
* Author: brian@bevey.org
* Date: 9/25/13
*
* Turn on the hall light if someone comes home (presence) and the door opens.
*/
definition(
name: "Hall Light: Welcome Home",
namespace: "imbrianj",
author: "brian@bevey.org",
description: "Turn on the hall light if someone comes home (presence) and the door opens.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)
preferences {
section("People to watch for?") {
input "people", "capability.presenceSensor", multiple: true
}
section("Front Door?") {
input "sensors", "capability.contactSensor", multiple: true
}
section("Hall Light?") {
input "lights", "capability.switch", title: "Switch Turned On", multilple: true
}
section("Presence Delay (defaults to 30s)?") {
input name: "presenceDelay", type: "number", title: "How Long?", required: false
}
section("Door Contact Delay (defaults to 10s)?") {
input name: "contactDelay", type: "number", title: "How Long?", required: false
}
}
def installed() {
init()
}
def updated() {
unsubscribe()
init()
}
def init() {
state.lastClosed = now()
subscribe(people, "presence.present", presence)
subscribe(sensors, "contact.open", doorOpened)
}
def presence(evt) {
def delay = contactDelay ?: 10
state.lastPresence = now()
if(now() - (delay * 1000) < state.lastContact) {
log.info('Presence was delayed, but you probably still want the light on.')
lights?.on()
}
}
def doorOpened(evt) {
def delay = presenceDelay ?: 30
state.lastContact = now()
if(now() - (delay * 1000) < state.lastPresence) {
log.info('Welcome home! Let me get that light for you.')
lights?.on()
}
}