Update influxdb-logger.groovy
[smartapps.git] / official / its-too-cold.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  *  It's Too Cold
14  *
15  *  Author: SmartThings
16  */
17 definition(
18     name: "It's Too Cold",
19     namespace: "smartthings",
20     author: "SmartThings",
21     description: "Monitor the temperature and when it drops below your setting get a text and/or turn on a heater or additional appliance.",
22     category: "Convenience",
23     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch.png",
24     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/temp_thermo-switch@2x.png"
25 )
26
27 preferences {
28         section("Monitor the temperature...") {
29                 input "temperatureSensor1", "capability.temperatureMeasurement"
30         }
31         section("When the temperature drops below...") {
32                 input "temperature1", "number", title: "Temperature?"
33         }
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
38         }
39     }
40         section("Turn on a heater...") {
41                 input "switch1", "capability.switch", required: false
42         }
43 }
44
45 def installed() {
46         subscribe(temperatureSensor1, "temperature", temperatureHandler)
47 }
48
49 def updated() {
50         unsubscribe()
51         subscribe(temperatureSensor1, "temperature", temperatureHandler)
52 }
53
54 def temperatureHandler(evt) {
55         log.trace "temperature: $evt.value, $evt"
56
57         def tooCold = temperature1
58         def mySwitch = settings.switch1
59
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 <= tooCold) {
62                 log.debug "Checking how long the temperature sensor has been reporting <= $tooCold"
63
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 <= tooCold } > 1
70
71                 if (alreadySentSms) {
72                         log.debug "SMS already sent within the last $deltaMinutes minutes"
73                         // TODO: Send "Temperature back to normal" SMS, turn switch off
74                 } else {
75                         log.debug "Temperature dropped below $tooCold:  sending SMS and activating $mySwitch"
76                         def tempScale = location.temperatureScale ?: "F"
77                         send("${temperatureSensor1.displayName} is too cold, reporting a temperature of ${evt.value}${evt.unit?:tempScale}")
78                         switch1?.on()
79                 }
80         }
81 }
82
83 private send(msg) {
84     if (location.contactBookEnabled) {
85         log.debug("sending notifications to: ${recipients?.size()}")
86         sendNotificationToContacts(msg, recipients)
87     }
88     else {
89         if (sendPushMessage != "No") {
90             log.debug("sending push message")
91             sendPush(msg)
92         }
93
94         if (phone1) {
95             log.debug("sending text message")
96             sendSms(phone1, msg)
97         }
98     }
99
100     log.debug msg
101 }