forked from joelwetzel/Hubitat-Combined-Presence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
advancedCombinedPresenceInstance.groovy
335 lines (270 loc) · 9.3 KB
/
advancedCombinedPresenceInstance.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
/**
* Advanced Combined Presence Instance v2.2.1
*
* Copyright 2020 Joel Wetzel
*
* 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.
*
*/
import groovy.time.*
import groovy.json.*
definition(
name: "Combined Presence Advanced Combiner",
parent: "joelwetzel:Combined Presence",
namespace: "joelwetzel",
author: "Joel Wetzel",
description: "This will set a virtual presence sensor to advanced combinations of all the input sensors. It is a child app of Combined Presence.",
category: "Safety & Security",
iconUrl: "",
iconX2Url: "",
iconX3Url: "")
def inputSensorsArrivingOr = [
name: "inputSensorsArrivingOr",
type: "capability.presenceSensor",
title: "ANY of these arrive",
multiple: true,
required: false
]
def inputSensorsArrivingAnd = [
name: "inputSensorsArrivingAnd",
type: "capability.presenceSensor",
title: "ALL of these are present",
multiple: true,
required: false
]
def inputSensorsDepartingOr = [
name: "inputSensorsDepartingOr",
type: "capability.presenceSensor",
title: "ANY of these depart",
multiple: true,
required: false
]
def inputSensorsDepartingAnd = [
name: "inputSensorsDepartingAnd",
type: "capability.presenceSensor",
title: "ALL of these are not present",
multiple: true,
required: false
]
def outputSensor = [
name: "outputSensor",
type: "capability.presenceSensor",
title: "Output Sensor",
description: "The virtual presence sensor that will be controlled by this instance.",
multiple: false,
required: true
]
def notificationDevice = [
name: "notificationDevice",
type: "capability.notification",
title: "Devices for Notifications",
description: "Send notifications to devices. ie. push notifications to a phone.",
required: false,
multiple: true
]
def notifyAboutStateChanges = [
name: "notifyAboutStateChanges",
type: "bool",
title: "Notify about state changes to the Output sensor",
default: false
]
def notifyAboutInconsistencies = [
name: "notifyAboutInconsistencies",
type: "bool",
title: "Notify about inconsistent Inputs for more than 30 minutes",
description: "Send notifications if input sensors have inconsistent values for an extended period.",
default: false
]
def enableLogging = [
name: "enableLogging",
type: "bool",
title: "Enable Debug Logging?",
defaultValue: false,
required: true
]
preferences {
page(name: "mainPage", title: "", install: true, uninstall: true) {
section(getFormat("title", "Advanced Combiner")) {
}
section(hideable: true, "If the output is Not Present, then make it arrive if:") {
input inputSensorsArrivingOr
paragraph "OR"
input inputSensorsArrivingAnd
}
section() {
paragraph ""
}
section(hideable: true, "If the output is Present, then make it depart if:") {
input inputSensorsDepartingOr
paragraph "OR"
input inputSensorsDepartingAnd
}
section() {
paragraph ""
}
section() {
input outputSensor
}
section() {
paragraph ""
}
section(hideable: true, hidden: true, "Notifications") {
input notificationDevice
input notifyAboutStateChanges
paragraph "This will send a notification any time the state of the Output Sensor is changed by Combined Presence."
input notifyAboutInconsistencies
paragraph "This will send notifications if your input sensors stay inconsistent for more than 30 minutes. That usually means one of the sensors has stopped reporting, and should be checked."
}
section() {
input enableLogging
}
}
}
def installed() {
log.info "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.info "Updated with settings: ${settings}"
initialize()
}
def initialize() {
unschedule()
unsubscribe()
subscribe(inputSensorsArrivingOr, "presence", presenceChangedHandler)
subscribe(inputSensorsArrivingAnd, "presence", presenceChangedHandler)
subscribe(inputSensorsDepartingOr, "presence", presenceChangedHandler)
subscribe(inputSensorsDepartingAnd, "presence", presenceChangedHandler)
app.updateLabel("Advanced Combiner for ${outputSensor.displayName}")
use (groovy.time.TimeCategory) {
state.lastInconsistencyWarningTime = new Date()-24.hours
state.lastConsistentTime = new Date()
}
runEvery1Minute(checkForInconsistencies)
}
def checkForInconsistencies() {
log "***** checkForInconsistencies()"
def inputsAreAllPresent = true
def inputsAreAllNotPresent = true
inputSensorsArrivingOr.each { inputSensor ->
if (inputSensor.currentValue("presence") == "present") {
inputsAreAllNotPresent = false
}
if (inputSensor.currentValue("presence") == "not present") {
inputsAreAllPresent = false
}
}
def inputsAreInconsistent = !(inputsAreAllPresent || inputsAreAllNotPresent)
log "inputsAreAllPresent ${inputsAreAllPresent}"
log "inputsAreAllNotPresent ${inputsAreAllNotPresent}"
log "inputsAreInconsistent ${inputsAreInconsistent}"
def currentTime = new Date()
if (inputsAreInconsistent) {
def lastConsistentTime = new Date()
if (state.lastConsistentTime) {
lastConsistentTime = Date.parse("yyyy-MM-dd'T'HH:mm:ssZ", state.lastConsistentTime)
}
def lastInconsistencyWarningTime = new Date()
if (state.lastInconsistencyWarningTime) {
lastInconsistencyWarningTime = Date.parse("yyyy-MM-dd'T'HH:mm:ssZ", state.lastInconsistencyWarningTime)
}
def timeSinceConsistency = TimeCategory.minus(currentTime, lastConsistentTime)
def timeSinceLastWarning = TimeCategory.minus(currentTime, lastInconsistencyWarningTime)
log "timeSinceConsistency.minutes: ${timeSinceConsistency.minutes}"
log "timeSinceConsistency.hours: ${timeSinceConsistency.hours}"
log "timeSinceConsistency.days: ${timeSinceConsistency.days}"
log "timeSinceLastWarning.hours ${timeSinceLastWarning.hours}"
log "timeSinceLastWarning.days ${timeSinceLastWarning.days}"
if ((timeSinceConsistency.minutes > 30 || timeSinceConsistency.hours > 0 || timeSinceConsistency.days > 0) && (timeSinceLastWarning.hours > 18 || timeSinceLastWarning.days > 0)) {
def msg = "Input sensors for ${outputSensor.displayName} have been inconsistent for 30 minutes. This may mean one of your presence sensors is not updating."
log(msg)
if (notifyAboutInconsistencies) {
sendNotification(msg)
}
state.lastInconsistencyWarningTime = currentTime
}
}
else {
state.lastConsistentTime = currentTime
}
}
def sendNotification(msg) {
if (msg && msg.size() > 0) {
if (notificationDevice) {
notificationDevice.deviceNotification(msg)
}
}
}
def presenceChangedHandler(evt) {
log "PRESENCE CHANGED for: ${evt.device.name}"
//log.debug groovy.json.JsonOutput.toJson(evt)
def oldPresent = outputSensor.currentValue("presence") == "present"
def newPresent = false
if (!oldPresent) {
def anyHaveArrived = false
inputSensorsArrivingOr.each { inputSensor ->
if (inputSensor.currentValue("presence") == "present" && inputSensor.name == evt.device.name) {
//log.debug "ARRIVED: ${inputSensor.name}"
anyHaveArrived = true
}
}
def allHaveArrived = inputSensorsArrivingAnd && inputSensorsArrivingAnd.size() > 0
inputSensorsArrivingAnd.each { inputSensor ->
if (inputSensor.currentValue("presence") != "present") {
allHaveArrived = false
}
}
newPresent = anyHaveArrived || allHaveArrived
}
else {
def anyHaveDeparted = false
inputSensorsDepartingOr.each { inputSensor ->
if (inputSensor.currentValue("presence") != "present" && inputSensor.name == evt.device.name) {
//log.debug "DEPARTED: ${inputSensor.name}"
anyHaveDeparted = true
}
}
def allHaveDeparted = inputSensorsDepartingAnd && inputSensorsDepartingAnd.size() > 0
inputSensorsDepartingAnd.each { inputSensor ->
if (inputSensor.currentValue("presence") == "present") {
allHaveDeparted = false
}
}
newPresent = !(anyHaveDeparted || allHaveDeparted)
}
if (newPresent) {
outputSensor.arrived()
if (!oldPresent) {
log "${outputSensor.displayName}.arrived()"
if (notifyAboutStateChanges) {
sendNotification("Arrived: ${outputSensor.displayName}")
}
}
}
else {
outputSensor.departed()
if (oldPresent) {
log "${outputSensor.displayName}.departed()"
if (notifyAboutStateChanges) {
sendNotification("Departed: ${outputSensor.displayName}")
}
}
}
}
def getFormat(type, myText=""){
if(type == "header-green") return "<div style='color:#ffffff;font-weight: bold;background-color:#81BC00;border: 1px solid;box-shadow: 2px 3px #A9A9A9'>${myText}</div>"
if(type == "line") return "\n<hr style='background-color:#1A77C9; height: 1px; border: 0;'></hr>"
if(type == "title") return "<h2 style='color:#1A77C9;font-weight: bold'>${myText}</h2>"
}
def log(msg) {
if (enableLogging) {
log.debug msg
}
}