4 * Copyright 2014 Arnaud
6 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7 * in compliance with the License. You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13 * for the specific language governing permissions and limitations under the License.
17 name: "Smart Auto Lock / Unlock",
18 namespace: "smart-auto-lock-unlock",
20 description: "Automatically locks door X minutes after being closed and keeps door unlocked if door is open.",
21 category: "Safety & Security",
22 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
23 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")
27 section("Select the door lock:") {
28 input "lock1", "capability.lock", required: true
30 section("Select the door contact sensor:") {
31 input "contact1", "capability.contactSensor", required: true
33 section("Automatically lock the door when closed...") {
34 input "minutesLater", "number", title: "Delay (in minutes):", required: true
36 section("Automatically unlock the door when open...") {
37 input "secondsLater", "number", title: "Delay (in seconds):", required: true
39 section( "Push notification?" ) {
40 input "sendPushMessage", "enum", title: "Send push notification?", metadata:[values:["Yes", "No"]], required: false
42 section( "Text message?" ) {
43 input "sendText", "enum", title: "Send text message notification?", metadata:[values:["Yes", "No"]], required: false
44 input "phoneNumber", "phone", title: "Enter phone number:", required: false
62 log.debug "Settings: ${settings}"
63 subscribe(lock1, "lock", doorHandler, [filterEvents: false])
64 subscribe(lock1, "unlock", doorHandler, [filterEvents: false])
65 subscribe(contact1, "contact.open", doorHandler)
66 subscribe(contact1, "contact.closed", doorHandler)
71 if (lock1.latestValue("lock") == "unlocked")
73 log.debug "Locking $lock1..."
75 log.debug ("Sending Push Notification...")
76 if (sendPushMessage != "No") sendPush("$lock1 locked after $contact1 was closed for $minutesLater minute(s)!")
77 log.debug("Sending text message...")
78 if ((sendText == "Yes") && (phoneNumber != "0")) sendSms(phoneNumber, "$lock1 locked after $contact1 was closed for $minutesLater minute(s)!")
80 else if (lock1.latestValue("lock") == "locked")
82 log.debug "$lock1 was already locked..."
88 if (lock1.latestValue("lock") == "locked")
90 log.debug "Unlocking $lock1..."
92 log.debug ("Sending Push Notification...")
93 if (sendPushMessage != "No") sendPush("$lock1 unlocked after $contact1 was open for $secondsLater seconds(s)!")
94 log.debug("Sending text message...")
95 if ((sendText == "Yes") && (phoneNumber != "0")) sendSms(phoneNumber, "$lock1 unlocked after $contact1 was open for $secondsLater seconds(s)!")
97 else if (lock1.latestValue("lock") == "unlocked")
99 log.debug "$lock1 was already unlocked..."
105 if ((contact1.latestValue("contact") == "open") && (evt.value == "locked"))
107 def delay = secondsLater
108 runIn (delay, unlockDoor)
110 else if ((contact1.latestValue("contact") == "open") && (evt.value == "unlocked"))
112 unschedule (unlockDoor)
114 else if ((contact1.latestValue("contact") == "closed") && (evt.value == "locked"))
116 unschedule (lockDoor)
118 else if ((contact1.latestValue("contact") == "closed") && (evt.value == "unlocked"))
120 log.debug "Unlocking $lock1..."
122 def delay = (minutesLater * 60)
123 runIn (delay, lockDoor)
125 else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "open"))
127 unschedule (lockDoor)
128 log.debug "Unlocking $lock1..."
131 else if ((lock1.latestValue("lock") == "unlocked") && (evt.value == "closed"))
133 log.debug "Unlocking $lock1..."
135 def delay = (minutesLater * 60)
136 runIn (delay, lockDoor)
138 else if ((lock1.latestValue("lock") == "locked") && (evt.value == "open"))
140 unschedule (lockDoor)
141 log.debug "Unlocking $lock1..."
144 else if ((lock1.latestValue("lock") == "locked") && (evt.value == "closed"))
146 unschedule (lockDoor)
147 log.debug "Unlocking $lock1..."
152 log.debug "Problem with $lock1, the lock might be jammed!"
153 unschedule (lockDoor)
154 unschedule (unlockDoor)