-
Notifications
You must be signed in to change notification settings - Fork 7
/
Departure & Arrival-Arrival Child Multi-part message
644 lines (608 loc) · 30.8 KB
/
Departure & Arrival-Arrival Child Multi-part message
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/**
* **************** Arrivals Child App ****************
*
* Design Usage:
* Let the rest of the house know when one or more people have returned to the area.
*
* Copyright 2019 Bryan Turcotte (@bptworld)
*
* This App is free. If you like and use this app, please be sure to give a shout out on the Hubitat forums to let
* people know that it exists! Thanks.
*
* Remember...I am not a programmer, everything I do takes a lot of time and research!
* Donations are never necessary but always appreciated. Donations to support development efforts are accepted via:
*
* Paypal at: https://paypal.me/bptworld
*
* Unless noted in the code, ALL code contained within this app is mine. You are free to change, ripout, copy, modify or
* otherwise use the code in anyway you want. This is a hobby, I'm more than happy to share what I have learned and help
* the community grow. Have FUN with it!
*
*-------------------------------------------------------------------------------------------------------------------
* 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.
* ------------------------------------------------------------------------------------------------------------------------------
*
* If modifying this project, please keep the above header intact and add your comments/credits below - Thank you! - @BPTWorld
*
* App and Driver updates can be found at https://github.com/bptworld/Hubitat/
*
* ------------------------------------------------------------------------------------------------------------------------------
*
* Changes:
*
* V2.0.1 - 08/26/19 - Now messages have an 'opening' and a 'closing' segment.
* V2.0.0 - 08/18/19 - Now App Watchdog compliant
* V1.0.0 - 03/15/19 - Initial release.
*
*/
import groovy.time.TimeCategory
def setVersion(){
// * V2.0.0 - 08/18/19 - Now App Watchdog compliant
if(logEnable) log.debug "In setVersion - App Watchdog Child app code"
// Must match the exact name used in the json file. ie. AppWatchdogParentVersion, AppWatchdogChildVersion or AppWatchdogDriverVersion
state.appName = "ArrivalsChildVersion"
state.version = "v2.0.0"
try {
if(parent.sendToAWSwitch && parent.awDevice) {
awInfo = "${state.appName}:${state.version}"
parent.awDevice.sendAWinfoMap(awInfo)
if(logEnable) log.debug "In setVersion - Info was sent to App Watchdog"
schedule("0 0 3 ? * * *", setVersion)
}
} catch (e) { log.error "In setVersion - ${e}" }
}
definition(
name: "Arrivals Child",
namespace: "BPTWorld",
author: "Bryan Turcotte",
description: "Let the rest of the house know when one or more people have returned to the area.",
category: "",
parent: "BPTWorld:Departures and Arrivals",
iconUrl: "",
iconX2Url: "",
iconX3Url: "",
importUrl: "https://raw.githubusercontent.com/bptworld/Hubitat/master/Apps/Departures/D-child.groovy",
)
preferences {
page(name: "pageConfig")
}
def pageConfig() {
dynamicPage(name: "", title: "<h2 style='color:#1A77C9;font-weight: bold'>Arrivals</h2>", install: true, uninstall: true, refreshInterval:0) {
display()
section("Instructions:", hideable: true, hidden: true) {
paragraph "<b>Notes:</b>"
paragraph "When a sensor becomes 'present', it will wait X minutes and then check to see who else just left before making the announcement."
paragraph "<b>Requirements:</b>"
paragraph "Be sure to enter in the Preset Values in Advanced Config before creating Child Apps."
}
section(getFormat("header-green", "${getImage("Blank")}"+" Presence Options")) {
paragraph "Note: If you are not seeing your 'Friendly Names', then go back to the parent app, enter them in and hit 'done' before setting up any child apps."
input(name: "presenceSensor1a", type: "bool", defaultValue: "false", title: "Use ${parent.friendlyName1}", description: "PS1", submitOnChange: true)
if(presenceSensor1a) input("presenceSensor1", "capability.presenceSensor", title: "Match a Presence Sensor to the Friendly Name", required: true, multiple: false)
input(name: "presenceSensor2a", type: "bool", defaultValue: "false", title: "Use ${parent.friendlyName2}", description: "PS2", submitOnChange: true)
if(presenceSensor2a) input("presenceSensor2", "capability.presenceSensor", title: "Match a Presence Sensor to the Friendly Name", required: true, multiple: false)
input(name: "presenceSensor3a", type: "bool", defaultValue: "false", title: "Use ${parent.friendlyName3}", description: "PS3", submitOnChange: true)
if(presenceSensor3a) input("presenceSensor3", "capability.presenceSensor", title: "Match a Presence Sensor to the Friendly Name", required: true, multiple: false)
input(name: "presenceSensor4a", type: "bool", defaultValue: "false", title: "Use ${parent.friendlyName4}", description: "PS4", submitOnChange: true)
if(presenceSensor4a) input("presenceSensor4", "capability.presenceSensor", title: "Match a Presence Sensor to the Friendly Name", required: true, multiple: false)
input(name: "presenceSensor5a", type: "bool", defaultValue: "false", title: "Use ${parent.friendlyName5}", description: "PS5", submitOnChange: true)
if(presenceSensor5a) input("presenceSensor5", "capability.presenceSensor", title: "Match a Presence Sensor to the Friendly Name", required: true, multiple: false)
}
section(getFormat("header-green", "${getImage("Blank")}"+" Speech Options")) {
input "speechMode", "enum", required: true, title: "Select Speaker Type", submitOnChange: true, options: ["Music Player", "Speech Synth"]
if (speechMode == "Music Player"){
input "speaker", "capability.musicPlayer", title: "Choose speaker(s)", required: true, multiple: true, submitOnChange: true
paragraph "<hr>"
paragraph "If you are using the 'Echo Speaks' app with your Echo devices then turn this option ON.<br>If you are NOT using the 'Echo Speaks' app then please leave it OFF."
input(name: "echoSpeaks", type: "bool", defaultValue: "false", title: "Is this an 'echo speaks' app device?", description: "Echo speaks device", submitOnChange: true)
if(echoSpeaks) input "restoreVolume", "number", title: "Volume to restore speaker to AFTER anouncement", description: "0-100%", required: true, defaultValue: "30"
}
if (speechMode == "Speech Synth"){
input "speaker", "capability.speechSynthesis", title: "Choose speaker(s)", required: true, multiple: true
}
}
section(getFormat("header-green", "${getImage("Blank")}"+" Volume Control Options")) {
paragraph "NOTE: Not all speakers can use volume controls."
input "volSpeech", "number", title: "Speaker volume for speech", description: "0-100", required: true
input "volRestore", "number", title: "Restore speaker volume to X after speech", description: "0-100", required: true
input "volQuiet", "number", title: "Quiet Time Speaker volume", description: "0-100", required: false, submitOnChange: true
if(volQuiet) input "QfromTime", "time", title: "Quiet Time Start", required: true
if(volQuiet) input "QtoTime", "time", title: "Quiet Time End", required: true
}
if(speechMode){
section(getFormat("header-green", "${getImage("Blank")}"+" Allow messages between what times? (Optional)")) {
input "fromTime", "time", title: "From", required: false
input "toTime", "time", title: "To", required: false
}
}
section(getFormat("header-green", "${getImage("Blank")}"+" Message Options")) {
input(name: "oRandomG1", type: "bool", defaultValue: "false", title: "Random Time of day greeting 1?", description: "Random", submitOnChange: "true")
if(!oRandomG1) input "greeting1", "text", required: true, title: "Time of day greeting - 1 (am) - Single message", defaultValue: "Good Morning"
if(oRandomG1) {
input "greeting1", "text", title: "Time of day greeting - 1 (am) - Separate each message with <b>;</b> (semicolon)", required: true, submitOnChange: "true"
input(name: "oG1List", type: "bool", defaultValue: "false", title: "Show a list view of Time of day greeting 1?", description: "List View", submitOnChange: "true")
if(oG1List) {
def valuesG1 = "${greeting1}".split(";")
listMapG1 = ""
valuesG1.each { itemG1 -> listMapG1 += "${itemG1}<br>" }
paragraph "${listMapG1}"
}
}
paragraph "<hr>"
input(name: "oRandomG2", type: "bool", defaultValue: "false", title: "Random Time of day greeting 2?", description: "Random", submitOnChange: "true")
if(!oRandomG2) input "greeting2", "text", required: true, title: "Time of day greeting - 2 (pm before 6) - Single message", defaultValue: "Good Afternoon"
if(oRandomG2) {
input "greeting2", "text", title: "Time of day greeting - 2 (pm before 6) - Separate each message with <b>;</b> (semicolon)", required: true, submitOnChange: "true"
input(name: "oG2List", type: "bool", defaultValue: "false", title: "Show a list view of the Time of day greeting 2?", description: "List View", submitOnChange: "true")
if(oG2List) {
def valuesG2 = "${greeting2}".split(";")
listMapG2 = ""
valuesG2.each { itemG2 -> listMapG2 += "${itemG2}<br>" }
paragraph "${listMapG2}"
}
}
paragraph "<hr>"
input(name: "oRandomG3", type: "bool", defaultValue: "false", title: "Random Time of day greeting 3?", description: "Random", submitOnChange: "true")
if(!oRandomG3) input "greeting3", "text", required: true, title: "Time of day greeting - 3 (pm after 6) - Single message", defaultValue: "Good Evening"
if(oRandomG3) {
input "greeting3", "text", title: "Time of day greeting - 3 (pm after 6) - Separate each message with <b>;</b> (semicolon)", required: true, submitOnChange: "true"
input(name: "oG3List", type: "bool", defaultValue: "false", title: "Show a list view of the Time of day greeting 3?", description: "List View", submitOnChange: "true")
if(oG3List) {
def valuesG3 = "${greeting3}".split(";")
listMapG3 = ""
valuesG3.each { itemG3 -> listMapG3 += "${itemG3}<br>" }
paragraph "${listMapG3}"
}
}
paragraph "<hr>"
input(name: "oRandom", type: "bool", defaultValue: "false", title: "Random Opening and Closing Messages?", description: "Random", submitOnChange: "true")
paragraph "<u>Optional wildcards:</u><br>%greeting% - returns a greeting based on time of day.<br>%name% - returns the Friendly Name associcated with a Presence Sensor<br>%is_are% - returns 'is' or 'are' depending on number of sensors<br>%has_have% - returns 'has' or 'have' depending on number of sensors<br>Message constructed as 'Opening message' + %name% + 'is_are' + 'Closing message'"
if(!oRandom) input "omessage", "text", title: "Opening message to be spoken - Single message", required: true
if(!oRandom) input "cmessage", "text", title: "Closing message to be spoken - Single message", required: true
if(oRandom) {
input "omessage", "text", title: "Opening message to be spoken - Separate each message with <b>;</b> (semicolon)", required: true, submitOnChange: true
input(name: "oMsgList", type: "bool", defaultValue: "true", title: "Show a list view of the opening messages?", description: "List View", submitOnChange: "true")
if(oMsgList) {
def ovalues = "${omessage}".split(";")
olistMap = ""
ovalues.each { item -> olistMap += "${item}<br>"}
paragraph "${olistMap}"
}
input "cmessage", "text", title: "Closing message to be spoken - Separate each message with <b>;</b> (semicolon)", required: true, submitOnChange: true
input(name: "cMsgList", type: "bool", defaultValue: "true", title: "Show a list view of the closing messages?", description: "List View", submitOnChange: "true")
if(cMsgList) {
def cvalues = "${cmessage}".split(";")
clistMap = ""
cvalues.each { item -> clistMap += "${item}<br>"}
paragraph "${clistMap}"
}
}
}
section() {
paragraph "This next option is so the app can pickup if any other sensors become 'present' around the same time."
input "delay1", "number", title: "How many minutes from the time a trigger being activated to the announcement being made (default=1)", required: true, defaultValue: 1
}
section(getFormat("header-green", "${getImage("Blank")}"+" General")) {label title: "Enter a name for this automation", required: false}
section() {
input(name: "logEnable", type: "bool", defaultValue: "true", title: "Enable Debug Logging", description: "Enable extra logging for debugging.")
}
display2()
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
if(logEnable) log.debug "Updated with settings: ${settings}"
unsubscribe()
unschedule()
initialize()
}
def initialize() {
setDefaults()
subscribe(presenceSensor1, "presence", checkAllHandler)
subscribe(presenceSensor2, "presence", checkAllHandler)
subscribe(presenceSensor3, "presence", checkAllHandler)
subscribe(presenceSensor4, "presence", checkAllHandler)
subscribe(presenceSensor5, "presence", checkAllHandler)
}
def checkAllHandler(evt) {
if(logEnable) log.debug "In checkAllHandler..."
state.presenceMap = ""
state.nameCount = 0
if(presenceSensor1) presenceSensorHandler1()
if(presenceSensor2) presenceSensorHandler2()
if(presenceSensor3) presenceSensorHandler3()
if(presenceSensor4) presenceSensorHandler4()
if(presenceSensor5) presenceSensorHandler5()
if(logEnable) log.debug "In checkAllHandler - Waiting to talk - ${state.canSpeak}"
delay1m = delay1 * 60
if(state.canSpeak == "yes" && state.nameCount >= 1) {
runIn(delay1m,letsTalk)
} else {
log.info "${app.label} - No need for an announcement."
}
}
def setupNewStuff() {
if(logEnable) log.debug "In setupNewStuff...Setting up Maps"
if(state.presenceMap == null) state.presenceMap = [:]
}
def presenceSensorHandler1() {
state.presenceSensorValue1 = presenceSensor1.currentValue("presence")
if(logEnable) log.debug "In presenceSensorHandler1 - Presence Sensor: ${state.presenceSensorValue1}"
if(state.presenceSensorValue1 == "present"){
if(logEnable) log.debug "In presenceSensorHandler1 - Presence Sensor is present"
def lastActivity1 = presenceSensor1.getLastActivity()
if(logEnable) log.debug "In presenceSensorHandler1 - lastActivity: ${lastActivity1}"
long timeDiff
def now = new Date()
def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity1}".replace("+00:00","+0000"))
long unxNow = now.getTime()
long unxPrev = prev.getTime()
unxNow = unxNow/1000
unxPrev = unxPrev/1000
timeDiff = Math.abs(unxNow-unxPrev)
timeDiff = Math.round(timeDiff/60)
if(logEnable) log.debug "In presenceSensorHandler1 - PS1 has been home for ${timeDiff} minutes"
if(timeDiff < delay1) {
log.info "${app.label} - ${parent.friendlyName1} just left! Time Diff: ${timeDiff}"
state.nameCount = state.nameCount + 1
if(state.nameCount == 1) state.presenceMap = [parent.friendlyName1]
if(state.nameCount >= 2) state.presenceMap += [parent.friendlyName1]
if(logEnable) log.debug "In presenceSensorHandler1 - ${state.presenceMap}"
} else {
log.info "${app.label} - ${parent.friendlyName1} has been here too long. No announcement needed."
}
} else {
if(logEnable) log.debug "In presenceSensorHandler1 - Presence Sensor is not present. No announcement needed."
state.canSpeak = "yes"
}
}
def presenceSensorHandler2() {
state.presenceSensorValue2 = presenceSensor2.currentValue("presence")
if(logEnable) log.debug "In presenceSensorHandler2 - Presence Sensor: ${state.presenceSensorValue2}"
if(state.presenceSensorValue2 == "present"){
if(logEnable) log.debug "In presenceSensorHandler2 - Presence Sensor is present"
def lastActivity2 = presenceSensor2.getLastActivity()
if(logEnable) log.debug "In presenceSensorHandler2 - lastActivity: ${lastActivity2}"
long timeDiff
def now = new Date()
def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity2}".replace("+00:00","+0000"))
long unxNow = now.getTime()
long unxPrev = prev.getTime()
unxNow = unxNow/1000
unxPrev = unxPrev/1000
timeDiff = Math.abs(unxNow-unxPrev)
timeDiff = Math.round(timeDiff/60)
if(logEnable) log.debug "In presenceSensorHandler2 - PS1 has been here for ${timeDiff} minutes"
if(timeDiff < delay1) {
log.info "${app.label} - ${parent.friendlyName2} just left! Time Diff: ${timeDiff}"
state.nameCount = state.nameCount + 1
if(state.nameCount == 1) state.presenceMap = [parent.friendlyName2]
if(state.nameCount >= 2) state.presenceMap += [parent.friendlyName2]
if(logEnable) log.debug "In presenceSensorHandler2 - ${state.presenceMap}"
} else {
log.info "${app.label} - ${parent.friendlyName2} has been here too long. No announcement needed."
}
} else {
if(logEnable) log.debug "In presenceSensorHandler2 - Presence Sensor is not present. No announcement needed."
state.canSpeak = "yes"
}
}
def presenceSensorHandler3() {
state.presenceSensorValue3 = presenceSensor3.currentValue("presence")
if(logEnable) log.debug "In presenceSensorHandler3 - Presence Sensor: ${state.presenceSensorValue3}"
if(state.presenceSensorValue3 == "present"){
if(logEnable) log.debug "In presenceSensorHandler3 - Presence Sensor is present"
def lastActivity3 = presenceSensor3.getLastActivity()
if(logEnable) log.debug "In presenceSensorHandler3 - lastActivity: ${lastActivity3}"
long timeDiff
def now = new Date()
def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity3}".replace("+00:00","+0000"))
long unxNow = now.getTime()
long unxPrev = prev.getTime()
unxNow = unxNow/1000
unxPrev = unxPrev/1000
timeDiff = Math.abs(unxNow-unxPrev)
timeDiff = Math.round(timeDiff/60)
if(logEnable) log.debug "In presenceSensorHandler3 - PS3 has been here for ${timeDiff} minutes"
if(timeDiff < delay1) {
log.info "${app.label} - ${parent.friendlyName3} just left! Time Diff: ${timeDiff}"
state.nameCount = state.nameCount + 1
if(state.nameCount == 1) state.presenceMap = [parent.friendlyName3]
if(state.nameCount >= 2) state.presenceMap += [parent.friendlyName3]
if(logEnable) log.debug "In presenceSensorHandler3 - ${state.presenceMap}"
} else {
log.info "${app.label} - ${parent.friendlyName3} has been here too long. No announcement needed."
}
} else {
if(logEnable) log.debug "In presenceSensorHandler3 - Presence Sensor is not present. No announcement needed."
state.canSpeak = "yes"
}
}
def presenceSensorHandler4() {
state.presenceSensorValue4 = presenceSensor4.currentValue("presence")
if(logEnable) log.debug "In presenceSensorHandler4 - Presence Sensor: ${state.presenceSensorValue4}"
if(state.presenceSensorValue4 == "present"){
if(logEnable) log.debug "In presenceSensorHandler4 - Presence Sensor is present"
def lastActivity4 = presenceSensor4.getLastActivity()
if(logEnable) log.debug "In presenceSensorHandler4 - lastActivity: ${lastActivity4}"
long timeDiff
def now = new Date()
def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity4}".replace("+00:00","+0000"))
long unxNow = now.getTime()
long unxPrev = prev.getTime()
unxNow = unxNow/1000
unxPrev = unxPrev/1000
timeDiff = Math.abs(unxNow-unxPrev)
timeDiff = Math.round(timeDiff/60)
if(logEnable) log.debug "In presenceSensorHandler4 - PS4 has been here for ${timeDiff} minutes"
if(timeDiff < delay1) {
log.info "${app.label} - ${parent.friendlyName4} just left! Time Diff: ${timeDiff}"
state.nameCount = state.nameCount + 1
if(state.nameCount == 1) state.presenceMap = [parent.friendlyName4]
if(state.nameCount >= 2) state.presenceMap += [parent.friendlyName4]
if(logEnable) log.debug "In presenceSensorHandler4 - ${state.presenceMap}"
} else {
log.info "${app.label} - ${parent.friendlyName4} has been here too long. No announcement needed."
}
} else {
if(logEnable) log.debug "In presenceSensorHandler4 - Presence Sensor is not present. No announcement needed."
state.canSpeak = "yes"
}
}
def presenceSensorHandler5() {
state.presenceSensorValue5 = presenceSensor5.currentValue("presence")
if(logEnable) log.debug "In presenceSensorHandler5 - Presence Sensor: ${state.presenceSensorValue5}"
if(state.presenceSensorValue5 == "present"){
if(logEnable) log.debug "In presenceSensorHandler5 - Presence Sensor is present"
def lastActivity5 = presenceSensor5.getLastActivity()
if(logEnable) log.debug "In presenceSensorHandler5 - lastActivity: ${lastActivity5}"
long timeDiff
def now = new Date()
def prev = Date.parse("yyy-MM-dd HH:mm:ss","${lastActivity5}".replace("+00:00","+0000"))
long unxNow = now.getTime()
long unxPrev = prev.getTime()
unxNow = unxNow/1000
unxPrev = unxPrev/1000
timeDiff = Math.abs(unxNow-unxPrev)
timeDiff = Math.round(timeDiff/60)
if(logEnable) log.debug "In presenceSensorHandler5 - PS5 has been here for ${timeDiff} minutes"
if(timeDiff < delay1) {
log.info "${app.label} - ${parent.friendlyName5} just left! Time Diff: ${timeDiff}"
state.nameCount = state.nameCount + 1
if(state.nameCount == 1) state.presenceMap = [parent.friendlyName5]
if(state.nameCount >= 2) state.presenceMap += [parent.friendlyName5]
if(logEnable) log.debug "In presenceSensorHandler5 - ${state.presenceMap}"
} else {
log.info "${app.label} - ${parent.friendlyName5} has been here too long. No announcement needed."
}
} else {
if(logEnable) log.debug "In presenceSensorHandler5 - Presence Sensor is not present. No announcement needed."
state.canSpeak = "yes"
}
}
def letsTalk() {
if(logEnable) log.debug "In letsTalk..."
checkTime()
checkVol()
atomicState.randomPause = Math.abs(new Random().nextInt() % 1500) + 400
if(logEnable) log.debug "In letsTalk - pause: ${atomicState.randomPause}"
pauseExecution(atomicState.randomPause)
if(logEnable) log.debug "In letsTalk - continuing"
if(state.timeBetween == true) {
messageHandler()
if(logEnable) log.debug "Speaker in use: ${speaker}"
state.theMsg = "${state.theMessage}"
if(logEnable) log.debug "In letsTalk - Waiting ${delay1} seconds to Speak"
def delay1ms = delay1 * 1000
pauseExecution(delay1ms)
if (speechMode == "Music Player"){
if(logEnable) log.debug "In letsTalk - Music Player - speaker: ${speaker}, vol: ${state.volume}, msg: ${state.theMsg}"
if(echoSpeaks) {
speaker.setVolumeSpeakAndRestore(state.volume, state.theMsg, volRestore)
state.canSpeak = "no"
if(logEnable) log.debug "In letsTalk - Wow, that's it!"
}
if(!echoSpeaks) {
if(volSpeech) speaker.setLevel(state.volume)
speaker.playTextAndRestore(state.theMsg, volRestore)
state.canSpeak = "no"
if(logEnable) log.debug "In letsTalk - Wow, that's it!"
}
}
if(speechMode == "Speech Synth"){
speechDuration = Math.max(Math.round(state.theMsg.length()/12),2)+3 // Code from @djgutheinz
atomicState.speechDuration2 = speechDuration * 1000
if(logEnable) log.debug "In letsTalk - Speech Synth - speaker: ${speaker}, vol: ${state.volume}, msg: ${state.theMsg}"
if(volSpeech) speaker.setVolume(state.volume)
speaker.speak(state.theMsg)
pauseExecution(atomicState.speechDuration2)
if(volRestore) speaker.setVolume(volRestore)
state.canSpeak = "no"
if(logEnable) log.debug "In letsTalk - Wow, that's it!"
}
log.info "${app.label} - ${state.theMsg}"
} else {
if(logEnable) log.debug "In letsTalk - Messages not allowed at this time"
}
}
def checkVol(){
if(logEnable) log.debug "In checkVol..."
if(QfromTime) {
state.quietTime = timeOfDayIsBetween(toDateTime(QfromTime), toDateTime(QtoTime), new Date(), location.timeZone)
if(logEnable) log.debug "In checkVol - quietTime: ${state.quietTime}"
if(state.quietTime) state.volume = volQuiet
if(!state.quietTime) state.volume = volSpeech
} else {
state.volume = volSpeech
}
if(logEnable) log.debug "In checkVol - volume: ${state.volume}"
}
def checkTime() {
if(logEnable) log.debug "In checkTime - ${fromTime} - ${toTime}"
if((fromTime != null) && (toTime != null)) {
state.betweenTime = timeOfDayIsBetween(toDateTime(fromTime), toDateTime(toTime), new Date(), location.timeZone)
if(state.betweenTime) state.timeBetween = true
if(!state.betweenTime) state.timeBetween = false
} else {
state.timeBetween = true
}
if(logEnable) log.debug "In checkTime - timeBetween: ${state.timeBetween}"
}
def messageHandler() {
if(logEnable) log.debug "In messageHandler..."
if(oRandom) {
def ovalues = "${omessage}".split(";")
ovSize = ovalues.size()
ocount = ovSize.toInteger()
def orandomKey = new Random().nextInt(ocount)
def cvalues = "${cmessage}".split(";")
cvSize = cvalues.size()
ccount = cvSize.toInteger()
def crandomKey = new Random().nextInt(ccount)
theMessage = ovalues[orandomKey] + ' %name%. %name% %is_are% ' + cvalues[crandomKey]
if(logEnable) log.debug "In messageHandler - Random - ovSize: ${ovSize}, orandomKey: ${orandomKey}; Random - cvSize: ${cvSize}, crandomKey: ${crandomKey}, theMessage: ${theMessage}"
} else {
theMessage = "${omessage} %name%. %name% %is_are% ${cmessage}"
if(logEnable) log.debug "In messageHandler - Static - theMessage: ${theMessage}"
}
theMessage = theMessage.toLowerCase()
if (theMessage.toLowerCase().contains("%greeting%")) {theMessage = theMessage.toLowerCase().replace('%greeting%', getGreeting() )}
if (theMessage.toLowerCase().contains("%name%")) {theMessage = theMessage.toLowerCase().replace('%name%', getName() )}
if (theMessage.toLowerCase().contains("%is_are%")) {theMessage = theMessage.toLowerCase().replace('%is_are%', "${is_are}" )}
if (theMessage.toLowerCase().contains("%has_have%")) {theMessage = theMessage.toLowerCase().replace('%has_have%', "${has_have}" )}
state.theMessage = "${theMessage}"
return state.theMessage
}
private getName(){
if(logEnable) log.debug "In getName..."
if(logEnable) log.debug "In getName...Number of Names: ${state.nameCount} - Names: ${state.presenceMap}"
name = ""
myCount = 0
if(state.nameCount == 1) {
state.presenceMap.each { it ->
if(logEnable) log.debug "*********** In nameCount=1: myCount = ${myCount}"
name = "${it.value}"
}
}
if(state.nameCount == 2) {
state.presenceMap.each { it ->
if(logEnable) log.debug "*********** In nameCount=2: myCount = ${myCount}"
myCount = myCount + 1
name = "${name}" + "${it.value} "
if(myCount == 1) name = "${name}" + "and "
}
}
if(state.nameCount == 3) {
state.presenceMap.each { it ->
if(logEnable) log.debug "*********** In nameCount=3: myCount = ${myCount}"
myCount = myCount + 1
name = "${name}" + "${it.value}, "
if(myCount == 2) name = "${name}" + "and "
}
}
if(state.nameCount == 4) {
state.presenceMap.each { it ->
if(logEnable) log.debug "*********** In nameCount=4: myCount = ${myCount}"
myCount = myCount + 1
name = "${name}" + "${it.value}, "
if(myCount == 3) name = "${name}" + "and "
}
}
if(state.nameCount == 5) {
state.presenceMap.each { it ->
if(logEnable) log.debug "*********** In nameCount=5: myCount = ${myCount}"
myCount = myCount + 1
name = "${name}" + "${it.value}, "
if(myCount == 4) name = "${name}" + "and "
}
}
is_are = (name.contains(' and ') ? 'are' : 'is')
has_have = (name.contains(' and ') ? 'have' : 'has')
if(name == null) names = "Whoever you are"
if(name == "") names = "Whoever you are"
if(logEnable) log.debug "AGAIN...Name = ${name}"
return name
}
private getGreeting(){ // Heavily Modified from @Cobra Code
if(logEnable) log.debug "In getGreeting..."
def calendar = Calendar.getInstance()
calendar.setTimeZone(location.timeZone)
def timeHH = calendar.get(Calendar.HOUR) toInteger()
def timeampm = calendar.get(Calendar.AM_PM) ? "pm" : "am"
if(logEnable) log.debug "timeHH = $timeHH"
if(timeampm == 'am'){
if(oRandomG1) {
def values = "${greeting1}".split(";")
vSize = values.size()
count = vSize.toInteger()
def randomKey = new Random().nextInt(count)
state.greeting = values[randomKey]
if(logEnable) log.debug "In getGreeting - Random - vSize: ${vSize}, randomKey: ${randomKey}, greeting: ${state.greeting} timeampm = ${timeampm} - timehh = ${timeHH}"
} else {
state.greeting = "${greeting1}"
if(logEnable) log.debug "In getGreeting - Static - greeting: ${state.greeting}"
}
}
else if(timeampm == 'pm' && timeHH < 6){
if(oRandomG2) {
def values = "${greeting2}".split(";")
vSize = values.size()
count = vSize.toInteger()
def randomKey = new Random().nextInt(count)
state.greeting = values[randomKey]
if(logEnable) log.debug "In getGreeting - Random - vSize: ${vSize}, randomKey: ${randomKey}, greeting: ${state.greeting} timeampm = ${timeampm} - timehh = ${timeHH}"
} else {
state.greeting = "${greeting2}"
if(logEnable) log.debug "In getGreeting - Static - greeting: ${state.greeting}"
}
}
else if(timeampm == 'pm' && timeHH >= 6){
if(oRandomG3) {
def values = "${greeting3}".split(";")
vSize = values.size()
count = vSize.toInteger()
def randomKey = new Random().nextInt(count)
state.greeting = values[randomKey]
if(logEnable) log.debug "In getGreeting - Random - vSize: ${vSize}, randomKey: ${randomKey}, greeting: ${state.greeting} timeampm = ${timeampm} - timehh = ${timeHH}"
} else {
state.greeting = "${greeting3}"
if(logEnable) log.debug "In getGreeting - Static - greeting: ${state.greeting}"
}
}
return state.greeting
}
// ********** Normal Stuff **********
def setDefaults(){
setupNewStuff()
state.nameCount = 0
state.canSpeak = "no"
}
def getImage(type) { // Modified from @Stephack
def loc = "<img src=https://raw.githubusercontent.com/bptworld/Hubitat/master/resources/images/"
if(type == "Blank") return "${loc}blank.png height=40 width=5}>"
}
def getFormat(type, myText=""){ // Modified from @Stephack
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 "<div style='color:blue;font-weight: bold'>${myText}</div>"
}
def display() {
section() {
paragraph getFormat("line")
}
}
def display2(){
setVersion()
section() {
paragraph getFormat("line")
paragraph "<div style='color:#1A77C9;text-align:center'>Arrivals - @BPTWorld<br><a href='https://github.com/bptworld/Hubitat' target='_blank'>Find more apps on my Github, just click here!</a><br>Get app update notifications and more with <a href='https://github.com/bptworld/Hubitat/tree/master/Apps/App%20Watchdog' target='_blank'>App Watchdog</a><br>${state.version}</div>"
}
}