-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmart Light
277 lines (246 loc) · 9.11 KB
/
Smart Light
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Smart security Light
*
* Author: SmartThings
*
*
* Adapted from SmartThings' Smart NightLight by Barry Burke
*
* Changes:
* 2014/09/23 Added support for physical override:
* * If lights turned on manually, don't turn them off if motion stops
* but DO turn them off at sunrise (in case the are forgotten)
* * Double-tap ON will stop the motion-initiated timed Off event
* * Double-tap OFF will keep the lights off until it gets light, someone manually
* turns on or off the light, or another app turns on the lights.
* * TO RE-ENABLE MOTION CONTROL: Manually turn OFF the lights (single tap)
*
*
*/
definition(
name: "Smart Security Light",
namespace: "smartthings",
author: "SmartThings & Barry Burke",
description: "Turns on lights when it's dark and motion is detected. Turns lights off when it becomes light or some time after motion ceases. Optionally allows for manual override.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/[email protected]"
)
preferences {
section("Control these lights..."){
input "light", "capability.switch", multiple: false
}
section("Turning on when it's dark and there's movement..."){
input "motionSensor", "capability.motionSensor", title: "Where?"
}
section("And then off when it's light or there's been no movement for..."){
input "delayMinutes", "number", title: "Minutes?"
}
section("Using either on this light sensor (optional) or the local sunrise and sunset"){
input "lightSensor", "capability.illuminanceMeasurement", required: false
input "luxLevel", "number", title: "Darkness Lux level?", defaultValue: 50, required: true
}
section ("Sunrise offset (optional)...") {
input "sunriseOffsetValue", "text", title: "HH:MM", required: false
input "sunriseOffsetDir", "enum", title: "Before or After", required: false, metadata: [values: ["Before","After"]]
}
section ("Sunset offset (optional)...") {
input "sunsetOffsetValue", "text", title: "HH:MM", required: false
input "sunsetOffsetDir", "enum", title: "Before or After", required: false, metadata: [values: ["Before","After"]]
}
section ("Zip code (optional, defaults to location coordinates when location services are enabled)...") {
input "zipCode", "text", required: false
}
section ("Overrides") {
input "physicalOverride", "bool", title: "Physical override?", required: true, defaultValue: false
paragraph "Double-tap ON to lock light on"
input "doubleTapOn", "bool", title: "Double-Tap ON override?", required: true, defaultValue: true
input "doubleTapOff", "bool", title: "Double-Tap OFF override?", required: true, defaultValue: true
}
}
def installed() {
log.debug "Installed with settings: $settings"
initialize()
}
def updated() {
log.debug "Updated with settings: $settings"
unsubscribe()
unschedule()
initialize()
}
def initialize() {
subscribe(motionSensor, "motion", motionHandler)
if (physicalOverride) {
subscribe(light, "switch.on", lightsOnHandler)
subscribe(light, "switch.off", lightsOffHandler)
}
if (doubleTapOn || doubleTapOff) {
subscribe(light, "switch", switchHandler, [filterEvents: false])
}
if (light.latestValue( "switch" ) == "on") {
state.physical = true
state.lastStatus = "on"
}
else {
state.physical = false
state.lastStatus = "off"
}
state.keepOff = false
if (lightSensor) {
subscribe(lightSensor, "illuminance", illuminanceHandler, [filterEvents: false])
}
else {
astroCheck()
def sec = Math.round(Math.floor(Math.random() * 60))
def min = Math.round(Math.floor(Math.random() * 60))
def cron = "$sec $min * * * ?"
schedule(cron, astroCheck) // check every hour since location can change without event?
}
}
def lightsOnHandler(evt) {
log.debug "lightsOnHandler: $evt.name: $evt.value"
if (evt.isPhysical()) {
state.physical = true
}
state.keepOff = false // If anything manages to turn the light on, stop the keepOff
}
def lightsOffHandler(evt) {
log.debug "lightsOffHandler: $evt.name: $evt.value"
state.physical = false
state.lastStatus = "off"
}
def switchHandler(evt) {
log.debug "switchHandler: $evt.name: $evt.value"
// use Event rather than DeviceState because we may be changing DeviceState to only store changed values
def recentStates = light.eventsSince(new Date(now() - 4000), [all:true, max: 10]).findAll{it.name == "switch"}
log.debug "${recentStates?.size()} states found, last at ${recentStates ? recentStates[0].dateCreated : ''}"
if (evt.isPhysical()) {
if (evt.value == "on") {
state.keepOff = false
if (state.lastStatus == "off") {
if (physicalOverride) { state.physical = true } // Manual on BEFORE motion on
}
else if (lastTwoStatesWere("on", recentStates, evt)) {
log.debug "detected two ON taps, override motion"
if (doubleTapOn) { state.physical = true } // Manual override of PRIOR motion on
}
}
else if (evt.value == "off") {
state.physical = false // Somebody turned off the light
state.keepOff = false // Single off resets keepOff
if (lastTwoStatesWere("off", recentStates, evt)) {
log.debug "detected two OFF taps, doing nothing"
if (doubleTapOff) { state.keepOff = true } // Double tap enables the keepOff
}
}
}
}
private lastTwoStatesWere(value, states, evt) {
def result = false
if (states) {
log.trace "unfiltered: [${states.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
def onOff = states.findAll { it.isPhysical() || !it.type }
log.trace "filtered: [${onOff.collect{it.dateCreated + ':' + it.value}.join(', ')}]"
// This test was needed before the change to use Event rather than DeviceState. It should never pass now.
if (onOff[0].date.before(evt.date)) {
log.warn "Last state does not reflect current event, evt.date: ${evt.dateCreated}, state.date: ${onOff[0].dateCreated}"
result = evt.value == value && onOff[0].value == value
}
else {
result = onOff.size() > 1 && onOff[0].value == value && onOff[1].value == value
}
}
result
}
def motionHandler(evt) {
log.debug "motionHandler: $evt.name: $evt.value"
if (state.physical) { return } // ignore motion if lights were most recently turned on manually
if (evt.value == "active") {
if (enabled()) {
log.debug "turning on light due to motion"
light.on()
state.lastStatus = "on"
}
state.motionStopTime = null
}
else {
if (state.keepOff) { return }
state.motionStopTime = now()
if(delayMinutes) {
unschedule(turnOffMotionAfterDelay)
runIn(delayMinutes*60, turnOffMotionAfterDelay, [overwrite: false])
} else {
turnOffMotionAfterDelay()
}
}
}
def illuminanceHandler(evt) {
log.debug "$evt.name: $evt.value, lastStatus: $state.lastStatus, motionStopTime: $state.motionStopTime"
def lastStatus = state.lastStatus // its getting light now, we can turn off
if (state.keepOff && evt.integerValue >= luxLevel) { state.keepOff = false } // reset keepOff
if ((lastStatus != "off") && (evt.integerValue >= luxLevel)) { // whether or not it was manually turned on
light.off()
state.lastStatus = "off"
state.physical = false
state.keepOff = false // it's a new day
}
else if (state.motionStopTime) {
if (state.physical) { return } // light was manually turned on
if (lastStatus != "off") {
def elapsed = now() - state.motionStopTime
if (elapsed >= (delayMinutes ?: 0) * 60000L) {
light.off()
state.lastStatus = "off"
state.keepOff = false
}
}
}
else if (lastStatus != "on" && evt.integerValue < luxLevel) {
if ( state.keepOff ) { return } // or we locked it off for the night
light.on()
state.lastStatus = "on"
state.physical = false // we turned them on...
}
}
def turnOffMotionAfterDelay() {
log.debug "In turnOffMotionAfterDelay"
if (state.physical) { return } // light was manually turned on
// Don't turn it off
if (state.motionStopTime && state.lastStatus != "off") {
def elapsed = now() - state.motionStopTime
if (elapsed >= (delayMinutes ?: 0) * 60000L) {
light.off()
state.lastStatus = "off"
}
}
}
def scheduleCheck() {
log.debug "In scheduleCheck - skipping"
//turnOffMotionAfterDelay()
}
def astroCheck() {
def s = getSunriseAndSunset(zipCode: zipCode, sunriseOffset: sunriseOffset, sunsetOffset: sunsetOffset)
state.riseTime = s.sunrise.time
state.setTime = s.sunset.time
log.debug "rise: ${new Date(state.riseTime)}($state.riseTime), set: ${new Date(state.setTime)}($state.setTime)"
}
private enabled() {
def result
if (state.keepOff) {
result = false // if OFF was double-tapped, don't turn on
}
else if (lightSensor) {
result = (lightSensor.currentIlluminance as Integer) < luxLevel
}
else {
def t = now()
result = t < state.riseTime || t > state.setTime
}
result
}
private getSunriseOffset() {
sunriseOffsetValue ? (sunriseOffsetDir == "Before" ? "-$sunriseOffsetValue" : sunriseOffsetValue) : null
}
private getSunsetOffset() {
sunsetOffsetValue ? (sunsetOffsetDir == "Before" ? "-$sunsetOffsetValue" : sunsetOffsetValue) : null
}