Update turn-on-before-sunset.groovy
[smartapps.git] / official / wunderground-pws-connect.groovy
1 /**
2  *  Weather Underground PWS Connect
3  *
4  *  Copyright 2015 Andrew Mager
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
7  *  in compliance with the License. You may obtain a copy of the License at:
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
12  *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
13  *  for the specific language governing permissions and limitations under the License.
14  *
15  */
16  
17 import java.text.DecimalFormat
18  
19 definition(
20     name: "Weather Underground PWS Connect",
21     namespace: "co.mager",
22     author: "Andrew Mager",
23     description: "Connect your SmartSense Temp/Humidity sensor to your Weather Underground Personal Weather Station.",
24     category: "Green Living",
25     iconUrl: "http://i.imgur.com/HU0ANBp.png",
26     iconX2Url: "http://i.imgur.com/HU0ANBp.png",
27     iconX3Url: "http://i.imgur.com/HU0ANBp.png",
28     oauth: true)
29
30
31 preferences {
32     section("Select a sensor") {
33         input "temp", "capability.temperatureMeasurement", title: "Temperature", required: true
34         input "humidity", "capability.relativeHumidityMeasurement", title: "Humidity", required: true
35     }
36     section("Configure your Weather Underground credentials") {
37         input "weatherID", "text", title: "Weather Station ID", required: true
38         input "password", "password", title: "Weather Underground password", required: true
39
40     }
41 }
42
43 def installed() {
44     log.debug "Installed with settings: ${settings}"
45     initialize()
46 }
47
48
49 def updated() {
50     log.debug "Updated with settings: ${settings}"
51     initialize()
52 }
53
54
55 def initialize() {
56     
57     runEvery10Minutes(updateCurrentWeather)
58     
59     log.trace "Temp: " + temp.currentTemperature
60     log.trace "Humidity: " + humidity.currentHumidity
61     log.trace "Dew Point: " + calculateDewPoint(temp.currentTemperature, humidity.currentHumidity)
62
63 }
64
65
66 def updateCurrentWeather() {
67     
68     def params = [
69         uri: "http://weatherstation.wunderground.com",
70         path: "/weatherstation/updateweatherstation.php",
71         query: [
72             "ID": weatherID,
73             "PASSWORD": password,
74             "dateutc": "now",
75             "tempf": temp.currentTemperature,
76             "humidity": humidity.currentHumidity,
77             "dewptf": calculateDewPoint(temp.currentTemperature, humidity.currentHumidity),
78             "action": "updateraw",
79             "softwaretype": "SmartThings"
80         ]
81     ]
82     
83     if (temp.currentTemperature) {
84         try {
85             httpGet(params) { resp ->   
86                 log.debug "response data: ${resp.data}"
87             }
88         } catch (e) {
89             log.error "something went wrong: $e"
90         }
91     }
92
93 }
94
95
96 def calculateDewPoint(t, rh) {
97     def dp = 243.04 * ( Math.log(rh / 100) + ( (17.625 * t) / (243.04 + t) ) ) / (17.625 - Math.log(rh / 100) - ( (17.625 * t) / (243.04 + t) ) ) 
98     return new DecimalFormat("##.##").format(dp)
99 }