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.
19 namespace: "smartthings",
20 author: "SmartThings",
21 description: "Send a text when mail arrives in your mailbox using a SmartSense Multi on your mailbox door. Note: battery life may be impacted in cold climates.",
22 category: "Convenience",
23 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact.png",
24 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/mail_contact@2x.png"
28 section("When mail arrives...") {
29 input "accelerationSensor", "capability.accelerationSensor", title: "Where?"
31 section("Notify me...") {
32 input("recipients", "contact", title: "Send notifications to") {
33 input "pushNotification", "bool", title: "Push notification", required: false, defaultValue: "true"
34 input "phone1", "phone", title: "Phone number", required: false
40 subscribe(accelerationSensor, "acceleration.active", accelerationActiveHandler)
45 subscribe(accelerationSensor, "acceleration.active", accelerationActiveHandler)
48 def accelerationActiveHandler(evt) {
49 log.trace "$evt.value: $evt, $settings"
51 // Don't send a continuous stream of notifications
53 def timeAgo = new Date(now() - (1000 * deltaSeconds))
54 def recentEvents = accelerationSensor.eventsSince(timeAgo)
55 log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
56 def alreadySentNotifications = recentEvents.count { it.value && it.value == "active" } > 1
58 if (alreadySentNotifications) {
59 log.debug "Notifications already sent within the last $deltaSeconds seconds (phone1: $phone1, pushNotification: $pushNotification)"
62 if (location.contactBookEnabled) {
63 log.debug "$accelerationSensor has moved, notifying ${recipients?.size()}"
64 sendNotificationToContacts("Mail has arrived!", recipients)
67 if (phone1 != null && phone1 != "") {
68 log.debug "$accelerationSensor has moved, texting $phone1"
69 sendSms(phone1, "Mail has arrived!")
71 if (pushNotification) {
72 log.debug "$accelerationSensor has moved, sending push"
73 sendPush("Mail has arrived!")