forked from mhutchy/Hubitat-Apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Watering system
351 lines (260 loc) · 11.5 KB
/
Watering system
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* Water the garden
*
* Copyright 2019 Mark Hutchings
*
* 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.
*
*/
definition(
name: "Water the garden",
namespace: "mhutchy",
author: "Mark Hutchings",
description: "Switch a device on and notify with various triggers / sensors (timed)",
category: "Convenience",
iconUrl: "http://cdn.device-icons.smartthings.com/Outdoor/outdoor12-icn.png",
iconX2Url: "http://cdn.device-icons.smartthings.com/Outdoor/[email protected]",
iconX3Url: "http://cdn.device-icons.smartthings.com/Outdoor/[email protected]")
preferences {
section ("Trigger Inputs"){
input "manualSwitch", "capability.switch", title: "Switch to run sequence manually", multiple: false, required: false
input "manualButton", "capability.pushableButton", title: "button to run sequence manually", multiple: false, required: false
}
section ("Weather information"){
input "temperature", "capability.temperatureMeasurement", title: "Local temperature", description: "A temperature measuremet device located outside",multiple: false, require: true
input "apiKey", "string", title: "openweathermap API Key", required: true
input "city", "string", title: "city", required: true
input "country", "string", title: "country", description: "Two letter code eg us, uk, fr etc", required: true
}
section ("Water Valvle"){
input "sprinkler", "capability.switch", title: "Sprinkler to be operated?", multiple: false, required: true
}
section ("Automatic watering"){
input "startTime", "time", title: "if autoschedule required: Start Time", required: false
input "duration", "number", title: "How long to water for (auto and manual modes) (s)", required: true
}
section ("Allow automatic watering if the following conditions are met"){
input "maxTemp", "number", title: "Today's temperature was over (C)", required: true
input "minTemp", "number", title: "Tomorrow morning's min temperature is over (C)", required: true
input "maxRain24", "number", title: "Rain in last 24h is less than (mm)", required: true
input "dryDays", "number", title: "How many dry days before watering", required: true
input "highTemp", "number", title: "Above this temperature, will water after only 24hrs dry", required: true
input "rainForcastAllowed", "number", title: "If forcast rain is less than (mm)", required: true
}
section ("Notifications"){
input "notification", "bool", title: "Send a push notification to smartphone", multiple: false, required: true
input "notificationDevice", "capability.notification", title: "Notification Device", multiple: true, required: false
input "SMSMessage", "bool", title: "Send an SMS notification", multiple: false, required: true
input "SMSNumber", "string", title: "phone number for text message", multiple: true, required: false
}
section ("App preferences"){
input(name: "detailedLog", type: "bool", title: "Detailed logging", description: "Detailed logging", displayDuringSetup: true, required: true)
input(name: "conditionalManual", type: "bool", title: "Manual with weather check", description: "If this is enabled the manual trigger will run the weather check 1st as if it is in auto mode", displayDuringSetup: true, required: true)
}
}
def installed() {
log.debug "$app.label : Installed with settings: ${settings}"
unsubscribe()
initialize()
}
def updated() {
log.debug "$app.label : Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
log.debug "$app.label : Initialised ${settings}"
subscribe (manualSwitch, "switch.on", manual)
subscribe (manualButton, "pushed.1", manual)
subscribe (sprinkler, "switch", sendNotification)
subscribe (temperature, "temperature", updateTemperatures)
if (startTime) {
schedule(startTime, "scheduled")
}
else {logDebug ("$app.label : No sechedule set for ${sprinkler.displayName}")}
}
def manual(evt) {
logDebug ("${evt.value} received")
if (conditionalManual){
scheduled (evt)
}
else{
if (state.command == "on") {
logDebug ("${sprinkler.displayName} Manual switch off")
switchOff ()
}
else {
logDebug ("${sprinkler.displayName} Manual run for ${duration}s")
switchOn ()
runIn (duration, switchOff)
}
}
}
def updateTemperatures (evt) {
def today = new Date().format( 'EEEE' )
logDebug ("It is $today today, last reading was on $state.readingDay")
logDebug ("Temperature changes detected - $evt.value")
if ((state.maxTempToday == null) || (state.minTempToday == null) || (state.readingDay != today)){
logDebug ("initialising max and min temp to $evt.value")
state.maxTempYesterday = state.maxTempToday
state.minTempYesterday = state.minTempToday
state.maxTempToday = evt.value
state.minTempToday = evt.value
}
if (evt.value > state.maxTempToday) {
logDebug ("Setting max temp from $state.maxTempToday to $evt.value")
state.maxTempToday = evt.value
}
if (evt.value < state.minTempToday) {
logDebug ("Setting min temp from $state.minTempToday to $evt.value")
state.minTempToday = evt.value
}
state.readingDay = today
}
def scheduled (evt) {
logDebug ("$app.label : scheduled handler called, scheduled ? $scheduled")
def sprinklerState = sprinkler.currentValue("switch")
logDebug ("Getting Current weather data")
try {
httpGet("https://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${apiKey}") { resp ->
resp.headers.each {
}
def theHeaders = resp.getHeaders("Content-Length")
weatherNow = resp.data
}
} catch (e) {
log.error "Error getting current data: $e"
}
logDebug ("Getting weather forcast")
try {
httpGet("https://api.openweathermap.org/data/2.5/forecast?q=${city},${country}&appid=${apiKey}") { forcast ->
forcast.headers.each {
}
// get an array of all headers with the specified key
def theHeaders = forcast.getHeaders("Content-Length")
weatherForcast = forcast.data
}
} catch (e) {
log.error "Error getting forcast: $e"
}
//workout forcast max and min
def rainAmount = 0
def forcastRain = 0
def forcastMax = weatherForcast.list[0].main.temp
def forcastMin = weatherForcast.list[0].main.temp
for (int i = 0; i < 9; i++) {
rainI = weatherForcast.list[i].rain
tempI = weatherForcast.list[i].main.temp
if (rainI != null) {
rainAmount = (rainI.get("3h"))
}
if (tempI > forcastMax){
forcastMax = tempI}
else if (tempI < forcastMin){
forcastMin = tempI}
forcastRain = forcastRain + rainAmount
}
forcastMax -= 273.15
forcastMin -= 273.15
def maxToday = weatherNow.main.temp_max - 273.15
def minToday = weatherNow.main.temp_min - 273.15
def Rain24H = "$forcastRain"
def Rain1H = weatherForcast.list[0].rain
logDebug "rain 1h ${Rain1H}"
if (weatherForcast.list[0].rain != null){
logDebug "rain 1h ${Rain1H.get("3h")}"
Rain1H = Rain1H.get("3h")
}
else{
rain1H = 0
}
logDebug ("Forcast minimum next 24h : ${forcastMin}C")
def temperatureOK = ((state.maxTempToday as float) >= (maxTemp as float))
logDebug ("Max temperature today of ${state.maxTempToday}C, ok for watering (>= ${maxTemp}C)? $temperatureOK")
def veryHot = (state.maxTempToday >= highTemp)
def aboveFreezing = (forcastMin >= minTemp)
logDebug ("Minimum temperature 24h of ${forcastMin}C forcast, Ok for watering (>= ${minTemp}C)? $aboveFreezing")
def rain24HourOK = (state.todaysRain <= maxRain24)
logDebug ("${state.todaysRain}mm rain has fallen in last 24h, Ok for watering ( <= ${maxRain24}mm)? $rain24HourOK")
def rainingNow = (Rain1H >= 0)
logDebug ("Is it raining now? ($Rain1H >= 0) ${rainingNow}")
if (!state.noDryDays) {state.noDryDays = 0}
def today = new Date().format( 'EEEE' )
logDebug ("Today is $today, last checked for dry day on $state.checkedDry")
if (state.checkedDry != today) {
if (rain24HourOK) {
state.noDryDays = state.noDryDays + 1
}
else (!rain24HourOK) {
state.noDryDays = 0
}
state.todaysRain = forcastRain
}
state.checkedDry = today
logDebug ("There have been $state.noDryDays dry days")
def enoughDryDays = (state.noDryDays >= dryDays)
// check to see if it will rain tonight or tomorrow during the day
def lowRainForcast = (forcastRain < rainForcastAllowed)
logDebug ("Forcast rain is $forcastRain : (< $rainForcastAllowed ? $lowRainForcast)")
logDebug ("ok for watering? scheduled ($scheduled) && temperatureOK ($temperatureOK) && rain24HourOK ($rain24HourOK) && !(rainingNow) ($rainingNow) && aboveFreezing ($aboveFreezing) && (enoughDryDays ($enoughDryDays) || veryHot ($veryHot)) && (lowRainForcast ($lowRainForcast))")
def okForAuto = (scheduled && temperatureOK && rain24HourOK && !(rainingNow) && aboveFreezing && (enoughDryDays || veryHot) && lowRainForcast)
logDebug ("All conditions met for auto watering? $okForAuto")
if (okForAuto){
if (sprinklerState == "on") { logDebug ("${sprinkler.displayName} already on - no action")
}
if (sprinklerState == "off") {
logDebug ("${sprinkler.displayName} will run for ${duration}s")
switchOn ()
runIn (duration, switchOff)
}
}
}
def switchOn () {
logDebug ("$app.label : ${sprinkler.displayName} switching on")
sprinkler.on ()
state.command = "on"
runIn (2, sprinklerStatus)
}
def switchOff () {
logDebug ("$app.label : ${sprinkler.displayName} switching off")
sprinkler.off ()
state.command = "off"
runIn (2, sprinklerStatus)
}
def sendNotification (evt) {
logDebug ("$app.label - Notification handler called")
if (notification) {
def message = "$app.label : ${sprinkler.displayName} switched ${sprinkler.currentValue("switch")}"
if (notification) {
log.debug "$app.label Push notification : $message}"
notificationDevice.deviceNotification(message)
sendPush(message)}
if (SMSMessage) {
log.debug "$app.label Texting $SMSNumber - $message"
sendSmsMessage("$SMSNumber", message)}
}
}
def sprinklerStatus () {
logDebug ("${sprinkler.displayName} is now ${sprinkler.currentValue("switch")}")
if (state.command != sprinkler.currentValue("switch")){
log.error "$app.label - ${sprinkler.displayName} did not switch $state.command"
if (state.command == "off"){
log.debug "$app.label : ${sprinkler.displayName} sending off command again"
switchOff ()}
else {
log.debug "$app.label : ${sprinkler.displayName} sending on command again"
switchOn ()}
}
}
def logDebug (text) {
if (text){
log.debug "$text"
}
}