The script below gets weather details and forecasts from YR (met.no) for pressure, wind, wind direction and rain forecast for the next 12, 24 and 48 hours.
I use it out of interest, and to control closing blinds based on 12h rain forecast.
Complete the TODO points - you need to put your location and identity into the request.
Then you need to create devices for the various measurements and put the references into the script (replacing the numbers 437-442 which match my system). If you don't want a measurement, just comment out the line that sets it.
I run the script every fifteen minutes.
See https://api.met.no/weatherapi/locati...a/get_complete for full API details.
Units are hPa, km/h, degrees and mm.
I use it out of interest, and to control closing blinds based on 12h rain forecast.
Complete the TODO points - you need to put your location and identity into the request.
Then you need to create devices for the various measurements and put the references into the script (replacing the numbers 437-442 which match my system). If you don't want a measurement, just comment out the line that sets it.
I run the script every fifteen minutes.
Code:
Imports System.Web Imports System.Net Imports System.IO Imports System.Text Imports Newtonsoft.Json.Linq Sub Main(args As object) ' Get JSON data from web ' TODO put your location in the URL parameters Dim url as String = "https://api.met.no/weatherapi/locationforecast/2.0/compact?altitude=350&lat=47.36676&lon=8.54171" Dim wRequest as HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) wRequest.Method = "GET" ' TODO put your email address in here wRequest.UserAgent = "" Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse) Dim sResponse As String Using srRead As New StreamReader(wResponse.GetResponseStream()) sResponse = srRead.ReadToEnd() End Using ' Parse JSON data Dim o as JObject = JObject.Parse(sResponse) ' Get current values Dim offset as Integer = -1 Dim token as JToken Do offset = offset + 1 token = o.SelectToken("properties.timeseries[" & offset & "]") Loop Until token.Value(of DateTime)("time").ToLocalTime() > Now If offset > 0 Then offset = offset - 1 End If token = o.SelectToken("properties.timeseries[" & offset & "]") hs.WriteLog("Weather", "Current: " & token.Value(of DateTime)("time").ToLocalTime() & " (offset " & offset & ")") token = token.SelectToken("data.instant.details") Dim baro as Double = token.Value(of Double)("air_pressure_at_sea_level") Dim winddir as Double = token.Value(of Double)("wind_from_direction") Dim windspeed as Double = token.Value(of Double)("wind_speed") * 3.6 hs.WriteLog("Weather", "Barometer: " & baro & " Wind direction: " & winddir & " speed: " & windspeed) ' TODO update device numbers here hs.SetDeviceValueByRef(437, baro, True) hs.SetDeviceValueByRef(438, windspeed, True) hs.SetDeviceValueByRef(439, winddir, True) ' Get rain for next 12, 24 and 48 hours Dim rain12 as Double = 0.0 Dim rain24 as Double = 0.0 Dim rain48 as Double = 0.0 For i as Integer = 0 to 42 step 6 Dim rainslice as Double = o.SelectToken("properties.timeseries[" & (i+offset) & "].data.next_6_hours.details").Value(of Double)("precipitation_amount") If i <= 6 Then rain12 = rain12 + rainslice End If If i <= 18 Then rain24 = rain24 + rainslice End If rain48 = rain48 + rainslice Next hs.WriteLog("Weather", "Rain 12h: " & rain12 & " 24h: " & rain24 & " 48h: " & rain48) ' TODO update device numbers here hs.SetDeviceValueByRef(440, rain12, True) hs.SetDeviceValueByRef(441, rain24, True) hs.SetDeviceValueByRef(442, rain48, True) End Sub
Units are hPa, km/h, degrees and mm.
Comment