3 * Compares two temperatures – indoor vs outdoor, for example – then sends an alert if windows are open (or closed!).
5 * Copyright 2014 Eric Gideon
7 * Based in part on the "When it's going to rain" SmartApp by the SmartThings team,
8 * primarily the message throttling code.
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:
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 name: "Smart Windows",
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"
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." }
36 section( "Set the temperature range for your comfort zone..." ) {
37 input "minTemp", "number", title: "Minimum temperature"
38 input "maxTemp", "number", title: "Maximum temperature"
40 section( "Select windows to check..." ) {
41 input "sensors", "capability.contactSensor", multiple: true
43 section( "Select temperature devices to monitor..." ) {
44 input "inTemp", "capability.temperatureMeasurement", title: "Indoor"
45 input "outTemp", "capability.temperatureMeasurement", title: "Outdoor (optional)", required: false
48 if (location.channelName != 'samsungtv') {
49 section( "Set your location" ) { input "zipCode", "text", title: "Zip code" }
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:"
60 log.debug "Installed: $settings"
61 subscribe( inTemp, "temperature", temperatureHandler )
65 log.debug "Updated: $settings"
67 subscribe( inTemp, "temperature", temperatureHandler )
71 def temperatureHandler(evt) {
72 def currentOutTemp = null
74 currentOutTemp = outTemp.latestValue("temperature")
76 log.debug "No external temperature device set. Checking WUnderground...."
77 currentOutTemp = weatherCheck()
80 def currentInTemp = evt.doubleValue
81 def openWindows = sensors.findAll { it?.latestValue("contact") == 'open' }
83 log.trace "Temp event: $evt"
84 log.info "In: $currentInTemp; Out: $currentOutTemp"
86 // Don't spam notifications
87 // *TODO* use state.foo from Severe Weather Alert to do this better
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"
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?
102 def alreadyNotified = recentEvents.count { it.doubleValue > currentOutTemp } > 1
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." )
110 log.debug "No notifications sent. Everything is in the right place."
113 log.debug "Already notified! No notifications sent."
115 } else if ( currentInTemp < minTemp ) {
116 // Too cold! Is it warmer outside?
118 def alreadyNotified = recentEvents.count { it.doubleValue < currentOutTemp } > 1
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." )
126 log.debug "No notifications sent. Everything is in the right place."
129 log.debug "Already notified! No notifications sent."
136 if (location.channelName != 'samsungtv')
137 json = getWeatherFeature("conditions", zipCode)
139 json = getWeatherFeature("conditions")
140 def currentTemp = json?.current_observation?.temp_f
143 log.trace "Temp: $currentTemp (WeatherUnderground)"
146 log.warn "Did not get a temp: $json"
152 if ( sendPushMessage != "No" ) {
153 log.debug( "sending push message" )
155 sendEvent(linkText:app.label, descriptionText:msg, eventType:"SOLUTION_EVENT", displayed: true, name:"summary")
159 log.debug( "sending text message" )
160 sendSms( phone1, msg )