PROBLEM! Script running more then 10 seconds, please help

Moderator: leecollings

Post Reply
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

PROBLEM! Script running more then 10 seconds, please help

Post by DutchHans »

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.

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
Please help.
regards Hans
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: PROBLEM! Script running more then 10 seconds, please help

Post by jmleglise »

Hi,

1 :
if devicechanged['WindowsCheck'] == 'On' then
commandArray['WindowsCheck']='Off'
If "windowscheck" is an hardware switch , you have an infinite Loop. Set WindowsCheck to Off will trigger an event that will launch again your script "device" and so on.
If it's an virtual switch, no problem because, by design , in this case, LUA don't trigger the event, (unlike the GUI)

2 (more probably) : you request 3 URL (curl + openURL) , so you may exceed the limit of 10 sec.

Does it help ?
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: PROBLEM! Script running more then 10 seconds, please help

Post by DutchHans »

Good evening jmleglise,
Windowscheck is a virtual timer with an off delay of 30 minutes.. So no infinite loop there
What can i change to the curl and openurl?
Btw, its not always giving errors.. I guess only when the eventsystem is busy..
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: PROBLEM! Script running more then 10 seconds, please help

Post by DutchHans »

I thought of splitting the script and store windowvar in a variable..windowvar van be calculated separately from the rainprediction with devicechanged i guess .. When there is rain predicted it calls the variable which then builds the message..

Could that be a solution?
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: PROBLEM! Script running more then 10 seconds, please help

Post by jmleglise »

For point 1 :
to be more accurate , you could write :
if devicechanged['WindowsCheck'] == 'On' and otherdevices["WindowsCheck"]=="On"
isn't it ?

And For point 2 :
Make a try : Remove your 2 last OpenURL. If that works better, then I advise to transform openURL to an os.execute ('curl " " &')
Do you know the '&' in unix ? The '&' will put the job in background.
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
DutchHans
Posts: 230
Joined: Friday 03 April 2015 20:44
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Germany (near dutch border)
Contact:

Re: PROBLEM! Script running more then 10 seconds, please help

Post by DutchHans »

I've split the file in two,
part one updates a variable when a window opens or closes;

part two checks if there will be rain in the next 15 minutes and will execute the notification.
I have changed the "OpenUrl" into os.execute ('curl " " &') as you mentioned with the "&". For now it looks good.

Hope it stays that way...
Thank you
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest