- Check if Rain Current of Rain Predicted is right to open or close the sunscreen
Code: Select all
-----------------------------------------------------------------------------
-- LUA Script
-- Extract values from WU device
-- Auteur: Luuc Alarm
-- Datum : 29-05-2014
-----------------------------------------------------------------------------
-- Setup uservariables:
-- 1) Weather_Debug = String = True / False
-- 2) Weather_WindSpeedThreshold = integer
-- 3) Weather_WindGustThreshold = integer
-- 4) Weather_RainThreshold = integer
-- 5) Weather_RainPredThreshold = integer
-- 6) Weather_UVThreshold = integer
-- 7) Weather_SolarThreshold = integer
-- 8) Weather_TempThreshold = integer
-- Create Switches:
-- 1) Regen = Dummy Humiditysensor
-- 2) WindSnelheid = Dummy Text
-- 3) WindRichting = Dummy Text
-- 4) Barometer = Weather Underground/Forecast IO
-- 5) Wind = Weather Underground/Forecast IO
-- 6) Regen = Weather Underground/Forecast IO
-- 7) Zon = Weather Underground/Forecast IO
-- 8) Zonnescherm = Up/Down switch
-- 9) Zonnescherm manual override = Dummy On/Off switch
-----------------------------------------------------------------------------
commandArray = {}
-- tijd ophalen
time = os.date("*t")
------------------------------------------------------------------------
-- Variabelen
lat='52.025526' -- example lat/lon for Gouda
lon='4.692806'
sDebug = tostring(uservariables["Weather_Debug"])
UVThreshold = uservariables["Weather_UVThreshold"]
SolarThreshold = uservariables["Weather_SolarThreshold"]
WindSpeedThreshold = uservariables["Weather_WindSpeedThreshold"]
WindGustThreshold = uservariables["Weather_WindGustThreshold"]
RainThreshold = uservariables["Weather_RainThreshold"]
RainPredThreshold = uservariables["Weather_RainPredThreshold"]
TempThreshold = uservariables["Weather_TempThreshold"]
tempfilename = '/var/tmp/rain.tmp' -- can be anywhere writeable
idx = 220 --idx regen
idx2 = 221 -- idx windrichting
idx3 = 222 -- idx windsnelheid
minuten = 5 -- time within rainprediction
sBarometer = 'Barometer Gouda'
sWind = 'Wind Gouda'
sRain = 'Regen Gouda'
sUV = 'UV Gouda'
sSolar = 'Zon Gouda'
------------------------------------------------------------------------
--Sunscreen thresholds:
-- Windspeed = 100.0 (value is multiplied by 100)
-- Rain = 0.0
-- UV = 2.0
-- Temperature = 12.0
-- Closed = Down = On
-- Open = Up = Off
------------------------------------------------------------------------
-- Functies
function Message(fMessage, fDebug)
if (fDebug == "True") then
print("Weather: " .. fMessage)
end
end
function Notification(fNotification, fDebug)
if (fDebug == "False") then
commandArray['SendNotification'] = "" .. fNotification .. ""
end
end
function IsItGonnaRain( minutesinfuture )
url='http://gps.buienradar.nl/getrr.php?lat='..lat..'&lon='..lon
Message("-- "..url,sDebug)
read = os.execute('curl -s -o '..tempfilename..' "'..url..'"')
file = io.open(tempfilename, "r")
totalrain=0
rainlines=0
-- now analyse the received lines, format is like 000|15:30 per line.
while true do
line = file:read("*line")
if not line then break end
Message("-- Line:" ..line,sDebug)
linetime=string.sub(tostring(line), 5, 9)
Message("-- Linetime: "..linetime,sDebug)
-- Linetime2 holds the full date calculated from the time on a line
linetime2 = os.time{year=os.date('%Y'), month=os.date('%m'), day=os.date('%d'), hour=string.sub(linetime,1,2), min=string.sub(linetime,4,5), sec=os.date('%S')}
difference = os.difftime (linetime2,os.time())
-- When a line entry has a time in the future AND is in the given range, then totalize the rainfall
if ((difference > 0) and (difference<=minutesinfuture*60)) then
Message("-- Line in time range found",sDebug)
rain=tonumber(string.sub(tostring(line), 0, 3))
totalrain = totalrain+rain
rainlines=rainlines+1
Message("-- Rain in timerange: "..rain,sDebug)
Message("-- Total rain now: "..totalrain,sDebug)
end
end
file:close()
averagerain=totalrain/rainlines
return(averagerain)
end
regen = IsItGonnaRain(minuten)
Message("-- Regen verwacht: "..regen.." binnen "..minuten.." minuten.",sDebug)
if (regen > 0 ) then verw = 3 else verw = 2 end
--Weatherstation data:
sWeatherTemp, sWeatherHumidity, sWeatherUV, sWeatherPressure, sWeatherUV2 = otherdevices_svalues[sBarometer]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWeatherTemp = tonumber(sWeatherTemp);
sWeatherHumidity = tonumber(sWeatherHumidity);
sWeatherPressure = tonumber(sWeatherPressure);
sWeatherUV = tonumber(sWeatherUV);
sWeatherUV2 = tonumber(sWeatherUV2);
Message("______________________________________________________________________________________",sDebug)
Message("Weather station: Temperature is " .. sWeatherTemp .. " ",sDebug)
Message("Weather station: Humidity is " .. sWeatherHumidity .. " ",sDebug)
Message("Weather station: Pressure is " .. sWeatherPressure .. " ",sDebug)
Message("Weather station: UV is " .. sWeatherUV .. " ",sDebug)
Message("Weather station: UV2 is " .. sWeatherUV2 .. " ",sDebug)
Message("______________________________________________________________________________________",sDebug)
------------------------------------------------------------------------
--Windmeter data:
sWindDirectionDegrees, sWindDirection, sWindSpeed, sWindGust, sWindTemperature, sWindFeel = otherdevices_svalues[sWind]:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
sWindDirectionDegrees = tonumber(sWindDirectionDegrees);
sWindDirection = (sWindDirection);
sWindSpeed = tonumber(sWindSpeed);
sWindGust = tonumber(sWindGust);
sWindTemperature = tonumber(sWindTemperature);
sWindFeel = tonumber(sWindFeel);
Message("______________________________________________________________________________________",sDebug)
Message("Windmeter: Winddirection (in degrees) is: " .. sWindDirectionDegrees .. " ",sDebug)
Message("Windmeter: Winddirection is: " .. sWindDirection .. " ",sDebug)
Message("Windmeter: Windspeed is: " .. sWindSpeed .. " ",sDebug)
Message("Windmeter: Windgust is: " .. sWindGust .. " ",sDebug)
Message("Windmeter: Windtemperature is: " .. sWindTemperature .. " ",sDebug)
Message("Windmeter: Windfeel is: " .. sWindFeel .. " ",sDebug)
Message("______________________________________________________________________________________")
------------------------------------------------------------------------
--Rainmeter data:
sRainmeterCurrent, sRainmeterTotal = otherdevices_svalues[sRain]:match("([^;]+);([^;]+)")
sRainmeterCurrent = tonumber(sRainmeterCurrent);
sRainmeterTotal = tonumber(sRainmeterTotal);
Message("______________________________________________________________________________________",sDebug)
Message("Rainmeter: Actual rain is: " .. sRainmeterCurrent .. " ",sDebug)
Message("Rainmeter: Total rain is: " .. sRainmeterTotal .. " ",sDebug)
Message("Rainmeter: Predicted rain is: " .. regen .. " ",sDebug)
Message("______________________________________________________________________________________",sDebug)
------------------------------------------------------------------------
--UV data:
sUV = otherdevices_svalues[sUV]:match("([^;]+)")
sSolar = otherdevices_svalues[sSolar]:match("([^;]+)")
sUV = tonumber(sUV);
sSolar = tonumber(sSolar);
Message("UV: UV Strength is: " .. sUV .." ",sDebug)
Message("UV: Solar radiation is: " .. sSolar .." ",sDebug)
--Thresholds:
Message("______________________________________________________________________________________",sDebug)
Message("Thresholds: Temp threshold is: " .. TempThreshold .." ",sDebug)
Message("Thresholds: UV threshold is: " .. UVThreshold .." ",sDebug)
Message("Thresholds: Solar threshold is: " .. SolarThreshold .." ",sDebug)
Message("Thresholds: WindSpeed threshold is: " .. WindSpeedThreshold .." ",sDebug)
Message("Thresholds: WindGust threshold is: " .. WindGustThreshold .." ",sDebug)
Message("Thresholds: Rain threshold is: " .. RainThreshold .." ",sDebug)
Message("Thresholds: Rain Prediction threshold is: " .. RainPredThreshold .." ",sDebug)
Message("______________________________________________________________________________________",sDebug)
Message("Override: ".. otherdevices['Zonnescherm manual override'],sDebug)
Message("______________________________________________________________________________________",sDebug)
if (timeofday['Daytime']) then
Message('It is past sunrise, monitoring variables for sunscreen',sDebug)
if (otherdevices['Zonnescherm'] == 'Open' ) then
Message ('Sunscreen is up',sDebug)
else
Message ('Sunscreen is down',sDebug)
end
if (otherdevices['Zonnescherm'] == 'Open'
and sRainmeterCurrent == RainThreshold
and sWeatherTemp > TempThreshold
and (sUV > UVThreshold or sSolar > SolarThreshold)
and sWindSpeed < WindSpeedThreshold
and sWindGust < WindGustThreshold)
then
Message ('Sun is shining, all thresholds OK, lowering sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen down --> Sun is shining',sDebug)
commandArray['Zonnescherm']='On'
elseif (otherdevices['Zonnescherm'] == 'Closed' and (sRainmeterCurrent > RainThreshold or regen > RainPredThreshold)) then
Message ('It is raining, raising sunscreen')
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> It is raining',sDebug)
commandArray['Zonnescherm']='Off'
elseif (otherdevices['Zonnescherm'] == 'Closed' and sWeatherTemp < TempThreshold-1 and otherdevices['Zonnescherm manual override'] == 'Off') then
Message ('Temperature too low, raising sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> It is too cold',sDebug)
commandArray['Zonnescherm']='Off'
elseif (otherdevices['Zonnescherm'] == 'Closed' and (sUV < UVThreshold-1 or sSolar < SolarThreshold) and otherdevices['Zonnescherm manual override'] == 'Off') then
Message ('Sun not shining too bright, raising sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> Sunshine not too bright',sDebug)
commandArray['Zonnescherm']='Off'
elseif (otherdevices['Zonnescherm'] == 'Closed' and sWindSpeed > WindSpeedThreshold) then
Message ('Windspeed too high, raising sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> Windspeed too high',sDebug)
commandArray['Zonnescherm']='Off'
elseif (otherdevices['Zonnescherm'] == 'Closed' and sWindGust > WindGustThreshold) then
Message ('Windgust too high, raising sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> Windgust too high',sDebug)
commandArray['Zonnescherm']='Off'
else
Message('Sunscreen status OK --> No action',sDebug)
Message('______________________________________________________________________________________',sDebug)
end
::done::
Message("At the end of the script",sDebug)
Message("______________________________________________________________________________________",sDebug)
elseif (timeofday['Nighttime'] and otherdevices['Zonnescherm'] == 'Closed') then
Message('It is night, raising sunscreen',sDebug)
Message('______________________________________________________________________________________',sDebug)
Notification('Sunscreen#Sunscreen up --> It is night',sDebug)
commandArray['Zonnescherm']='Off'
else
Message('Sunscreen already up --> No action',sDebug)
Message('______________________________________________________________________________________',sDebug)
end
commandArray[1] = {['UpdateDevice'] = idx .. '|'..tostring(regen)..'|'..tostring(verw)}
commandArray[2] = {['UpdateDevice'] = idx2 .. '|0|'.. sWindDirection}
commandArray[3] = {['UpdateDevice'] = idx3 .. '|0|'.. sWindSpeed}
return CommandArray
For me this would be the latest version because in my case I can not trust on WU of Forecast IO.
When there is a clear blue sky WU sometimes think it's rain. As long as I don't have a dedicated weather station I will leave it like this. If anyone has any comments or want something else let me know and I will take a look into it.