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