How to avoid Time script to overlap

Moderator: leecollings

Post Reply
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

How to avoid Time script to overlap

Post by emme »

Ciao,

I have few time script that runs for more than 1 minute (they ping some devices and retry if they are not availble... so it could take time)

Because it is supposed to trigger switches in Domoticz, I have the issue that the script runs twice because a minute pass and it strats while the old one is still running.

This results in double actions... I don't like it!

So, unless there is no a clear way to avoid a script to run in case it has not finished yet, I create a not-so-complex workaround I'm going to show you:

Example:
script_time_dostuff.lua

Code: Select all

commandArray = {}
   os.execute('sleep 65')
return commandArray
..so this would overlap in the next minute

SOLUTION:
CREATE:
string variable called dostuff_running (0-not running, 1-running)
script_time_dostuff_running.lua

Code: Select all

commandArray = {}
   os.execute('sleep 2') --this to ensure the other script starts   
   if uservariables['dostuff_running'] == '0' then commandArray['Variable:dostuff_running'] = '1'
return commandArray
EDIT script_time_dostuff.lua as follow:

Code: Select all

commandArray = {}
   if uservariables['dostuff_running'] == '0'  then 
      os.execute('sleep 65')
   end
   commandArray['Variable:dostuff_running'] = '0'
return commandArray
of course... the script could run once every 2 minutes in the worst case, but if it takes less that 60secs, it will run correctly the next minute

ciao
M
The most dangerous phrase in any language is:
"We always done this way"
User avatar
jvdz
Posts: 2332
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to avoid Time script to overlap

Post by jvdz »

In genreral my comment would be NOT to do this with the TIME event, but rather run the script with CRON which will update the switch status by means of a JSON call.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest