-
Notifications
You must be signed in to change notification settings - Fork 1
/
AJB-SmartWeather-mB-Dew-PWS.groovy
529 lines (467 loc) · 22.4 KB
/
AJB-SmartWeather-mB-Dew-PWS.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
*
* Copyright 2015 SmartThings
*
* 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.
*
* SmartWeather Station
*
* Author: abuttino
*
* Date: 2013-04-30
*/
metadata {
definition (name: "SmartWeatherv2019-withPWS", namespace: "abuttino", author: "abuttino") {
capability "Illuminance Measurement"
capability "Temperature Measurement"
capability "Relative Humidity Measurement"
capability "Ultraviolet Index"
capability "Sensor"
capability "Power Meter"
capability "Voltage Measurement"
capability "Refresh"
attribute "localSunrise", "string"
attribute "localSunset", "string"
attribute "city", "string"
attribute "timeZoneOffset", "string"
attribute "weather", "string"
attribute "wind", "string"
attribute "windVector", "string"
attribute "weatherIcon", "string"
attribute "forecastIcon", "string"
attribute "feelsLike", "string"
attribute "percentPrecip", "string"
attribute "alert", "string"
attribute "alertKeys", "string"
attribute "sunriseDate", "string"
attribute "sunsetDate", "string"
attribute "lastUpdate", "string"
attribute "uvDescription", "string"
attribute "forecastToday", "string"
attribute "forecastTonight", "string"
attribute "forecastTomorrow", "string"
attribute "pressuremb", "string"
attribute "dewpoint", "string"
attribute "stationIdent", "string"
}
preferences {
input "zipCode", "text", title: "Zip Code (optional)", required: false
input "stationId", "text", title: "Personal Weather Station ID (optional)", required: false
}
tiles(scale: 2) {
valueTile("temperature", "device.temperature", height: 2, width: 2) {
state "default", label:'${currentValue}°',
backgroundColors:[
[value: 31, color: "#153591"],
[value: 44, color: "#1e9cbb"],
[value: 59, color: "#90d2a7"],
[value: 74, color: "#44b621"],
[value: 84, color: "#f1d801"],
[value: 95, color: "#d04e00"],
[value: 96, color: "#bc2323"]
]
}
valueTile("feelsLike", "device.feelsLike", decoration: "flat", height: 1, width: 2) {
state "default", label:'Feels like ${currentValue}°'
}
standardTile("weatherIcon", "device.weatherIcon", decoration: "flat", height: 2, width: 2) {
state "00", icon:"https://smartthings-twc-icons.s3.amazonaws.com/00.png", label: ""
state "01", icon:"https://smartthings-twc-icons.s3.amazonaws.com/01.png", label: ""
state "02", icon:"https://smartthings-twc-icons.s3.amazonaws.com/02.png", label: ""
state "03", icon:"https://smartthings-twc-icons.s3.amazonaws.com/03.png", label: ""
state "04", icon:"https://smartthings-twc-icons.s3.amazonaws.com/04.png", label: ""
state "05", icon:"https://smartthings-twc-icons.s3.amazonaws.com/05.png", label: ""
state "06", icon:"https://smartthings-twc-icons.s3.amazonaws.com/06.png", label: ""
state "07", icon:"https://smartthings-twc-icons.s3.amazonaws.com/07.png", label: ""
state "08", icon:"https://smartthings-twc-icons.s3.amazonaws.com/08.png", label: ""
state "09", icon:"https://smartthings-twc-icons.s3.amazonaws.com/09.png", label: ""
state "10", icon:"https://smartthings-twc-icons.s3.amazonaws.com/10.png", label: ""
state "11", icon:"https://smartthings-twc-icons.s3.amazonaws.com/11.png", label: ""
state "12", icon:"https://smartthings-twc-icons.s3.amazonaws.com/12.png", label: ""
state "13", icon:"https://smartthings-twc-icons.s3.amazonaws.com/13.png", label: ""
state "14", icon:"https://smartthings-twc-icons.s3.amazonaws.com/14.png", label: ""
state "15", icon:"https://smartthings-twc-icons.s3.amazonaws.com/15.png", label: ""
state "16", icon:"https://smartthings-twc-icons.s3.amazonaws.com/16.png", label: ""
state "17", icon:"https://smartthings-twc-icons.s3.amazonaws.com/17.png", label: ""
state "18", icon:"https://smartthings-twc-icons.s3.amazonaws.com/18.png", label: ""
state "19", icon:"https://smartthings-twc-icons.s3.amazonaws.com/19.png", label: ""
state "20", icon:"https://smartthings-twc-icons.s3.amazonaws.com/20.png", label: ""
state "21", icon:"https://smartthings-twc-icons.s3.amazonaws.com/21.png", label: ""
state "22", icon:"https://smartthings-twc-icons.s3.amazonaws.com/22.png", label: ""
state "23", icon:"https://smartthings-twc-icons.s3.amazonaws.com/23.png", label: ""
state "24", icon:"https://smartthings-twc-icons.s3.amazonaws.com/24.png", label: ""
state "25", icon:"https://smartthings-twc-icons.s3.amazonaws.com/25.png", label: ""
state "26", icon:"https://smartthings-twc-icons.s3.amazonaws.com/26.png", label: ""
state "27", icon:"https://smartthings-twc-icons.s3.amazonaws.com/27.png", label: ""
state "28", icon:"https://smartthings-twc-icons.s3.amazonaws.com/28.png", label: ""
state "29", icon:"https://smartthings-twc-icons.s3.amazonaws.com/29.png", label: ""
state "30", icon:"https://smartthings-twc-icons.s3.amazonaws.com/30.png", label: ""
state "31", icon:"https://smartthings-twc-icons.s3.amazonaws.com/31.png", label: ""
state "32", icon:"https://smartthings-twc-icons.s3.amazonaws.com/32.png", label: ""
state "33", icon:"https://smartthings-twc-icons.s3.amazonaws.com/33.png", label: ""
state "34", icon:"https://smartthings-twc-icons.s3.amazonaws.com/34.png", label: ""
state "35", icon:"https://smartthings-twc-icons.s3.amazonaws.com/35.png", label: ""
state "36", icon:"https://smartthings-twc-icons.s3.amazonaws.com/36.png", label: ""
state "37", icon:"https://smartthings-twc-icons.s3.amazonaws.com/37.png", label: ""
state "38", icon:"https://smartthings-twc-icons.s3.amazonaws.com/38.png", label: ""
state "39", icon:"https://smartthings-twc-icons.s3.amazonaws.com/39.png", label: ""
state "40", icon:"https://smartthings-twc-icons.s3.amazonaws.com/40.png", label: ""
state "41", icon:"https://smartthings-twc-icons.s3.amazonaws.com/41.png", label: ""
state "42", icon:"https://smartthings-twc-icons.s3.amazonaws.com/42.png", label: ""
state "43", icon:"https://smartthings-twc-icons.s3.amazonaws.com/43.png", label: ""
state "44", icon:"https://smartthings-twc-icons.s3.amazonaws.com/44.png", label: ""
state "45", icon:"https://smartthings-twc-icons.s3.amazonaws.com/45.png", label: ""
state "46", icon:"https://smartthings-twc-icons.s3.amazonaws.com/46.png", label: ""
state "47", icon:"https://smartthings-twc-icons.s3.amazonaws.com/47.png", label: ""
state "na", icon:"https://smartthings-twc-icons.s3.amazonaws.com/na.png", label: ""
}
valueTile("humidity", "device.humidity", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue}% humidity'
}
valueTile("wind", "device.windVector", decoration: "flat", height: 1, width: 2) {
state "default", label:'Wind\n${currentValue}'
}
valueTile("dewPoint", "device.dewpoint", decoration: "flat", height: 1, width: 2) {
state "default", label:'Dewpoint ${currentValue}°'
}
valueTile("pressureMb", "device.pressuremb", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue} "Hg'
}
valueTile("percentPrecip", "device.percentPrecip", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue}% precip'
}
valueTile("weather", "device.weather", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue}'
}
valueTile("alert", "device.alert", decoration: "flat", height: 2, width: 6) {
state "default", label:'${currentValue}'
}
standardTile("refresh", "device.weather", decoration: "flat", height: 1, width: 2) {
state "default", label: "", action: "refresh", icon:"st.secondary.refresh"
}
valueTile("rise", "device.localSunrise", decoration: "flat", height: 1, width: 2) {
state "default", label:'Sunrise ${currentValue}'
}
valueTile("set", "device.localSunset", decoration: "flat", height: 1, width: 2) {
state "default", label:'Sunset ${currentValue}'
}
valueTile("light", "device.illuminance", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue} lux'
}
valueTile("station", "device.stationIdent", decoration: "flat", height: 1, width: 2) {
state "default", label:'PWS: ${currentValue}'
}
valueTile("uvDesc", "device.uvDescription", decoration: "flat", height: 1, width: 2) {
state "default", label:'${currentValue}'
}
valueTile("uvIndex", "device.ultravioletIndex", decoration: "flat", height: 1, width: 2) {
state "default", label:'Index: ${currentValue}'
}
valueTile("today", "device.forecastToday", decoration: "flat", height: 1, width: 3) {
state "default", label:'Today:\n${currentValue}'
}
valueTile("tonight", "device.forecastTonight", decoration: "flat", height: 1, width: 3) {
state "default", label:'Tonight:\n${currentValue}'
}
valueTile("tomorrow", "device.forecastTomorrow", decoration: "flat", height: 1, width: 3) {
state "default", label:'Tomorrow:\n${currentValue}'
}
valueTile("lastUpdate", "device.lastUpdate", decoration: "flat", height: 1, width: 3) {
state "default", label:'Last update:\n${currentValue}'
}
main(["temperature", "weatherIcon","feelsLike"])
details(["temperature", "feelsLike", "weatherIcon", "humidity", "wind",
"dewPoint", "pressureMb", "percentPrecip", "weather", "light",
"rise", "set",
"refresh", "uvDesc", "uvIndex", "station",
"today", "tonight", "tomorrow", "lastUpdate",
"alert"])}
}
// parse events into attributes
def parse(String description) {
log.debug "Parsing '${description}'"
}
def installed() {
poll()
runEvery30Minutes(poll)
}
def uninstalled() {
unschedule()
}
// handle commands
def poll() {
log.info "WUSTATION: Executing 'poll', location: ${location.name}"
if (stationId) {
pollUsingPwsId(stationId.toUpperCase())
pollUsingZipCode(zipCode.toUpperCase())
} else {
if (zipCode && zipCode.toUpperCase().startsWith('PWS:')) {
log.debug zipCode.substring(4)
pollUsingPwsId(zipCode.substring(4).toUpperCase())
pollUsingZipCode(zipCode.toUpperCase())
} else {
pollUsingZipCode(zipCode.toUpperCase())
}
}
}
def pollUsingZipCode(String zipCode) {
// Last update time stamp
def timeZone = location.timeZone ?: timeZone(timeOfDay)
def timeStamp = new Date().format("yyyy MMM dd EEE h:mm:ss a", location.timeZone)
sendEvent(name: "Getting Zipcode Info", value: timeStamp)
sendEvent(name: "lastUpdate", value: timeStamp)
// Current conditions
def tempUnits = getTemperatureScale()
def windUnits = tempUnits == "C" ? "KPH" : "MPH"
def obs = getTwcConditions(zipCode)
if (obs) {
// TODO def weatherIcon = obs.icon_url.split("/")[-1].split("\\.")[0]
// The below commented lines are retrieved in PWS
// Uncomment the next NINE lines if you want to use ONLY Zipcode
// send(name: "dewpoint", value: obs.temperatureDewPoint, unit: tempUnits)
// send(name: "pressuremb", value: obs.pressureAltimeter)
// sendEvent(name: "power", value: obs.temperatureDewPoint, unit: tempUnits)
// sendEvent(name: "voltage", value: obs.pressureAltimeter)
// send(name: "humidity", value: obs.relativeHumidity, unit: "%")
// send(name: "wind", value: obs.windSpeed as String, unit: windUnits) // as String because of bug in determining state change of 0 numbers
// send(name: "windVector", value: "${obs.windDirectionCardinal} ${obs.windSpeed} ${windUnits}")
send(name: "weather", value: obs.wxPhraseShort)
send(name: "weatherIcon", value: obs.iconCode as String, displayed: false)
log.trace "Getting location info"
def loc = getTwcLocation(zipCode).location
def cityValue = "${loc.city}, ${loc.adminDistrictCode} ${loc.countryCode}"
if (cityValue != device.currentValue("city")) {
send(name: "city", value: cityValue, isStateChange: true)
}
send(name: "ultravioletIndex", value: obs.uvIndex)
send(name: "uvDescription", value: obs.uvDescription)
def dtf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
def sunriseDate = dtf.parse(obs.sunriseTimeLocal)
log.info "'${obs.sunriseTimeLocal}'"
def sunsetDate = dtf.parse(obs.sunsetTimeLocal)
def tf = new java.text.SimpleDateFormat("h:mm a")
tf.setTimeZone(TimeZone.getTimeZone(loc.ianaTimeZone))
def localSunrise = "${tf.format(sunriseDate)}"
def localSunset = "${tf.format(sunsetDate)}"
send(name: "localSunrise", value: localSunrise, descriptionText: "Sunrise today is at $localSunrise")
send(name: "localSunset", value: localSunset, descriptionText: "Sunset today at is $localSunset")
send(name: "illuminance", value: estimateLux(obs, sunriseDate, sunsetDate))
// Forecast
def f = getTwcForecast(zipCode)
if (f) {
def icon = f.daypart[0].iconCode[0] ?: f.daypart[0].iconCode[1]
def value = f.daypart[0].precipChance[0] ?: f.daypart[0].precipChance[1]
def narrative = f.daypart[0].narrative
send(name: "percentPrecip", value: value, unit: "%")
send(name: "forecastIcon", value: icon, displayed: false)
send(name: "forecastToday", value: narrative[0])
send(name: "forecastTonight", value: narrative[1])
send(name: "forecastTomorrow", value: narrative[2])
}
else {
log.warn "Forecast not found"
}
// Alerts
def alerts = getTwcAlerts("${loc.latitude},${loc.longitude}")
if (alerts) {
alerts.each {alert ->
def msg = alert.headlineText
if (alert.effectiveTimeLocal && !msg.contains(" from ")) {
msg += " from ${parseAlertTime(alert.effectiveTimeLocal).format("E hh:mm a", TimeZone.getTimeZone(alert.effectiveTimeLocalTimeZone))}"
}
if (alert.expireTimeLocal && !msg.contains(" until ")) {
msg += " until ${parseAlertTime(alert.expireTimeLocal).format("E hh:mm a", TimeZone.getTimeZone(alert.expireTimeLocalTimeZone))}"
}
send(name: "alert", value: msg, descriptionText: msg)
}
}
else {
send(name: "alert", value: "No current alerts", descriptionText: msg)
}
}
else {
log.warn "No response from TWC API"
}
return null
}
def pollUsingPwsId(String stationId) {
// Last update time stamp
def timeZone = location.timeZone ?: timeZone(timeOfDay)
def timeStamp = new Date().format("yyyy MMM dd EEE h:mm:ss a", location.timeZone)
sendEvent(name: "Getting PWS Info", value: timeStamp)
sendEvent(name: "lastUpdate", value: timeStamp)
// Current conditions
def tempUnits = getTemperatureScale()
def windUnits = tempUnits == "C" ? "KPH" : "MPH"
def obsWrapper = getTwcPwsConditions(stationId)
log.debug obsWrapper
if (obsWrapper && obsWrapper.observations && obsWrapper.observations.size()) {
def obs = obsWrapper.observations[0]
def obsPWS = obsWrapper.observations[0].imperial
def dataScale = obs.imperial ? 'imperial' : 'metric'
send(name: "temperature", value: convertTemperature(obs[dataScale].temp, dataScale, tempUnits), unit: tempUnits)
// Uncomment line for Windchill during summer months and vice versa
// send(name: "feelsLike", value: convertTemperature(obs[dataScale].windChill, dataScale, tempUnits), unit: tempUnits)
send(name: "feelsLike", value: convertTemperature(obs[dataScale].heatIndex, dataScale, tempUnits), unit: tempUnits)
send(name: "dewpoint", value: convertTemperature(obs[dataScale].dewpt, dataScale, tempUnits), unit: tempUnits)
send(name: "humidity", value: obs.humidity, unit: "%")
send(name: "stationIdent", value: obs.stationID)
send(name: "pressuremb", value: obs[dataScale].pressure)
sendEvent(name: "voltage", value: obs[dataScale].pressure)
sendEvent(name: "power", value: convertTemperature(obs[dataScale].dewpt, dataScale, tempUnits), unit: tempUnits)
send(name: "wind", value: convertWindSpeed(obs[dataScale].windSpeed, dataScale, tempUnits) as String, unit: windUnits) // as String because of bug in determining state change of 0 numbers
send(name: "windVector", value: "${obs.winddir}° ${convertWindSpeed(obs[dataScale].windSpeed, dataScale, tempUnits)} ${windUnits}")
def cityValue = obs.neighborhood
if (cityValue != device.currentValue("city")) {
send(name: "city", value: cityValue, isStateChange: true)
}
// send(name: "ultravioletIndex", value: obs.uv)
// send(name: "uvDescription", value: "n/a")
// send(name: "localSunrise", value: "n/a", descriptionText: "Sunrise is not supported when using PWS")
// send(name: "localSunset", value: "n/a", descriptionText: "Sunset is not supported when using PWS")
// send(name: "illuminance", value: null)
// Forecast not supported
// send(name: "percentPrecip", value: "n/a", unit: "%")
// send(name: "forecastIcon", value: null, displayed: false)
// send(name: "forecastToday", value: "n/a")
// send(name: "forecastTonight", value: "n/a")
// send(name: "forecastTomorrow", value: "n/a")
// log.warn "Forecast not supported when using PWS"
// Alerts
def alerts = getTwcAlerts("${obs.lat},${obs.lon}")
if (alerts) {
alerts.each {alert ->
def msg = alert.headlineText
if (alert.effectiveTimeLocal && !msg.contains(" from ")) {
msg += " from ${parseAlertTime(alert.effectiveTimeLocal).format("E hh:mm a", TimeZone.getTimeZone(alert.effectiveTimeLocalTimeZone))}"
}
if (alert.expireTimeLocal && !msg.contains(" until ")) {
msg += " until ${parseAlertTime(alert.expireTimeLocal).format("E hh:mm a", TimeZone.getTimeZone(alert.expireTimeLocalTimeZone))}"
}
send(name: "alert", value: msg, descriptionText: msg)
}
}
else {
send(name: "alert", value: "No current alerts", descriptionText: msg)
}
}
else {
log.warn "No response from TWC API"
}
return null
}
def parseAlertTime(s) {
def dtf = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
def s2 = s.replaceAll(/([0-9][0-9]):([0-9][0-9])$/,'$1$2')
dtf.parse(s2)
}
def refresh() {
poll()
}
def configure() {
poll()
}
private pad(String s, size = 25) {
def n = (size - s.size()) / 2
if (n > 0) {
def sb = ""
n.times {sb += " "}
sb += s
n.times {sb += " "}
return sb
}
else {
return s
}
}
private get(feature) {
getWeatherFeature(feature, zipCode)
}
private localDate(timeZone) {
def df = new java.text.SimpleDateFormat("yyyy-MM-dd")
df.setTimeZone(TimeZone.getTimeZone(timeZone))
df.format(new Date())
}
private send(map) {
log.debug "WUSTATION: event: $map"
sendEvent(map)
}
private estimateLux(obs, sunriseDate, sunsetDate) {
def lux = 0
if (obs.dayOrNight == 'N') {
lux = 10
}
else {
//day
switch(obs.iconCode) {
case '04':
lux = 200
break
case ['05', '06', '07', '08', '09', '10',
'11', '12', '13','14', '15','17','18','19','20',
'21','22','23','24','25','26']:
lux = 1000
break
case ['27', '28']:
lux = 2500
break
case ['29', '30']:
lux = 7500
break
default:
//sunny, clear
lux = 10000
}
//adjust for dusk/dawn
def now = new Date().time
def afterSunrise = now - sunriseDate.time
def beforeSunset = sunsetDate.time - now
def oneHour = 1000 * 60 * 60
if(afterSunrise < oneHour) {
//dawn
lux = (long)(lux * (afterSunrise/oneHour))
} else if (beforeSunset < oneHour) {
//dusk
lux = (long)(lux * (beforeSunset/oneHour))
}
}
lux
}
private fixScale(scale) {
switch (scale.toLowerCase()) {
case "c":
case "metric":
return "metric"
default:
return "imperial"
}
}
private convertTemperature(value, fromScale, toScale) {
def fs = fixScale(fromScale)
def ts = fixScale(toScale)
if (fs == ts) {
return value
}
if (ts == 'imperial') {
return value * 9.0 / 5.0 + 32.0
}
return (value - 32.0) * 5.0 / 9.0
}
private convertWindSpeed(value, fromScale, toScale) {
def fs = fixScale(fromScale)
def ts = fixScale(toScale)
if (fs == ts) {
return value
}
if (ts == 'imperial') {
return value * 1.608
}
return value / 1.608
}