LUA runs too many times

Moderator: leecollings

Post Reply
Scotzh
Posts: 4
Joined: Saturday 17 September 2016 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

LUA runs too many times

Post 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
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

LUA runs too many times

Post 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.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
Scotzh
Posts: 4
Joined: Saturday 17 September 2016 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA runs too many times

Post 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.
Scotzh
Posts: 4
Joined: Saturday 17 September 2016 9:38
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: LUA runs too many times

Post by Scotzh »

Got it! TY
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: LUA runs too many times

Post 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.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: LUA runs too many times

Post by dannybloe »

I'd suggest to use dzVents for this. Makes this a whole lot easier!
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest