2 * Copyright 2015 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
13 * Ridiculously Automated Garage Door
18 * Monitors arrival and departure of car(s) and
20 * 1) opens door when car arrives,
21 * 2) closes door after car has departed (for N minutes),
22 * 3) opens door when car door motion is detected,
23 * 4) closes door when door was opened due to arrival and interior door is closed.
27 name: "Ridiculously Automated Garage Door",
28 namespace: "smartthings",
29 author: "SmartThings",
30 description: "Monitors arrival and departure of car(s) and 1) opens door when car arrives, 2) closes door after car has departed (for N minutes), 3) opens door when car door motion is detected, 4) closes door when door was opened due to arrival and interior door is closed.",
31 category: "Convenience",
32 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact.png",
33 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/garage_contact@2x.png"
38 section("Garage door") {
39 input "doorSensor", "capability.contactSensor", title: "Which sensor?"
40 input "doorSwitch", "capability.momentary", title: "Which switch?"
41 input "openThreshold", "number", title: "Warn when open longer than (optional)",description: "Number of minutes", required: false
42 input("recipients", "contact", title: "Send notifications to") {
43 input "phone", "phone", title: "Warn with text message (optional)", description: "Phone Number", required: false
46 section("Car(s) using this garage door") {
47 input "cars", "capability.presenceSensor", title: "Presence sensor", description: "Which car(s)?", multiple: true, required: false
48 input "carDoorSensors", "capability.accelerationSensor", title: "Car door sensor(s)", description: "Which car(s)?", multiple: true, required: false
50 section("Interior door (optional)") {
51 input "interiorDoorSensor", "capability.contactSensor", title: "Contact sensor?", required: false
53 section("False alarm threshold (defaults to 10 min)") {
54 input "falseAlarmThreshold", "number", title: "Number of minutes", required: false
59 log.trace "installed()"
70 // log.debug "present: ${cars.collect{it.displayName + ': ' + it.currentPresence}}"
71 subscribe(doorSensor, "contact", garageDoorContact)
73 subscribe(cars, "presence", carPresence)
74 subscribe(carDoorSensors, "acceleration", accelerationActive)
76 if (interiorDoorSensor) {
77 subscribe(interiorDoorSensor, "contact.closed", interiorDoorClosed)
83 final thresholdMinutes = openThreshold
84 if (thresholdMinutes) {
85 def currentState = doorSensor.contactState
86 log.debug "doorOpenCheck"
87 if (currentState?.value == "open") {
88 log.debug "open for ${now() - currentState.date.time}, openDoorNotificationSent: ${state.openDoorNotificationSent}"
89 if (!state.openDoorNotificationSent && now() - currentState.date.time > thresholdMinutes * 60 *1000) {
90 def msg = "${doorSwitch.displayName} was been open for ${thresholdMinutes} minutes"
93 if (location.contactBookEnabled) {
94 sendNotificationToContacts(msg, recipients)
102 state.openDoorNotificationSent = true
106 state.openDoorNotificationSent = false
113 log.info "$evt.name: $evt.value"
114 // time in which there must be no "not present" events in order to open the door
115 final openDoorAwayInterval = falseAlarmThreshold ? falseAlarmThreshold * 60 : 600
117 if (evt.value == "present") {
120 def car = getCar(evt)
121 def t0 = new Date(now() - (openDoorAwayInterval * 1000))
122 def states = car.statesSince("presence", t0)
123 def recentNotPresentState = states.find{it.value == "not present"}
125 if (recentNotPresentState) {
126 log.debug "Not opening ${doorSwitch.displayName} since car was not present at ${recentNotPresentState.date}, less than ${openDoorAwayInterval} sec ago"
129 if (doorSensor.currentContact == "closed") {
131 sendPush "Opening garage door due to arrival of ${car.displayName}"
132 state.appOpenedDoor = now()
135 log.debug "door already open"
141 if (doorSensor.currentContact == "open") {
143 log.debug "Closing ${doorSwitch.displayName} after departure"
144 sendPush("Closing ${doorSwitch.displayName} after departure")
148 log.debug "Not closing ${doorSwitch.displayName} because its already closed"
153 def garageDoorContact(evt)
155 log.info "garageDoorContact, $evt.name: $evt.value"
156 if (evt.value == "open") {
157 schedule("0 * * * * ?", "doorOpenCheck")
160 unschedule("doorOpenCheck")
165 def interiorDoorClosed(evt)
167 log.info "interiorContact, $evt.name: $evt.value"
169 // time during which closing the interior door will shut the garage door, if the app opened it
170 final threshold = 15 * 60 * 1000
171 if (state.appOpenedDoor && now() - state.appOpenedDoor < threshold) {
172 state.appOpenedDoor = 0
176 log.debug "app didn't open door"
180 def accelerationActive(evt)
182 log.info "$evt.name: $evt.value"
184 if (doorSensor.currentContact == "closed") {
185 log.debug "opening door when car door opened"
192 if (doorSensor.currentContact == "closed") {
193 log.debug "opening door"
200 if (doorSensor.currentContact == "open") {
201 log.debug "closing door"
208 cars.find{it.id == evt.deviceId}