-
Notifications
You must be signed in to change notification settings - Fork 5
/
BathroomLightControl.groovy
411 lines (372 loc) · 17.8 KB
/
BathroomLightControl.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Bathroom Light/Fan Control
*
* Controls bathroom lights:
* -- Turn on light when motion is detected.
* -- Turn off bathroom light after x minutes of no motion detection
* -- Extend off timer to x minutes if humidity sensor is above the average for the room and trending upward (user in the shower?)
* -- Optionally turn on exhaust fan (switch) when humidity is above the average for the room and turn off when it is equal to or below the average.
*
* Developed / Tested with Aeon Multisensor which by default sends humidty every 8 minutes.
*
* --------------------------------------------------------------------------
*
* Copyright (c) 2014 Rayzurbock.com
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --------------------------------------------------------------------------
*
* The latest version of this file can be found on GitHub at:
* http://github.com/rayzurbock/SmartThings-BathroomLightControl
*
* Version 1.0.2 (2014-11-29)
*/definition(
name: "Bathroom Light/Fan Control",
namespace: "rayzurbock",
author: "[email protected]",
description: "Control light in the bathroom based on motion, extend when humidty rises (Shower in use); optionally turn on exhaust fan",
category: "Green Living",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]",
iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/[email protected]")
preferences {
page(name: "appStatus")
page(name: "appConfigure")
}
def appStatus() {
dynamicPage(name: "appStatus", title: "Status - $app.label"){
section{
if (state.installed){
if (state.humiditytrend == "up"){ paragraph "Humidity is ${state.lastHumidity}% and rising. ${state.lastHumidityTimeStamp}" }
if (state.humiditytrend == "down"){ paragraph "Humidity is ${state.lastHumidity}% and falling. ${state.lastHumidityTimeStamp}" }
if (state.humiditytrend == "stay"){ paragraph "Humidity is ${state.lastHumidity}%. ${state.lastHumidityTimeStamp}" }
paragraph "Light is currently ${lightswitch.latestValue('switch')}. ${state.lastSwitchTimeStamp}"
if (!(fanswitch == null)) {
paragraph "Fan is currently ${fanswitch.latestValue('switch')}. ${state.lastFanTimeStamp}"
}
paragraph "Using ${state.mode} based timer."
} else {
paragraph "Not configured, please configure."
paragraph ""
}
href "appConfigure", title:"Configure", description:"Tap to open"
paragraph ""
if (!(state.appversion == null)) {
paragraph "Bathroom Light/Fan Control ${state.appversion}"
} else {
paragraph "Bathroom Light/Fan Control"
}
paragraph "http://github.com/rayzurbock"
}
}
}
def appConfigure(){
dynamicPage(name: "appConfigure", title: "Configure - $app.label", install:true, uninstall:state.installed){
section{
def inputoffaftermotion = [
name: "motionOff",
type: "number",
title: "Turn off after motion is inactive (minutes)",
defaultValue: 5,
required: true
]
def inputoffafterhumidity = [
name: "humidityOff",
type: "number",
title: "Turn off after motion is inactive and humidity is higher than average (minutes)",
defaultValue: 10,
required: true
]
def inputpollsforhumidity = [
name: "humiditypollsforaverage",
type: "number",
title: "How many humidity sensor readings should determine the average/normal humidity for the room?",
defaultValue: 4,
required: true
]
input "lightswitch", "capability.switch", title: "Light Switch", required: true
input "motionSensor", "capability.motionSensor", title: "Motion Sensor(s)", required: true
input "humiditySensor", "capability.relativeHumidityMeasurement", title: "Humidity Sensor(s)", required: true
input "fanswitch", "capability.switch", title: "Fan Switch (optional)", required: false
input inputoffaftermotion
input inputoffafterhumidity
input inputpollsforhumidity
if (!(state.appversion == null)) {
paragraph "Bathroom Light/Fan Control ${state.appversion}"
} else {
paragraph "Bathroom Light/Fan Control"
}
paragraph "http://github.com/rayzurbock"
}
section([mobileOnly:true]){
label title: "Bathroom name", required: true
mode title: "Set for specific mode(s)", required: false
}
}
}
def installed() {
initialize()
if (state.loglevel > 0){TRACE("Installed with settings: ${settings}")}
}
def updated() {
unsubscribe()
initialize()
if (state.loglevel > 0){TRACE("Updated with settings: ${settings}")}
}
def initialize() {
state.appversion = "1.0.2"
state.loglevel = 2 //0 = off, 1 = on, 2 = debug
//Subscribe to device events
subscribe(lightswitch, "switch", SwitchEvent)
subscribe(motionSensor, "motion", MotionEvent)
subscribe(humiditySensor, "humidity", HumidityEvent)
subscribe(fanswitch, "switch", FanEvent)
//Init state variables
state.averagehumiditymaxpollcount = settings.humiditypollsforaverage //How many polls should we base our average off of?
if (state.averagehumiditycurrentpollcount == null) { state.averagehumiditycurrentpollcount = 0 }
if (state.averageHumidity == null) {
if (humiditySensor.latestValue("humidity") == null) {
state.averageHumidity = 0
} else {
state.averageHumidity = humiditySensor.latestValue("humidity")
}
}
if (state.lastHumidity == null) {
if (humiditySensor.latestValue("humidity") == null) {
state.lastHumidity = 0
} else {
state.lastHumidity = humiditySensor.latestValue("humidity")
}
}
if (state.humiditytrend == null) { state.humiditytrend = "stay" } //Trend = stay, down, or up; App starts with "stay"
if (state.mode == null) { state.mode = "motion" }
if (state.trigger == null) { state.trigger = "none" }
if (state.offtime == null) { state.offtime = settings.motionOff * 60 }
if (state.scheduled == null) { state.scheduled = false }
if (state.lightswitchlastValue == null) { state.lightswitchlastValue = lightswitch.latestValue('switch') }
if (state.lastHumidityTimeStamp == null) { state.lastHumidityTimeStamp = "" }
if (state.lastSwitchTimeStamp == null) { state.lastSwitchTimeStamp = "" }
if (state.lastMotionTimeStamp == null) { state.lastMotionTimeStamp = "" }
if (state.lastFanTimeStamp == null) { state.lastFanTimeStamp = "" }
state.installed = true
if (state.loglevel == 2){DEBUG("Initialized")}
}
def SwitchEvent(evt) {
state.lastSwitchTimeStamp = "(" + timeStampMMDDHHmm() + ")"
if (!(fanswitch == null)) {
//If Humidity is high and fan is configured, turn fan on, back off when it reaches the average
if (state.lastHumidity > state.averageHumidity){
if (fanswitch.latestValue("switch") == "off"){fanOn()}
} else {
if (fanswitch.latestValue("switch") == "on"){fanOff()}
}
}
//DEBUG("DEBUG: Switch | Current: ${evt.value}, LastValue: ${state.lightswitchlastValue}")
if (evt.value == "on" && state.lightswitchlastValue == "off") {
if (state.trigger == "none") {
state.trigger = "manual"
}
if (state.loglevel == 2){DEBUG("Switch | ${evt.value}, trigger:${state.trigger}, mode: ${state.mode}, lasthumidity:${state.lastHumidity}, avghumidity:${state.averageHumidity}, trend:${state.humiditytrend}")}
if ((state.lastHumidity > state.averageHumidity && state.humiditytrend == "up") && state.mode == "motion" && state.trigger == "manual") {
if (state.loglevel == 2){DEBUG("Switch | Using humidity off timer (1)")}
unschedule()
state.mode = "humidity"
state.offtime = settings.humidityOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
} else {
if (((state.lastHumidity <= state.averageHumidity) || (state.humiditytrend == "down")) && (!(state.humiditytrend == "stay")) && (state.mode == "motion") && (state.trigger == "manual")) {
//Light came on, but humidity is below the threshold, set off time to motion off timer.
if (state.loglevel == 2){DEBUG("Switch | Using motion off timer (1)")}
unschedule()
state.mode = "motion"
state.offtime = settings.motionOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
if (state.humiditytrend == "stay" && state.trigger == "manual") {
if (state.mode == "motion") {
//Light came on, but humidity is below the threshold, set off time to motion off timer.
if (state.loglevel == 2){DEBUG("Switch | Using motion off timer (2)")}
unschedule()
state.offtime = settings.motionOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
if (state.mode == "humidity") {
if (state.loglevel == 2){DEBUG("Switch | Using humidity off timer (2)")}
unschedule()
state.offtime = settings.humidityOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
}
}
if ((state.lastHumidity > state.averageHumidity && state.humiditytrend == "up") && (state.mode == "humidity") && state.trigger == "manual") {
if (state.loglevel == 2){DEBUG("Switch | Using humidity off timer (3)")}
unschedule()
state.mode = "humidity"
state.offtime = settings.humidityOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
if ((state.lastHumidity <= state.averageHumidity || state.humiditytrend == "down") && !(state.humiditytrend == "stay") && (state.mode == "humidity") && state.trigger == "manual") {
if (state.loglevel == 2){DEBUG("Switch | Using motion off timer (3)")}
unschedule()
state.mode = "motion"
state.offtime = settings.motionOff * 60
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
}
if (evt.value == "off") {
if (state.loglevel == 2){DEBUG("Switch | Light was turned off, unscheduling off timer events")}
if (state.scheduled) { unschedule() }
state.trigger = "none"
}
state.lightswitchlastValue = lightswitch.latestValue('switch')
}
def MotionEvent(evt) {
state.lastMotionTimeStamp = "(" + timeStampMMDDHHmm() + ")"
if (state.loglevel == 2){DEBUG("Motion Sensor | ${evt.value}, LastTrigger:${state.trigger}")}
if (evt.value == "inactive" && state.lastMotionStatus == "active") {
//Motion is inactive
if (state.loglevel == 2){DEBUG("Motion Sensor | Motion inactive triggered")}
if (state.mode == "motion"){
unschedule()
state.offtime = settings.motionOff * 60
if (state.loglevel == 2){DEBUG("Motion Sensor | Scheduling off based on motion timer")}
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
if (state.mode == "humidity") {
unschedule()
state.offtime = settings.humidityOff * 60
if (state.loglevel == 2){DEBUG("Motion Sensor | Scheduling off based on humidity timer")}
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
} else {
//Motion is active
if (evt.value == "active" && state.lastMotionStatus == "inactive") {
if (state.trigger == "none" || state.trigger == "manual") {
if (state.loglevel == 2){DEBUG("Motion Sensor | Switching trigger from none/manual to motion")}
state.trigger = "motion"
}
if (state.loglevel == 2){DEBUG("Motion Sensor | Motion active triggered")}
if (lightswitch.latestValue("switch") == "off") { lightsOn() }
if (state.scheduled) { unschedule() }
}
}
state.lastMotionStatus = evt.value
}
def HumidityEvent(evt) {
state.lastHumidityTimeStamp = "(" + timeStampMMDDHHmm() + ")"
def currentHumidity = Double.parseDouble(evt.value.replace("%", ""))
state.humiditytrend = "stay"
if ((currentHumidity > state.lastHumidity) && (currentHumidity - state.lastHumidity > 1)) { state.humiditytrend = "up" }
if ((currentHumidity < state.lastHumidity) && (state.lastHumidity - currentHumidity > 1)) { state.humiditytrend = "down" }
if (!(state.averageHumidity > 0)) { state.humiditytrend = "stay" } //AverageHumidity hasn't been established yet; stay.
if (state.loglevel == 2){DEBUG("Humidity Sensor | Current(${currentHumidity}),last(${state.lastHumidity}),trend(${state.humiditytrend})")}
state.lastHumidity = currentHumidity
//Start Check Humidity Average
if (state.averagehumiditycurrentpollcount == state.averagehumiditymaxpollcount) {
//Average the data
state.averageHumidity = (state.averageHumidityCalc / state.averagehumiditymaxpollcount)
if (state.loglevel == 2){DEBUG("Humidity Sensor | Average Humidity (${state.averagehumiditymaxpollcount} polls) = ${state.averageHumidity}, Trend: ${state.humiditytrend}")}
state.averageHumidityCalc = 0
state.averagehumiditycurrentpollcount = 0
} else {
//Collect more data
if (state.averageHumidityCalc > 0) {
state.averageHumidityCalc = (state.averageHumidityCalc + state.lastHumidity)
} else {
state.averageHumidityCalc = state.lastHumidity
state.averageHumidity = state.lastHumidity
}
state.averagehumiditycurrentpollcount = state.averagehumiditycurrentpollcount + 1
if (state.loglevel == 2){DEBUG("Humidity Sensor | Collected Humidity on poll # ${state.averagehumiditycurrentpollcount} of ${state.averagehumiditymaxpollcount}")}
if (state.loglevel == 2){DEBUG("Humidity Sensor | Current Average Humidity is ${state.averageHumidityCalc / state.averagehumiditycurrentpollcount}, Trend: ${state.humiditytrend}")}
}
//End Check Humidity Average
if (lightswitch.latestValue('switch') == "on") {
if ((state.lastHumidity > state.averageHumidity && state.humiditytrend == "up") && state.mode == "motion") {
if (state.loglevel > 0){TRACE("Humidity Sensor | Humidity > Threshold; adjusting to use humidity timer")}
state.mode = "humidity"
state.offtime = settings.humidityOff * 60
if (state.loglevel == 2){
DEBUG("Humidity Sensor | Scheduling off based on inactivity + humidity ${state.offtime}")
sendNotificationEvent("BLC: ${app.label} switched to Humidity timer via humidity event")
}
unschedule()
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
if ((state.lastHumidity <= state.averageHumidity || state.humiditytrend == "down") && state.mode == "humidity") {
if (state.loglevel > 0){TRACE("Humidity Sensor | Humidity <= Threshold or trending down; adjusting to use motion timer")}
state.mode = "motion"
state.offtime = settings.motionOff * 60
if (state.loglevel == 2){
DEBUG("Humidity Sensor | Scheduling off based on inactivity ${state.offtime}")
sendNotificationEvent("BLC: ${app.label} switched to Motion timer via humidity event")
}
unschedule()
runIn(state.offtime, "lightsOut")
state.scheduled = true
}
}
if (!(fanswitch == null)) {
//If Humidity is high and fan is configured, turn fan on, back off when it reaches the average
if (state.lastHumidity > state.averageHumidity){
if (fanswitch.latestValue("switch") == "off"){fanOn()}
} else {
if (fanswitch.latestValue("switch") == "on"){fanOff()}
}
}
}
def FanEvent(evt){
state.lastFanTimeStamp = "(" + timeStampMMDDHHmm() + ")"
}
def lightsOn() {
TRACE("BLC: lightsOn()")
lightswitch.on()
}
def lightsOut() {
TRACE("BLC: lightsOut()")
lightswitch.off()
}
def fanOn(){
TRACE("BLC: fanOn()")
fanswitch.on()
}
def fanOff(){
TRACE("BLC: fanOff()")
fanswitch.off()
}
def TRACE(message){
log.trace("BLC: ${message}")
}
def DEBUG(message){
log.debug("BLC: ${message}")
}
def timeStampHHmm(){
return (new Date(now())).format("HH:mm", location.timeZone)
}
def timeStampHHmmss(){
return (new Date(now())).format("HH:mm:ss", location.timeZone)
}
def timeStampMMDDHHmm(){
return (new Date(now())).format("MMM dd HH:mm", location.timeZone)
}