' Script created by Rutger Bockholts and is provided on https://rutg3r.com for free. ' Version 1.0 // May 05, 2020 ' Data source: buienradar.nl / https://data.buienradar.nl/2.0/feed/json ' Locate your nearby weather station here: https://www.google.com/maps/d/u/0/embed?mid=1NivHkTGQUOs0dwQTnTMZi8Uatj0&ll=52.92957401169082%2C5.184999999999945&z=7 ' Fill in the number behind the station name, in the "BRstation" variable on line 15 of this script Imports System.Net Imports System.Text Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq ' ********** START USER CHANGE ********** Public Shared Debug as Boolean = true ' True/False Public Shared CreateDevices as Boolean = true ' True/False Public Shared BRstation = "6391" 'Arcen Public Shared stationcity as String ' ********** END USER CHANGE ********** Public Shared Dim dv as Scheduler.Classes.DeviceClass = Nothing Public Shared Dim dvp as Scheduler.Classes.DeviceClass = Nothing Sub Main(parm as object) 'Sub Main(ByVal Parm As Object) Try Dim conn as MySqlConnection Dim conn1 as MySqlConnection Dim myCommand as New MySqlCommand Dim myCommand1 as New MySqlCommand Dim obj as New Object ' Variables for creating devices Dim DevExists Dim BaseRef as Integer = 0 Dim root_dv as Scheduler.Classes.DeviceClass = Nothing Dim DVaddress as String = "BR" Dim DevicesArrayK as New ArrayList ' Create array for all Keys Dim DevicesArrayV as New ArrayList ' Create array for all Values Dim mysql_columnnames as String = "" Dim insert_values as String = "" ' https://data.buienradar.nl/2.0/feed/json Dim loaddata = "https://data.buienradar.nl/2.0/feed/json" System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12 Dim dataresult = hs.URLAction(loaddata, "GET", "", "") obj = JsonConvert.DeserializeObject(dataresult) Dim wordcount as String() = dataresult.Split("$") If Debug = True Then hs.writelog("wordcount", wordcount.Length) ' Grab data from chosen weather station Dim s As JObject = JObject.Parse(dataresult) For Each item As JProperty In s.Item("actual") Dim itemObjects As JToken = item.Value Dim ps For Each m As JObject In itemObjects ' Gather data from the predefined weather station If (m.ToString.Contains(BRstation)) Then For Each ps In m If (ps.Key.ToString.Contains("$id")) Then Continue For Else DevicesArrayK.Add(ps.Key.ToString) DevicesArrayV.Add(ps.Value.ToString) End If ' Write Regio name in variable for use during device creation If (ps.Key.ToString.Contains("regio")) Then stationcity = ps.Value.ToString End If Next Exit For End If Next Next ' Weather Report Text Dim wreport_summary as String = obj.item("forecast").item("weatherreport").item("summary").ToString If Debug = True Then hs.writelog("wreport_summary", wreport_summary) DevicesArrayK.Add("Report Summary") DevicesArrayV.Add(wreport_summary) ' Weather Short Term Text Dim wreport_short as String = obj.item("forecast").item("shortterm").item("forecast").ToString If Debug = True Then hs.writelog("wreport_short", wreport_short) DevicesArrayK.Add("Report Short Term") DevicesArrayV.Add(wreport_short) ' Weather Long Term Text Dim wreport_long as String = obj.item("forecast").item("longterm").item("forecast").ToString If Debug = True Then hs.writelog("wreport_long", wreport_long) DevicesArrayK.Add("Report Long Term") DevicesArrayV.Add(wreport_long) Dim daycountdata = obj.item("forecast").item("fivedayforecast").ToString If Debug = True Then hs.writelog("daycountdata", daycountdata) Dim daycount as String() = daycountdata.Split("$") If Debug = True Then hs.writelog("daycount", daycount.Length) Dim daynr as Integer = 0 For j as Byte = 0 To daycount.length - 2 daynr = j + 1 Dim forecastday as String = obj.item("forecast").item("fivedayforecast").item(j).item("day").ToString forecastday = forecastday.split(" ")(0) If Debug = True Then hs.writelog("forecastday" & daynr, forecastday) DevicesArrayK.Add("Forecast Day" & daynr) DevicesArrayV.Add(forecastday) Dim mintemperature as String = obj.item("forecast").item("fivedayforecast").item(j).item("mintemperature").ToString If Debug = True Then hs.writelog("mintemp" & daynr, mintemperature) DevicesArrayK.Add("Min Temperature Day" & daynr) DevicesArrayV.Add(mintemperature & " °C") Dim maxtemperature as String = obj.item("forecast").item("fivedayforecast").item(j).item("maxtemperature").ToString If Debug = True Then hs.writelog("maxtemp" & daynr, maxtemperature) DevicesArrayK.Add("Max Temperature Day" & daynr) DevicesArrayV.Add(maxtemperature & " °C") Dim mintemperatureMax as String = obj.item("forecast").item("fivedayforecast").item(j).item("mintemperatureMax").ToString If Debug = True Then hs.writelog("mintempMax" & daynr, mintemperatureMax) DevicesArrayK.Add("Min Temperature Max Day" & daynr) DevicesArrayV.Add(mintemperatureMax & " °C") Dim mintemperatureMin as String = obj.item("forecast").item("fivedayforecast").item(j).item("mintemperatureMin").ToString If Debug = True Then hs.writelog("mintempMin" & daynr, mintemperatureMin) DevicesArrayK.Add("Min Temperature Min Day" & daynr) DevicesArrayV.Add(mintemperatureMin & " °C") Dim maxtemperatureMax as String = obj.item("forecast").item("fivedayforecast").item(j).item("maxtemperatureMax").ToString If Debug = True Then hs.writelog("maxtempMax" & daynr, maxtemperatureMax) DevicesArrayK.Add("Max Temperature Max Day" & daynr) DevicesArrayV.Add(maxtemperatureMax & " °C") Dim maxtemperatureMin as String = obj.item("forecast").item("fivedayforecast").item(j).item("maxtemperatureMin").ToString If Debug = True Then hs.writelog("maxtempMin" & daynr, maxtemperatureMin) DevicesArrayK.Add("Max Temperature Min Day" & daynr) DevicesArrayV.Add(maxtemperatureMin & " °C") Dim rainChance as String = obj.item("forecast").item("fivedayforecast").item(j).item("rainChance").ToString If Debug = True Then hs.writelog("rainChance" & daynr, rainChance) DevicesArrayK.Add("Rain Chance Day" & j) DevicesArrayV.Add(rainChance & " %") Dim sunChance as String = obj.item("forecast").item("fivedayforecast").item(j).item("sunChance").ToString If Debug = True Then hs.writelog("sunChance" & daynr, sunChance) DevicesArrayK.Add("Sun Chance Day" & daynr) DevicesArrayV.Add(sunChance & " %") Dim windDirection as String = obj.item("forecast").item("fivedayforecast").item(j).item("windDirection").ToString windDirection = windDirection.ToUpper() If Debug = True Then hs.writelog("windDirection" & daynr, windDirection) DevicesArrayK.Add("Wind Direction Day" & daynr) DevicesArrayV.Add(windDirection) Dim wind as String = obj.item("forecast").item("fivedayforecast").item(j).item("wind").ToString wind = wind.ToUpper() If Debug = True Then hs.writelog("wind" & daynr, wind) DevicesArrayK.Add("Wind Day" & daynr) DevicesArrayV.Add(wind) Dim mmRainMin as String = obj.item("forecast").item("fivedayforecast").item(j).item("mmRainMin").ToString If Debug = True Then hs.writelog("mmRainMin" & daynr, mmRainMin) DevicesArrayK.Add("mm Rain Min Day" & daynr) DevicesArrayV.Add(mmRainMin & " mm") Dim mmRainMax as String = obj.item("forecast").item("fivedayforecast").item(j).item("mmRainMax").ToString If Debug = True Then hs.writelog("mmRainMax" & daynr, mmRainMax) DevicesArrayK.Add("mm Rain Max Day" & daynr) DevicesArrayV.Add(mmRainMax & " mm") Dim weatherdescription as String = obj.item("forecast").item("fivedayforecast").item(j).item("weatherdescription").ToString If Debug = True Then hs.writelog("weatherdesc" & daynr, weatherdescription) DevicesArrayK.Add("Weather Description Day" & daynr) DevicesArrayV.Add(weatherdescription) Dim iconurl as String = obj.item("forecast").item("fivedayforecast").item(j).item("iconurl").ToString If Debug = True Then hs.writelog("iconurl" & daynr, iconurl) DevicesArrayK.Add("Icon Url Day" & daynr) DevicesArrayV.Add(iconurl) Next If createdevices = True DevExists = hs.DeviceExistsAddress(DVaddress & "_Root Device", False) If Debug = True Then hs.writelog("DevExists?", "Root device ref ID: " & DevExists) If DevExists = -1 ' Create all child devices For i as Byte = 0 To DevicesArrayK.Count - 1 Dim DeviceArrayEname as String = DevicesArrayK(i) Select Case i Case 0 ' Create Root Device dv = hs.GetDeviceByRef(hs.NewDeviceRef(DVaddress & "_Root Device")) dv.Address(hs) = DVaddress & "_Root Device" BaseRef = dv.Ref(hs) hs.SetDeviceValueByRef(dv.Ref(hs), DevicesArrayK.Count - 1, True) hs.SetDeviceString(dv.Ref(hs), "No Status", True) Case 1 To DevicesArrayK.Count - 1 ' Create child devices dv = hs.GetDeviceByRef(hs.NewDeviceRef(DVaddress & "_" & DevicesArrayK(i))) ' Fill Homeseer Name Column dv.Address(hs) = DVaddress & "_" & i & "_" & DevicesArrayK(i) ' Fill Homeseer DVaddress+name in Address column. Counter(i) added for descent sorting End Select dv.Location2(hs) = "Buienradar" & "_" & stationcity 'Floor dv.location(hs) = "Buienradar" 'Room dv.Device_Type_String(hs) = DVAddress ' Set up the relationships between the devices If i = 0 Then root_dv = dv dv.Relationship(hs) = Enums.eRelationship.Parent_Root Elseif DevicesArrayK(i).Contains("Switch Status") Then If root_dv IsNot Nothing Then root_dv.AssociatedDevice_Add(hs, dv.Ref(hs)) dv.Relationship(hs) = Enums.eRelationship.Child dv.AssociatedDevice_Add(hs, BaseRef) hs.SaveEventsDevices ' DV.Status_Support(hs) = False ' DV.Can_Dim(hs) = False DV.MISC_Set(hs, Enums.dvMISC.SHOW_VALUES) 'This is &H100 ' DV.MISC_Clear(hs, Enums.dvMISC.STATUS_ONLY) 'This is &H10 ' DV = Nothing Else If root_dv IsNot Nothing Then root_dv.AssociatedDevice_Add(hs, dv.Ref(hs)) dv.Relationship(hs) = Enums.eRelationship.Child dv.AssociatedDevice_Add(hs, BaseRef) End If ' Extend virtual devices with icons, value ranges etc Select Case i Case 0 Dim SPair as VSPair Dim GPair as VGPair GPair = New VGPair GPair.PairType = VSVGPairType.Range GPair.RangeStart = -99999999999 GPair.RangeEnd = 99999999999 GPair.Graphic = "/images/HomeSeer/5-x-5_trans_spacer.gif" hs.DeviceVGP_AddPair(dv.Ref(hs), GPair) Case else Dim SPair as VSPair Dim GPair as VGPair GPair = New VGPair GPair.PairType = VSVGPairType.Range GPair.RangeStart = -99999999999 GPair.RangeEnd = 99999999999 GPair.Graphic = "/images/HomeSeer/5-x-5_trans_spacer.gif" hs.DeviceVGP_AddPair(dv.Ref(hs), GPair) End Select Next End If ' End creating devices and events ' Update virtual devices For i as Byte = 0 To DevicesArrayK.Count - 1 If Debug = true Then hs.writelog("Upd Array: "&i, DevicesArrayK(i) & " - " & DevicesArrayV(i)) Dim update = hs.GetDeviceRefByName(DVaddress & "_" & DevicesArrayK(i)) ' If Debug = true Then hs.writelog("updateVal", update) ' Check if array key is already available as virtual device. If not, than create it. ' If Debug = true Then hs.writelog("To Update", DVaddress & "_" & DevicesArrayK(i)) If update = -1 ' Virtual device doesn't exists ' In the Root device, the total device counter value is stored. Based on this number, the new devices will get the increased followup number Dim RootCounterRef = hs.DeviceExistsAddress(DVaddress & "_Root Device", False) Dim RootCounter as Integer If RootCounterRef > 0 RootCounter = hs.Devicevalue(RootCounterRef) + 1 If Debug = true Then hs.writelog("RootCounter", RootCounter) End If If Debug = true Then hs.writelog("DevNotExists", DVaddress & "_" & DevicesArrayK(i)) ' If NOT DevicesArrayK(i).Contains("Test") ' Create child devices dv = hs.GetDeviceByRef(hs.NewDeviceRef(DVaddress & "_" & DevicesArrayK(i))) ' Fill Homeseer Name Column dv.Address(hs) = DVaddress & "_" & RootCounter & "_" & DevicesArrayK(i) ' Fill Homeseer DVaddress+name in Address column. Counter(i) added for descent sorting dv.Location2(hs) = "Buienradar" & "_" & stationcity 'Floor dv.location(hs) = "Buienradar" 'Room dv.Device_Type_String(hs) = DVAddress If root_dv IsNot Nothing Then root_dv.AssociatedDevice_Add(hs, dv.Ref(hs)) dv.Relationship(hs) = Enums.eRelationship.Child dv.AssociatedDevice_Add(hs, BaseRef) Dim dvRoot = hs.GetDeviceRefByName(DVaddress & "_Root Device") dvp = hs.GetDeviceByRef(dvRoot) ' Relate new child devices to Root device dvp.AssociatedDevice_Add(hs, dv.Ref(hs)) dv.AssociatedDevice_ClearAll(hs) dv.AssociatedDevice_Add(hs, dvp.Ref(hs)) hs.SaveEventsDevices() ' Extend virtual devices with icons, value ranges etc Dim SPair as VSPair Dim GPair as VGPair GPair = New VGPair GPair.PairType = VSVGPairType.Range GPair.RangeStart = -99999999999 GPair.RangeEnd = 99999999999 GPair.Graphic = "/images/HomeSeer/5-x-5_trans_spacer.gif" hs.DeviceVGP_AddPair(dv.Ref(hs), GPair) 'Save RootCounter in Root device hs.SetDeviceValueByRef(RootCounterRef, RootCounter, true) End If ' Update values Dim inttest as Integer Dim doubletest as Double Try ' True when value is an Integer inttest = Convert.ToInt32(DevicesArrayV(i)) hs.SetDeviceValueByRef(update, DevicesArrayV(i), true) hs.SetDeviceString(update, DevicesArrayV(i), true) Catch ' Value isn't an Integer hs.SetDeviceString(update, DevicesArrayV(i), true) End Try Try ' True when value is a Double doubletest = Convert.ToDouble(DevicesArrayV(i)) hs.SetDeviceValueByRef(update, DevicesArrayV(i), true) Catch ' Value isn't a Double hs.SetDeviceString(update, DevicesArrayV(i), true) End Try Next End If DevicesArrayK.Clear() DevicesArrayV.Clear() DVAddress = Nothing Catch ex As Exception If Debug = true then hs.writelog("BR-Weather", ex.ToString) End Try End Sub