PROBLEM! Script running more then 10 seconds, please help
Posted: Wednesday 16 November 2016 21:28
I have created a script (combined with the isitgonnarain script) to check my windows if there is rain expected.
But its driving me nuts.. it comes regulary(not always) with the error message that it is running for more than 10 seconds.
Could somebody tell me how I can prevent that.
Please help.
regards Hans
But its driving me nuts.. it comes regulary(not always) with the error message that it is running for more than 10 seconds.
Could somebody tell me how I can prevent that.
Code: Select all
-- script_device_windowcheck.lua
----------------------------------------------------------------------------------------------------------
lat = 'XXX'
lon = 'XXX'
debug = false
tempfilenameRW = '/var/tmp/RainAmount.tmp' -- can be anywhere writeable
totalrain = 0
rainlines = 0
----------------------------------------------------------------------------------------------------------
-- Lua Functions
----------------------------------------------------------------------------------------------------------
function IsItGonnaRain( minutesinfuture )
url='http://gpsgadget.buienradar.nl/data/raintext/?lat='..lat..'&lon='..lon
if debug then print(url) end
read = os.execute('curl -s -o '..tempfilenameRW..' "'..url..'"')
file = io.open(tempfilenameRW, "r")
-- 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) (example: 77 = 0.1 mm/hour)
averagerain=totalrain/rainlines
return(averagerain)
end
function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
WindowsText = ''
local Window_1 = 'Window 1'
local Window_2 = 'Window 2'
local Window_3 = 'Window 3'
local Window_4 = 'Window 4'
local Window_5 = 'Window 5'
local Window_6 = 'Window 6'
local Windowvar = 0
if ((otherdevices[Window_1] == 'Open')) then Windowvar = Windowvar + 1 end
if ((otherdevices[Window_2] == 'Open') ) then Windowvar = Windowvar + 2 end
if ((otherdevices[Window_3] == 'Open') ) then Windowvar = Windowvar + 4 end
if ((otherdevices[Window_4] == 'Open') ) then Windowvar = Windowvar + 8 end
if ((otherdevices[Window_5] == 'Open') ) then Windowvar = Windowvar + 16 end
if ((otherdevices[Window_6] == 'Open') ) then Windowvar = Windowvar + 32 end
-- print('-- ' .. Windowvar )
if Windowvar == 0 then WindowsText = "All Windows are closed"
elseif Windowvar == 1 then WindowsText = "Window 1 is open"
elseif Windowvar == 2 then WindowsText = "Window 2 is open"
elseif Windowvar == 3 then WindowsText = "Windows 1 and 2 are open"
elseif Windowvar == 4 then WindowsText = "Window 3 is open"
elseif Windowvar == 5 then WindowsText = "Windows 1 and 3 are open"
elseif Windowvar == 6 then WindowsText = "Windows 2 and 3 are open"
elseif Windowvar == 7 then WindowsText = "Windows 1, 2 and 3 are open"
elseif Windowvar == 8 then WindowsText = "Window 4 is open"
elseif Windowvar == 9 then WindowsText = "Windows 1 and 4 are open"
elseif Windowvar == 10 then WindowsText = "Windows 2 and 4 are open"
elseif Windowvar == 11 then WindowsText = "Windows 1, 2 and 4 are open"
elseif Windowvar == 12 then WindowsText = "Windows 3 and 4 are open"
elseif Windowvar == 13 then WindowsText = "Windows 1, 3 and 4 are open"
elseif Windowvar == 14 then WindowsText = "Windows 2, 3 and 4 are open"
elseif Windowvar == 15 then WindowsText = "Windows 1, 2, 3 and 4 are open"
elseif Windowvar == 16 then WindowsText = "Window 5 is open"
elseif Windowvar == 17 then WindowsText = "Windows 1 and 5 are open"
elseif Windowvar == 18 then WindowsText = "Windows 2 and 5 are open"
elseif Windowvar == 19 then WindowsText = "Windows 1, 2 and 5 are open"
elseif Windowvar == 20 then WindowsText = "Windows 2, 3 and 5 are open"
elseif Windowvar == 21 then WindowsText = "Windows 1, 3 and 5 are open"
elseif Windowvar == 22 then WindowsText = "Windows 2, 3 and 5 are open"
elseif Windowvar == 23 then WindowsText = "Windows 1, 2, and 5 are open"
elseif Windowvar == 24 then WindowsText = "Windows 4 and 5 are open"
elseif Windowvar == 25 then WindowsText = "Windows 1, 4, and 5 are open"
elseif Windowvar == 26 then WindowsText = "Windows 2, 4, and 5 are open"
elseif Windowvar == 27 then WindowsText = "Windows 1, 2, 4 and 5 are open"
elseif Windowvar == 28 then WindowsText = "Windows 3, 4, and 5 are open"
elseif Windowvar == 29 then WindowsText = "Windows 1, 3, 4 and 5 are open"
elseif Windowvar == 30 then WindowsText = "Windows 2, 3, 4 and 5 are open"
elseif Windowvar == 31 then WindowsText = "Windows 1, 2, 3, 4 and 5 are open"
elseif Windowvar == 32 then WindowsText = "Window 6 is open"
elseif Windowvar == 33 then WindowsText = "Windows 1 and 6 are open"
elseif Windowvar == 34 then WindowsText = "Windows 2 and 6 are open"
elseif Windowvar == 35 then WindowsText = "Windows 1, 2 and 6 are open"
elseif Windowvar == 36 then WindowsText = "Windows 3 and 6 are open"
elseif Windowvar == 37 then WindowsText = "Windows 1, 3 and 6 are open"
elseif Windowvar == 38 then WindowsText = "Windows 2, 3 and 6 are open"
elseif Windowvar == 39 then WindowsText = "Windows 1, 2, 3 and 6 are open"
elseif Windowvar == 40 then WindowsText = "Windows 4 and 6 are open"
elseif Windowvar == 41 then WindowsText = "Windows 1, 4 and 6 are open"
elseif Windowvar == 42 then WindowsText = "Windows 2, 4 and 6 are open"
elseif Windowvar == 43 then WindowsText = "Windows 1, 2, 4 and 6 are open"
elseif Windowvar == 44 then WindowsText = "Windows 3, 4 and 6 are open"
elseif Windowvar == 45 then WindowsText = "Windows 1, 3, 4 and 6 are open"
elseif Windowvar == 46 then WindowsText = "Windows 2, 3, 4 and 6 are open"
elseif Windowvar == 47 then WindowsText = "Windows 1, 2, 3, 4 and 6 are open"
elseif Windowvar == 48 then WindowsText = "Windows 5 and 6 are open"
elseif Windowvar == 49 then WindowsText = "Windows 1, 5 and 6 are open"
elseif Windowvar == 50 then WindowsText = "Windows 2, 5 and 6 are open"
elseif Windowvar == 51 then WindowsText = "Windows 1, 2, 5 and 6 are open"
elseif Windowvar == 52 then WindowsText = "Windows 1, 5 and 6 are open"
elseif Windowvar == 53 then WindowsText = "Windows 1, 3, 5 and 6 are open"
elseif Windowvar == 54 then WindowsText = "Windows 2, 3, 5 and 6 are open"
elseif Windowvar == 55 then WindowsText = "Windows 1, 2, 3, 5 and 6 are open"
elseif Windowvar == 56 then WindowsText = "Windows 4, 5 and 6 are open"
elseif Windowvar == 57 then WindowsText = "Windows 1, 4, 5 and 6 are open"
elseif Windowvar == 58 then WindowsText = "Windows 2, 4, 5 and 6 are open"
elseif Windowvar == 59 then WindowsText = "Windows 1, 2, 4, 5 and 6 are open"
elseif Windowvar == 60 then WindowsText = "Windows 3, 4, 5 and 6 are open"
elseif Windowvar == 61 then WindowsText = "Windows 1, 3, 4, 5 and 6 are open"
elseif Windowvar == 62 then WindowsText = "Windows 2, 3, 4, 5 and 6 are open"
elseif Windowvar == 63 then WindowsText = "Windows 1, 2, 3, 4, 5 and 6 are open"
end
minuten=15
RainPrediction = IsItGonnaRain(minuten)
URLtext = string.gsub( WindowsText, " ", "%%20")
commandArray = {}
if ((Windowvar > 0) and (RainPrediction > 20) and (otherdevices['RainmessageTimer'] == 'Off')) then
commandArray['RainmessageTimer'] ='On'
commandArray['OpenURL']='https://autoremotejoaomgcd.appspot.com/sendnotification?key=XXXXXXXXX&title=Message%20of%20the%20house&text=Precipitation%20is%20expected%20within%2015%20minutes%20and%20'..URLtext..'&url=xxxxxx.myfritz.net:8080'
end
if devicechanged['WindowsCheck'] == 'On' then
commandArray['OpenURL']='https://autoremotejoaomgcd.appspot.com/sendnotification?key=XXXXXXXXX&title=Message%20of%20the%20house&text=Precipitation%20is%20expected%20within%2015%20minutes%20and%20'..URLtext..'&url=xxxxxx.myfritz.net:8080'
commandArray['WindowsCheck']='Off'
end
return commandArray
regards Hans