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: "Monitor the temperature and when it rises above your setting get a notification and/or turn on an A/C unit or fan.",
22 category: "Convenience",
23 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/its-too-hot.png",
24 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/its-too-hot@2x.png"
28 section("Monitor the temperature...") {
29 input "temperatureSensor1", "capability.temperatureMeasurement"
31 section("When the temperature rises above...") {
32 input "temperature1", "number", title: "Temperature?"
34 section( "Notifications" ) {
35 input("recipients", "contact", title: "Send notifications to") {
36 input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
37 input "phone1", "phone", title: "Send a Text Message?", required: false
40 section("Turn on which A/C or fan...") {
41 input "switch1", "capability.switch", required: false
46 subscribe(temperatureSensor1, "temperature", temperatureHandler)
51 subscribe(temperatureSensor1, "temperature", temperatureHandler)
54 def temperatureHandler(evt) {
55 log.trace "temperature: $evt.value, $evt"
57 def tooHot = temperature1
58 def mySwitch = settings.switch1
60 // TODO: Replace event checks with internal state (the most reliable way to know if an SMS has been sent recently or not).
61 if (evt.doubleValue >= tooHot) {
62 log.debug "Checking how long the temperature sensor has been reporting <= $tooHot"
64 // Don't send a continuous stream of text messages
65 def deltaMinutes = 10 // TODO: Ask for "retry interval" in prefs?
66 def timeAgo = new Date(now() - (1000 * 60 * deltaMinutes).toLong())
67 def recentEvents = temperatureSensor1.eventsSince(timeAgo)?.findAll { it.name == "temperature" }
68 log.trace "Found ${recentEvents?.size() ?: 0} events in the last $deltaMinutes minutes"
69 def alreadySentSms = recentEvents.count { it.doubleValue >= tooHot } > 1
72 log.debug "SMS already sent within the last $deltaMinutes minutes"
73 // TODO: Send "Temperature back to normal" SMS, turn switch off
75 log.debug "Temperature rose above $tooHot: sending SMS and activating $mySwitch"
76 def tempScale = location.temperatureScale ?: "F"
77 send("${temperatureSensor1.displayName} is too hot, reporting a temperature of ${evt.value}${evt.unit?:tempScale}")
84 if (location.contactBookEnabled) {
85 log.debug("sending notifications to: ${recipients?.size()}")
86 sendNotificationToContacts(msg, recipients)
89 if (sendPushMessage != "No") {
90 log.debug("sending push message")
95 log.debug("sending text message")