Option Explicit ' Relay controlling the humidifiers. It will be closed to enable humidifiers and opened to disable them ' First relay is 1. Zero means it is not configured and this script will do nothing Dim humidifiersRelay humidifiersRelay = 1 ' ' Optional parameters (have a default value) ' ' If the difference between Dew Point and Ambient Temperature is less than this value ' the relay specified in "humidifiersRelay" will be closed Dim humOnMargin humOnMargin = 4 ' If the difference between Dew Point and Ambient Temperature is bigger than this value ' the relay specified in "humidifiersRelay" will be open Dim humOffMargin humOffMargin = 6 ' How often (seconds) weather data is retrieved. Once a minute should be good enough Dim requestInterval requestInterval = 60 ' Maximum allowed difference between current date and weather data date Dim maxSecondsGap maxSecondsGap = 300 ' ' DO NOT EDIT FROM THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING ' Dim df Set df = CreateObject("Dragonfly.Help") df.LogShow Sub Log(str) df.LogAddLine("Humidity Control: " & str) End Sub Function ToDouble(str) On Error Resume Next ToDouble = CDbl(str) If (Err.Number <> 0) Then ToDouble = "NaN" End If End Function Function ReadFromRegistry(strRegistryKey, strDefault) Dim WSHShell, value On Error Resume Next Set WSHShell = CreateObject("WScript.Shell") value = WSHShell.RegRead( strRegistryKey ) If err.number <> 0 Then readFromRegistry = strDefault Else readFromRegistry = value End If Set WSHShell = Nothing End Function Function GetJsonAttribute(json, attributeName) On Error Resume Next Dim re, match, attributeValue Set re = New RegExp re.Global = True re.IgnoreCase = True re.Pattern = """" & attributeName & """\s*:\s*(?:""([^""]*)""|(\d+\.?\d*))" Set match = re.Execute(json) If match.Count > 0 Then attributeValue = match.Item(0).SubMatches.Item(0) If (Len(attributeValue) = 0) Then attributeValue = match.Item(0).SubMatches.Item(1) End If Else attributeValue = "" End If GetJsonAttribute = attributeValue End Function Function ParseDate(dateString) On Error Resume Next Dim parts parts = Split(dateString, " ") Dim dateParts dateParts = Split(parts(0), "/") Dim timeParts timeParts = Split(parts(1), ":") ParseDate = DateSerial(CInt(dateParts(0)), CInt(dateParts(1)), CInt(dateParts(2))) + TimeSerial(timeParts(0), timeParts(1), timeParts(2)) If Err.Number <> 0 Then Log("Invalid Date '" & dateDiff & "'") ParseDate = DateSerial(1970, 1, 1) End If End Function Function ReadWeatherData On Error Resume Next Dim filePath, fso, file, contents, dateDiff filePath = ReadfromRegistry("HKEY_CURRENT_USER\SOFTWARE\VB and VBA Program Settings\AAG_CloudWatcher\Window\PathName", "") If (Len(filePath) <= 0) Then Log("Cannot find file path in the registry. Quitting") WScript.Quit(1) End If filePath = filePath & "\aag_json.dat" Log("Reading weather data from '" & filePath & "'") Set fso = WScript.CreateObject("Scripting.FileSystemObject") Set file = fso.OpenTextFile(filePath, 1) contents = file.ReadAll file.Close Set file = Nothing Set fso = Nothing If (Len(contents) = 0) Then Log("Error reading file '" & filePath & "'") ReadWeatherData = "" Exit Function End If dateDiff = Now() - ParseDate(GetJsonAttribute(contents, "dateLocalTime")) If (dateDiff >= 1) Then Log("Weather data more than 1 day old") ReadWeatherData = "" Exit Function End If dateDiff = dateDiff * 24 * 3600 ' (24 * 3600) = Number of seconds in one day If (dateDiff > maxSecondsGap) Then Log("Weather data too old: " & dateDiff & " seconds") ReadWeatherData = "" Exit Function End If ReadWeatherData = contents End Function Sub CheckDewPoint(json) Dim temperature temperature = ToDouble(GetJsonAttribute(json, "temp")) Dim dewPoint dewPoint = ToDouble(GetJsonAttribute(json, "dewp")) If (Not IsNumeric(temperature) Or Not IsNumeric(dewPoint)) Then Log("Error reading temperature or dew point. Switching OFF humidifiers") df.RelayOpen(humidifiersRelay) Exit Sub End If Log("Temperature: " & temperature & " vs Dew Point:" & dewPoint) Dim temperatureDifference temperatureDifference = Abs(temperature - dewPoint) If (temperatureDifference > humOffMargin) then Log("Switching OFF humidifiers in relay #" & humidifiersRelay) df.RelayOpen(humidifiersRelay) ElseIf (temperatureDifference < humOnMargin) then Log("Switching ON humidifiers in relay #" & humidifiersRelay) df.RelayClose(humidifiersRelay) End If End Sub If (humidifiersRelay < 1) Then WScript.Echo("Humidifiers relay is not configured. Exiting") WScript.Quit(1) End if Dim doLoop doLoop = True If (WScript.Arguments.Count > 0) Then If (WScript.Arguments(0) = "/once") Then Log("Only running once") doLoop = False End If End If Dim json Do json = ReadWeatherData If (Len(json) > 0) Then CheckDewPoint(json) End If If (doLoop) Then Log("Waiting " & requestInterval & " seconds for next reading") WScript.Sleep(requestInterval * 1000) End If Loop While(doLoop)