Update speaker-mood-music.groovy
[smartapps.git] / official / text-me-when-theres-motion-and-im-not-here.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
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:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  *
13  *  Text Me When There's Motion and I'm Not Here
14  *
15  *  Author: SmartThings
16  */
17
18 definition(
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"
26 )
27
28 preferences {
29         section("When there's movement...") {
30                 input "motion1", "capability.motionSensor", title: "Where?"
31         }
32         section("While I'm out...") {
33                 input "presence1", "capability.presenceSensor", title: "Who?"
34         }
35         section("Text me at...") {
36         input("recipients", "contact", title: "Send notifications to") {
37             input "phone1", "phone", title: "Phone number?"
38         }
39         }
40 }
41
42 def installed() {
43         subscribe(motion1, "motion.active", motionActiveHandler)
44 }
45
46 def updated() {
47         unsubscribe()
48         subscribe(motion1, "motion.active", motionActiveHandler)
49 }
50
51 def motionActiveHandler(evt) {
52         log.trace "$evt.value: $evt, $settings"
53
54         if (presence1.latestValue("presence") == "not present") {
55                 // Don't send a continuous stream of text messages
56                 def deltaSeconds = 10
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
61
62                 if (alreadySentSms) {
63                         log.debug "SMS already sent within the last $deltaSeconds seconds"
64                 } else {
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)
68             }
69             else {
70                 log.debug "$motion1 has moved while you were out, sending text"
71                 sendSms(phone1, "${motion1.label} ${motion1.name} moved while you were out")
72             }
73                 }
74         } else {
75                 log.debug "Motion detected, but presence sensor indicates you are present"
76         }
77 }