Update smart-alarm.groovy
[smartapps.git] / official / smart-windows.groovy
1 /**
2  *  Smart Windows
3  *      Compares two temperatures – indoor vs outdoor, for example – then sends an alert if windows are open (or closed!).
4  *
5  *  Copyright 2014 Eric Gideon
6  *
7  *      Based in part on the "When it's going to rain" SmartApp by the SmartThings team,
8  *  primarily the message throttling code.
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11  *  in compliance with the License. You may obtain a copy of the License at:
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
16  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
17  *  for the specific language governing permissions and limitations under the License.
18  *
19  */
20 definition(
21         name: "Smart Windows",
22         namespace: "egid",
23         author: "Eric Gideon",
24         description: "Compares two temperatures – indoor vs outdoor, for example – then sends an alert if windows are open (or closed!). If you don't use an external temperature device, your location will be used instead.",
25         iconUrl: "https://s3.amazonaws.com/smartthings-device-icons/Home/home9-icn.png",
26         iconX2Url: "https://s3.amazonaws.com/smartthings-device-icons/Home/home9-icn@2x.png"
27 )
28
29
30 preferences {
31
32   if (!(location.zipCode || ( location.latitude && location.longitude )) && location.channelName == 'samsungtv') {
33                 section { paragraph title: "Note:", "Location is required for this SmartApp. Go to 'Location Name' settings to setup your correct location." }
34         }
35
36         section( "Set the temperature range for your comfort zone..." ) {
37                 input "minTemp", "number", title: "Minimum temperature"
38                 input "maxTemp", "number", title: "Maximum temperature"
39         }
40         section( "Select windows to check..." ) {
41                 input "sensors", "capability.contactSensor", multiple: true
42         }
43         section( "Select temperature devices to monitor..." ) {
44                 input "inTemp", "capability.temperatureMeasurement", title: "Indoor"
45                 input "outTemp", "capability.temperatureMeasurement", title: "Outdoor (optional)", required: false
46         }
47
48         if (location.channelName != 'samsungtv') {
49                 section( "Set your location" ) { input "zipCode", "text", title: "Zip code" }
50   }
51
52         section( "Notifications" ) {
53                 input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required:false
54                 input "retryPeriod", "number", title: "Minutes between notifications:"
55         }
56 }
57
58
59 def installed() {
60         log.debug "Installed: $settings"
61         subscribe( inTemp, "temperature", temperatureHandler )
62 }
63
64 def updated() {
65         log.debug "Updated: $settings"
66         unsubscribe()
67         subscribe( inTemp, "temperature", temperatureHandler )
68 }
69
70
71 def temperatureHandler(evt) {
72         def currentOutTemp = null
73         if ( outTemp ) {
74                 currentOutTemp = outTemp.latestValue("temperature")
75         } else {
76                 log.debug "No external temperature device set. Checking WUnderground...."
77                 currentOutTemp = weatherCheck()
78         }
79
80         def currentInTemp = evt.doubleValue
81         def openWindows = sensors.findAll { it?.latestValue("contact") == 'open' }
82
83         log.trace "Temp event: $evt"
84         log.info "In: $currentInTemp; Out: $currentOutTemp"
85
86         // Don't spam notifications
87         // *TODO* use state.foo from Severe Weather Alert to do this better
88         if (!retryPeriod) {
89                 def retryPeriod = 30
90         }
91         def timeAgo = new Date(now() - (1000 * 60 * retryPeriod).toLong())
92         def recentEvents = inTemp.eventsSince(timeAgo)
93         log.trace "Found ${recentEvents?.size() ?: 0} events in the last $retryPeriod minutes"
94
95         // Figure out if we should notify
96         if ( currentInTemp > minTemp && currentInTemp < maxTemp ) {
97                 log.info "In comfort zone: $currentInTemp is between $minTemp and $maxTemp."
98                 log.debug "No notifications sent."
99         } else if ( currentInTemp > maxTemp ) {
100                 // Too warm. Can we do anything?
101
102                 def alreadyNotified = recentEvents.count { it.doubleValue > currentOutTemp } > 1
103
104                 if ( !alreadyNotified ) {
105                         if ( currentOutTemp < maxTemp && !openWindows ) {
106                                 send( "Open some windows to cool down the house! Currently ${currentInTemp}°F inside and ${currentOutTemp}°F outside." )
107                         } else if ( currentOutTemp > maxTemp && openWindows ) {
108                                 send( "It's gotten warmer outside! You should close these windows: ${openWindows.join(', ')}. Currently ${currentInTemp}°F inside and ${currentOutTemp}°F outside." )
109                         } else {
110                                 log.debug "No notifications sent. Everything is in the right place."
111                         }
112                 } else {
113                         log.debug "Already notified! No notifications sent."
114                 }
115         } else if ( currentInTemp < minTemp ) {
116                 // Too cold! Is it warmer outside?
117
118                 def alreadyNotified = recentEvents.count { it.doubleValue < currentOutTemp } > 1
119
120                 if ( !alreadyNotified ) {
121                         if ( currentOutTemp > minTemp && !openWindows ) {
122                                 send( "Open some windows to warm up the house! Currently ${currentInTemp}°F inside and ${currentOutTemp}°F outside." )
123                         } else if ( currentOutTemp < minTemp && openWindows ) {
124                                 send( "It's gotten colder outside! You should close these windows: ${openWindows.join(', ')}. Currently ${currentInTemp}°F inside and ${currentOutTemp}°F outside." )
125                         } else {
126                                 log.debug "No notifications sent. Everything is in the right place."
127                         }
128                 } else {
129                         log.debug "Already notified! No notifications sent."
130                 }
131         }
132 }
133
134 def weatherCheck() {
135         def json 
136         if (location.channelName != 'samsungtv')
137                 json = getWeatherFeature("conditions", zipCode)
138         else
139                 json = getWeatherFeature("conditions")
140         def currentTemp = json?.current_observation?.temp_f
141
142         if ( currentTemp ) {
143         log.trace "Temp: $currentTemp (WeatherUnderground)"
144                 return currentTemp
145         } else {
146                 log.warn "Did not get a temp: $json"
147                 return false
148         }
149 }
150
151 private send(msg) {
152         if ( sendPushMessage != "No" ) {
153                 log.debug( "sending push message" )
154                 sendPush( msg )
155         sendEvent(linkText:app.label, descriptionText:msg, eventType:"SOLUTION_EVENT", displayed: true, name:"summary")
156         }
157
158         if ( phone1 ) {
159                 log.debug( "sending text message" )
160                 sendSms( phone1, msg )
161         }
162
163         log.info msg
164 }