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 * Text Me When There's Motion and I'm Not Here
19 name: "Text Me When There's Motion and I'm Not Here",
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Send a text message when there is motion while you are away.",
23 category: "Convenience",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/intruder_motion-presence.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/intruder_motion-presence@2x.png"
29 section("When there's movement...") {
30 input "motion1", "capability.motionSensor", title: "Where?"
32 section("While I'm out...") {
33 input "presence1", "capability.presenceSensor", title: "Who?"
35 section("Text me at...") {
36 input("recipients", "contact", title: "Send notifications to") {
37 input "phone1", "phone", title: "Phone number?"
43 subscribe(motion1, "motion.active", motionActiveHandler)
48 subscribe(motion1, "motion.active", motionActiveHandler)
51 def motionActiveHandler(evt) {
52 log.trace "$evt.value: $evt, $settings"
54 if (presence1.latestValue("presence") == "not present") {
55 // Don't send a continuous stream of text messages
57 def timeAgo = new Date(now() - (1000 * deltaSeconds))
58 def recentEvents = motion1.eventsSince(timeAgo)
59 log.debug "Found ${recentEvents?.size() ?: 0} events in the last $deltaSeconds seconds"
60 def alreadySentSms = recentEvents.count { it.value && it.value == "active" } > 1
63 log.debug "SMS already sent within the last $deltaSeconds seconds"
65 if (location.contactBookEnabled) {
66 log.debug "$motion1 has moved while you were out, sending notifications to: ${recipients?.size()}"
67 sendNotificationToContacts("${motion1.label} ${motion1.name} moved while you were out", recipients)
70 log.debug "$motion1 has moved while you were out, sending text"
71 sendSms(phone1, "${motion1.label} ${motion1.name} moved while you were out")
75 log.debug "Motion detected, but presence sensor indicates you are present"