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.
20 * 2014-10-03: Added capability.button device picker and button.pushed event subscription. For Doorbell.
23 name: "Notify Me When",
24 namespace: "smartthings",
25 author: "SmartThings",
26 description: "Receive notifications when anything happens in your home.",
27 category: "Convenience",
28 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact.png",
29 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/window_contact@2x.png"
33 section("Choose one or more, when..."){
34 input "button", "capability.button", title: "Button Pushed", required: false, multiple: true //tw
35 input "motion", "capability.motionSensor", title: "Motion Here", required: false, multiple: true
36 input "contact", "capability.contactSensor", title: "Contact Opens", required: false, multiple: true
37 input "contactClosed", "capability.contactSensor", title: "Contact Closes", required: false, multiple: true
38 input "acceleration", "capability.accelerationSensor", title: "Acceleration Detected", required: false, multiple: true
39 input "mySwitch", "capability.switch", title: "Switch Turned On", required: false, multiple: true
40 input "mySwitchOff", "capability.switch", title: "Switch Turned Off", required: false, multiple: true
41 input "arrivalPresence", "capability.presenceSensor", title: "Arrival Of", required: false, multiple: true
42 input "departurePresence", "capability.presenceSensor", title: "Departure Of", required: false, multiple: true
43 input "smoke", "capability.smokeDetector", title: "Smoke Detected", required: false, multiple: true
44 input "water", "capability.waterSensor", title: "Water Sensor Wet", required: false, multiple: true
46 section("Send this message (optional, sends standard status message if not specified)"){
47 input "messageText", "text", title: "Message Text", required: false
49 section("Via a push notification and/or an SMS message"){
50 input("recipients", "contact", title: "Send notifications to") {
51 input "phone", "phone", title: "Enter a phone number to get SMS", required: false
52 paragraph "If outside the US please make sure to enter the proper country code"
53 input "pushAndPhone", "enum", title: "Notify me via Push Notification", required: false, options: ["Yes", "No"]
56 section("Minimum time between messages (optional, defaults to every message)") {
57 input "frequency", "decimal", title: "Minutes", required: false
62 log.debug "Installed with settings: ${settings}"
67 log.debug "Updated with settings: ${settings}"
72 def subscribeToEvents() {
73 subscribe(button, "button.pushed", eventHandler) //tw
74 subscribe(contact, "contact.open", eventHandler)
75 subscribe(contactClosed, "contact.closed", eventHandler)
76 subscribe(acceleration, "acceleration.active", eventHandler)
77 subscribe(motion, "motion.active", eventHandler)
78 subscribe(mySwitch, "switch.on", eventHandler)
79 subscribe(mySwitchOff, "switch.off", eventHandler)
80 subscribe(arrivalPresence, "presence.present", eventHandler)
81 subscribe(departurePresence, "presence.not present", eventHandler)
82 subscribe(smoke, "smoke.detected", eventHandler)
83 subscribe(smoke, "smoke.tested", eventHandler)
84 subscribe(smoke, "carbonMonoxide.detected", eventHandler)
85 subscribe(water, "water.wet", eventHandler)
88 def eventHandler(evt) {
89 log.debug "Notify got evt ${evt}"
91 def lastTime = state[evt.deviceId]
92 if (lastTime == null || now() - lastTime >= frequency * 60000) {
101 private sendMessage(evt) {
102 String msg = messageText
106 msg = defaultText(evt)
107 options = [translatable: true, triggerEvent: evt]
109 log.debug "$evt.name:$evt.value, pushAndPhone:$pushAndPhone, '$msg'"
111 if (location.contactBookEnabled) {
112 sendNotificationToContacts(msg, recipients, options)
115 options.phone = phone
116 if (pushAndPhone != 'No') {
117 log.debug 'Sending push and SMS'
118 options.method = 'both'
120 log.debug 'Sending SMS'
121 options.method = 'phone'
123 } else if (pushAndPhone != 'No') {
124 log.debug 'Sending push'
125 options.method = 'push'
127 log.debug 'Sending nothing'
128 options.method = 'none'
130 sendNotification(msg, options)
133 state[evt.deviceId] = now()
137 private defaultText(evt) {
138 if (evt.name == 'presence') {
139 if (evt.value == 'present') {
140 if (includeArticle) {
141 '{{ triggerEvent.linkText }} has arrived at the {{ location.name }}'
144 '{{ triggerEvent.linkText }} has arrived at {{ location.name }}'
147 if (includeArticle) {
148 '{{ triggerEvent.linkText }} has left the {{ location.name }}'
151 '{{ triggerEvent.linkText }} has left {{ location.name }}'
155 '{{ triggerEvent.descriptionText }}'
159 private getIncludeArticle() {
160 def name = location.name.toLowerCase()
161 def segs = name.split(" ")
162 !(["work","home"].contains(name) || (segs.size() > 1 && (["the","my","a","an"].contains(segs[0]) || segs[0].endsWith("'s"))))