-
Notifications
You must be signed in to change notification settings - Fork 70
/
ForecastHighLowTrigger.groovy
229 lines (211 loc) · 8.54 KB
/
ForecastHighLowTrigger.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/**
* Forecast High/Low Trigger
* Smartthings SmartApp
*
* Copyright 2015-2017 Sidney Johnson
* If you like this app, please support the developer via PayPal: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XKDRYZ3RUNR9Y
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
* for the specific language governing permissions and limitations under the License.
*
* Version: 1.0 - Initial Version
* Version: 1.1 - Update by David Smith - github: gunkl - Added ability to control a thermostat as well as switch.
* Version: 1.11 - Update by David Smith - github: gunkl - Change scheduled updates to be every other hour at 5mins after the hour.
* - New setting to NOT set thermostat if it is currently OFF. Avoids collisions with other apps, like those that turn it off when the door closes.
*
*/
definition(
name: "Forecast High/Low Trigger",
namespace: "sidjohn1",
author: "Sidney Johnson",
description: "Triggers a switch or thermostat mode based off the forecasted highs or lows for the day.",
category: "Green Living",
iconUrl: "http://cdn.device-icons.smartthings.com/Weather/weather11-icn.png",
iconX2Url: "http://cdn.device-icons.smartthings.com/Weather/[email protected]",
iconX3Url: "http://cdn.device-icons.smartthings.com/Weather/[email protected]")
preferences {
page name:"pageInfo"
}
def pageInfo() {
return dynamicPage(name: "pageInfo", install: true, uninstall: true) {
dailyForecast()
section("About") {
paragraph "Forecast High/Low Trigger smartapp for Smartthings. This app triggers a switch or thermostat mode based off the forecasted highs or lows for the day"
paragraph "${textVersion()}\n${textCopyright()}"
}
section("Current Forecast - High: ${state.forecastDayHigh}°${getTemperatureScale()} Low:${state.forecastDayLow}°${getTemperatureScale()}\nIf todays...") {
if (highLow != true) {
input name: "highLow", type: "bool", title: "High", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
else {
input name: "highLow", type: "bool", title: "Low", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
}
section("is") {
if (aboveBelow != true) {
input name: "aboveBelow", type: "bool", title: "Above or equal to", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
else {
input name: "aboveBelow", type: "bool", title: "Below or equal to", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
input name: "setTemp", type: "number", title: "this temperature... (°${getTemperatureScale()})", required: true, multiple: false, defaultValue: empty
}
section("Then set a switch...") {
input name: "switch1", type: "capability.switch", title: "Switch", required: false, multiple: true, defaultValue: empty
if (switch1OnOff != true) {
input name: "switch1OnOff", type: "bool", title: "On", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
else {
input name: "switch1OnOff", type: "bool", title: "Off", required: true, multiple:false, defaultValue: false, submitOnChange: true
}
input name: "switch1Schedule", type: "time", title: "at this time", required: false, multiple: false, defaultValue: empty
input name: "switch1Timmer", type: "number", title: "for this lengh of time (minutes)", required: false, multiple: false, defaultValue: empty
}
section("Then set the thermostat mode...") {
input name: "thermostat", type: "capability.thermostat", title: "thermostat", required: false, multiple: true, defaultValue: empty
input name: "thermostatmode", type: "enum", title: "Mode?", metadata: [values:["auto", "heat", "cool", "off"]]
if (dontsettstatalways != true) {
input name: "dontsettstatalways", type: "bool", title: "Set thermostat always", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
else {
input name: "dontsettstatalways", type: "bool", title: "Do not set thermostat if mode is OFF", required: false, multiple:false, defaultValue: false, submitOnChange: true
}
}
section([title:"Options", mobileOnly:true]) {
label(title:"Assign a name", required: false, defaultValue: "Forecast High/Low Trigger")
mode(title: "Set for specific mode(s)")
}
}
}
def installed() {
log.info "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.info "Updated with settings: ${settings}"
unschedule()
unsubscribe()
initialize()
}
def initialize() {
state.thermostatMode = thermostat.currentValue("thermostatMode")
log.info "Forecast Trigger ${textVersion()} ${textCopyright()}"
log.info "Thermostat is currently set to: ${state.thermostatMode}"
dailyForecast
// update forecast every hour at 5 minutes after the hour.
schedule("0 5 0/2 * * ?", dailyForecast)
if (settings.switch1Schedule) {
schedule(settings.switch1Schedule, scheduledRun)
}
}
def dailyForecast() {
def forecast
def forecastDayHigh
def forecastDayLow
def temperatureScale = getTemperatureScale()
if (settings.zipcode) {
forecast = getWeatherFeature("forecast", settings.zipcode)
}
else {
forecast = getWeatherFeature("forecast")
}
if (temperatureScale == "F") {
state.forecastDayHigh = forecast.forecast.simpleforecast.forecastday[0].high.fahrenheit.toInteger()
state.forecastDayLow = forecast.forecast.simpleforecast.forecastday[0].low.fahrenheit.toInteger()
}
else {
state.forecastDayHigh = forecast.forecast.simpleforecast.forecastday[0].high.celsius.toInteger()
state.forecastDayLow = forecast.forecast.simpleforecast.forecastday[0].low.celsius.toInteger()
}
state.update = new Date().format("yyyy-MM-dd HH:mm", location.timeZone)
if (settings.switch1Schedule == null) {
scheduledRun()
}
}
def scheduledRun() {
def delay
def forecastDay
state.thermostatMode = thermostat.currentValue("thermostatMode")
if (settings.switch1Timmer != null || 0) {
delay = switch1Timmer * 60
}
if (settings.highLow == false) {
forecastDay = state.forecastDayHigh.toInteger()
}
else {
forecastDay = state.forecastDayLow.toInteger()
}
if (settings.thermostat) {
log.debug "Dont set always: $dontsettstatalways - Thermostat mode: $state.thermostatMode"
if ((dontsettstatalways == true) && (state.thermostatMode != ["off"])) {
// if thermostat is off and setting is true then dont set thermostat mode
setThermostatMode()
}
if (dontsettstatalways == false) {
// if setting is false, then always set thermostat mode
setThermostatMode()
}
}
if (settings.switch1) {
if ((settings.aboveBelow == false) && (settings.switch1OnOff == false)) {
if (forecastDay >= settings.setTemp) {
switch1.on()
if (settings.switch1Timmer != null || 0) {
runIn(delay, "turnOff")
}
}
}
if ((settings.aboveBelow == false) && (settings.switch1OnOff == true)) {
if (forecastDay >= settings.setTemp) {
switch1.off()
if (settings.switch1Timmer != null || 0) {
runIn(delay, "turnOn")
}
}
}
if ((settings.aboveBelow == true) && (settings.switch1OnOff == false)) {
if (forecastDay <= settings.setTemp) {
switch1.on()
if (settings.switch1Timmer != null || 0) {
runIn(delay, "turnOff")
}
}
}
if ((settings.aboveBelow == true) && (settings.switch1OnOff == true)) {
if (forecastDay <= settings.setTemp) {
switch1.off()
if (settings.switch1Timmer != null || 0) {
runIn(delay, "turnOn")
}
}
}
}
}
def turnOff() {
switch1.off()
}
def turnOn() {
switch1.on()
}
private def textVersion() {
def text = "Version 1.11"
}
private def textCopyright() {
def text = "Copyright © 2015-2017 Sidjohn1"
}
private def setThermostatMode() {
// aboveBelow false means temp above or equal
if ((settings.aboveBelow == false) && (forecastDay >= settings.setTemp)) {
thermostat."${settings.thermostatmode}"()
}
// aboveBelow true means temp below or equal
if ((settings.aboveBelow == true) && (forecastDay <= settings.setTemp)) {
thermostat."${settings.thermostatmode}"()
}
}