-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDoor Knocker + switch.groovy
104 lines (88 loc) · 2.73 KB
/
Door Knocker + switch.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
/**
* Door Knocker
*
* Author: [email protected]
* Date: 9/10/13
* Modified April 2021 by Mariano Colmenarejo
* Added can select turn on a switch, in order to command a voice action
* Let me know when someone knocks on the door, but ignore
* when someone is opening the door.
*/
definition(
name: "Door Knocker + switch",
namespace: "imbrianj",
author: "[email protected]",
description: "Alert if door is knocked, but not opened.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png"
)
preferences {
section("When Someone Knocks?") {
input name: "knockSensor", type: "capability.accelerationSensor", title: "Where?"
}
section("But not when they open this door?") {
input name: "openSensor", type: "capability.contactSensor", title: "Where?"
}
section("Knock Delay (defaults to 5s)?") {
input name: "knockDelay", type: "number", title: "How Long?", required: false
}
section("Select device to turn On") {
input name: "switchToOn", type: "capability.switch", title: "What?", required: false
}
section("Notifications") {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata: [values: ["Yes", "No"]], required: false
input "phone", "phone", title: "Send a Text Message?", required: false
}
}
def installed() {
init()
}
def updated() {
unsubscribe()
init()
}
def init() {
state.lastClosed = 0
subscribe(knockSensor, "acceleration.active", handleEvent)
subscribe(openSensor, "contact.closed", doorClosed)
}
def doorClosed(evt) {
state.lastClosed = now()
// turn off switch after door closed
if (switchToOn != null && switchToOn.currentValue("switch") == "on") {
log.debug "Door Closed & switch to off"
switchToOn.off()
}
}
def doorKnock() {
if((openSensor.latestValue("contact") == "closed") &&
(now() - (60 * 1000) > state.lastClosed)) {
log.debug("${knockSensor.label ?: knockSensor.name} detected a knock.")
// turn on the selected switch
if (switchToOn != null && switchToOn.currentValue("switch") == "off") {
log.debug "switch to on"
switchToOn.on()
}
//send message
send("${knockSensor.label ?: knockSensor.name} detected a knock.")
}
else {
log.debug("${knockSensor.label ?: knockSensor.name} knocked, but looks like it was just someone opening the door.")
}
}
def handleEvent(evt) {
def delay = knockDelay ?: 5
runIn(delay, "doorKnock")
}
private send(msg) {
if(sendPushMessage != "No") {
log.debug("Sending push message")
sendPush(msg)
}
if(phone) {
log.debug("Sending text message")
sendSms(phone, msg)
}
log.debug(msg)
}