Page 1 of 1

LUA runs too many times

Posted: Monday 19 September 2016 19:15
by Scotzh
Hello
I'm trying to run a script three times in the afternoon and off three times again to turn it off but when I look in the logs it runs several times instead of just once.
What am I doing wrong?

Code: Select all

function timedifference (s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end

commandArray = {}

s = os.date("*t")
offTimeInMin = 22 * 60 + 0
currTimeInMin = s.hour * 60 + s.min
device = "Group:Uppe"

if (timedifference(otherdevices_lastupdate['Sovrum']) > 50) then
    if (currTimeInMin == timeofday['SunsetInMinutes'] and s.sec == 0 and currTimeInMin < offTimeInMin - 5 or
        currTimeInMin == timeofday['SunsetInMinutes'] + 1 and s.sec == 0 and currTimeInMin < offTimeInMin - 4 or
        currTimeInMin == timeofday['SunsetInMinutes'] + 1 and s.sec == 0 and currTimeInMin < offTimeInMin - 3) then 
        commandArray[device] = 'On'
    elseif(currTimeInMin == offTimeInMin and s.sec == 0 or 
           currTimeInMin == offTimeInMin + 1 and s.sec == 0 or
           currTimeInMin == offTimeInMin + 2 and s.sec == 0) then
        commandArray[device] = 'Off'
    end
end
return commandArray

LUA runs too many times

Posted: Monday 19 September 2016 19:32
by mvzut
Maybe an obvious one, but could it be that you made it into a device based script rather than a time based one? If you used a lua file in the domoticz/scripts/lua directory, make sure the name starts with script_time. If you created the script in the built-in event editor, make sure you select time as trigger. This way, the script is run every minute (instead of every time ANY device changes state), so that the time comparisons in your script will only be true once.
By the way, in a time based script you don't need to check for "if s.sec == 0" since they are always run exactly on the minute. If you have a lot of scripts it could be a second or so later, your "if s.sec == 0" statement could then even cause the script NOT to run.

Re: LUA runs too many times

Posted: Monday 19 September 2016 19:57
by Scotzh
Ahh, so that what it is for. TY mvzut! Can you point to a site describing the built-in-event editor? The manual doesn't describe it at all. I just did some trials and errors.

Re: LUA runs too many times

Posted: Monday 19 September 2016 22:20
by Scotzh
Got it! TY

Re: LUA runs too many times

Posted: Monday 19 September 2016 23:01
by mvzut
Scotzh wrote:Ahh, so that what it is for. TY mvzut! Can you point to a site describing the built-in-event editor? The manual doesn't describe it at all. I just did some trials and errors.
Some basic info can be found here I guess: https://www.domoticz.com/wiki/Events#Us ... icz_itself
But it's quite sparse I agree. I don't use Blockly myself anymore, so I haven't really searched thoroughly to be honest.

Re: LUA runs too many times

Posted: Sunday 25 September 2016 15:50
by dannybloe
I'd suggest to use dzVents for this. Makes this a whole lot easier!