-
Notifications
You must be signed in to change notification settings - Fork 0
/
motion-and-nobodys-home.groovy
106 lines (88 loc) · 2.14 KB
/
motion-and-nobodys-home.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
/*
* Motion detected and everybody is gone
*
* Author: [email protected]
* Date: 2013-03-22
* Version: 0.0.2
*/
def preferences() {[
sections: [
[
title: "When motion is detected...",
input: [
[
name: "motion",
title: "Where?",
type: "capability.motionSensor",
description: "Tap to set",
multiple: false
]
]
],
[
title: "And everybody is away...",
input: [
[
name: "presences",
title: "Which sensor(s)?",
type: "capability.presenceSensor",
description: "Tap to set",
multiple: true
]
]
]
]
]}
def installed() {
log.trace "Installed with settings: ${settings}"
subscribe(motion.motion)
// handle the list?
presences.each { sensor ->
subscribe(sensor.presence)
}
}
def updated() {
log.trace "Updated with settings: ${settings}"
unsubscribe()
subscribe(motion.motion)
presences.each { sensor ->
subscribe(sensor.presence)
}
}
def shouldNotify(sensor) {
log.trace "should notify? ... "
def recentEvents = sensor.eventsSince(new Date(now() - 1000))
def alreadySent = recentEvents.count { it.value && it.value == "active" } > 1
!alreadySent
}
def present(sensor){
sensor.latestValue == 'present'
}
def notPresent(sensor){
sensor.latestValue == 'not present'
}
def allNotPresent(sensors){
log.info("All Not Present: ${sensors}")
sensors.every { notPresent(it) }
}
def motion(evt) {
log.trace "${motion.name} evt.value: ${evt.value}"
if (evt.value == 'active'){
log.trace("Motion detected")
if (shouldNotify(motion) && allNotPresent(presences)) {
log.info " *** ${ presences.collect{ it.name }.join(', ') } not pesent! *** "
notifyMe "Motion detected by ${motion.label ?: motion.name} and nobody is home! ${new Date(now())}"
}
}
}
def notifyMe(message){
log.info "Notify Me: ${message}"
sendPush(message)
//sendSms(phoneNumber, message)
sendSms('6122076622', message)
//sendSms('6127309391', message)
}
def presence(evt) {
log.trace "Presence evt.value: ${evt.value}"
//noop
}