-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lights on Arrival
118 lines (108 loc) · 3.39 KB
/
Lights on Arrival
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
/**
* Lights on Arrival
*
* Copyright 2014 Alex Malikov
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
*/
definition(
name: "Lights on Arrival",
namespace: "625alex",
author: "Alex Malikov",
description: "Turn lights on when arriving if it's dark outside",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
preferences {
section("When one of these persons arrives") {
input "people", "capability.presenceSensor", multiple: true
}
section("And it's dark...") {
input "luminance", "capability.illuminanceMeasurement", title: "Where?"
}
section("Then flash..."){
input "switches", "capability.switch", title: "These lights", multiple: true
input "numFlashes", "number", title: "This number of times (default 3)", required: false
}
section("Time settings in milliseconds (optional)..."){
input "onFor", "number", title: "On for (default 1000)", required: false
input "offFor", "number", title: "Off for (default 1000)", required: false
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
subscribe()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
subscribe()
}
def initialize() {
subscribe(people, "presence", presenseHandler)
}
def presenceHandler(evt) {
log.debug "presence $evt.value"
if (evt.value == "present") {
def lightSensorState = luminance.currentIlluminance
log.debug "SENSOR = $lightSensorState"
if (lightSensorState && lightSensorState < 20) {
log.trace "light.on() ... [luminance: ${lightSensorState}]"
} else if (evt.value == "not present") {
flashLights()
}
else if (evt.value == "not present") {
flashLights()
}
}
}
private flashLights() {
def doFlash = true
def onFor = onFor ?: 600000
def offFor = offFor ?: 0
def numFlashes = numFlashes ?: 1
log.debug "LAST ACTIVATED IS: ${state.lastActivated}"
if (state.lastActivated) {
def elapsed = now() - state.lastActivated
def sequenceTime = (numFlashes + 1) * (onFor + offFor)
doFlash = elapsed > sequenceTime
log.debug "DO FLASH: $doFlash, ELAPSED: $elapsed, LAST ACTIVATED: ${state.lastActivated}"
}
if (doFlash) {
log.debug "FLASHING $numFlashes times"
state.lastActivated = now()
log.debug "LAST ACTIVATED SET TO: ${state.lastActivated}"
def initialActionOn = switches.collect{it.currentSwitch != "on"}
def delay = 1L
numFlashes.times {
log.trace "Switch on after $delay msec"
switches.eachWithIndex {s, i ->
if (initialActionOn[i]) {
s.on(delay: delay)
}
else {
s.off(delay:delay)
}
}
delay += onFor
log.trace "Switch off after $delay msec"
switches.eachWithIndex {s, i ->
if (initialActionOn[i]) {
s.off(delay: delay)
}
else {
s.on(delay:delay)
}
}
delay += offFor
}
}
}