My script doesn't seem to work anymore. I'm not aware of any changes I've made. Things go wrong because variable
rain seems to have a value of
-nan.
Any clues anyone?
Cheers, Tinus
Code: Select all
2016-09-11 08:20:00.297 LUA: interval_min: 1
2016-09-11 08:20:00.297 LUA: minutes: 5
2016-09-11 08:20:00.297 LUA: threshold: 0.2
2016-09-11 08:20:00.297 LUA: rain: -nan
2016-09-11 08:20:00.297 LUA: rainavg: -nan
2016-09-11 08:20:00.297 Error: EventSystem: ../domoticz/scripts/lua/script_time_rain.lua:88: attempt to concatenate global 'rainmmh' (a nil value)
Code: Select all
function IsItGonnaRain( minutesinfuture )
-- config ---------------------------------------------------------
lat='52.0842083' -- lat/lon of your location
lon='5.0474724'
-- http://gps.buienradar.nl/getrr.php?lat=52.0842083&lon=5.0474724
debug=false
tempfilename = '/var/tmp/rain.tmp' -- can be anywhere writeable
-- /config ---------------------------------------------------------
url='http://gps.buienradar.nl/getrr.php?lat='..lat..'&lon='..lon
if debug then print(url) end
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
if debug then print('Line:'..line) end
linetime=string.sub(tostring(line), 5, 9)
if debug then print('Linetime: '..linetime) end
-- 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
if debug then print('Line in time range found') end
rain=tonumber(string.sub(tostring(line), 0, 3))
totalrain = totalrain+rain
rainlines=rainlines+1
if debug then print('Rain in timerange: '..rain) end
if debug then print('Total rain now: '..totalrain) end
end
end
file:close()
-- Returned value is average rain fall for next time
-- 0 is no rain, 255 is very heavy rain
-- When needed, mm/h is calculated by 10^((value -109)/32)
averagerain=totalrain/rainlines
return(averagerain)
end
-- function to round mm/h output
function round(num, idp)
return tonumber(string.format("%." .. (idp or 0) .. "f", num))
end
-- RAIN ACTION SCRIPT
commandArray = {}
-- time variables
interval_min = 5
time = os.date("*t")
if ((time.min % interval_min)==0) then
----- rain variables ------
minutes=5 -- minimum value=5
threshold=0.20
----- rain calculations ------
rain = IsItGonnaRain(minutes)
rainavg = 10^((rain-109)/32)
rainmmh = round(rainavg, 2)
print('interval_min: '..interval_min)
print('minutes: '..minutes)
print('threshold: '..threshold)
print('rain: '..rain)
print('rainavg: '..rainavg)
print('rainmmh: '..rainmmh)
----- rain actions ------
if rainmmh > threshold then
-- if (otherdevices['Luifel Patio'] ~= 'Open') then
print(rainmmh..' mm/h rain expected in '..minutes..' minutes → switch canopy off!')
commandArray['Luifel Patio']='Off'
-- else
-- print(rainmmh..' mm/h rain expected in '..minutes..' minutes, canopy is already off → all clear and dry!')
--end
else
print(rainmmh..' mm/h rain expected in '..minutes..' minute(s).')
end
end
return commandArray