Hi
I want to use LUA to activate a scene if two conditions are true.
It has to be a interval off days and a interval of time. I have combined 2 set off LUA code found in “Conditional Executing …” but obviously I dont know anything about LUA. ;D
The code, it seems, only checks for the first part.
Can anybody help me?
local dFirst = 2 – Start day of period (1-7) Sunday = 1
local dLast = 6 – End day of period (1-7) Sunday = 1
local allow = true – true runs scene during period, false blocks it
local tNow = os.date(“*t”)
local dNow = tNow.wday
if dLast >= dFirst then
return (((dNow >= dFirst) and (dNow <= dLast)) == allow)
else
return (((dNow >= dFirst) or (dNow <= dLast)) == allow)
end
local startTime = “05:15”
local endTime = “07:30”
local hour = tonumber( startTime:sub( startTime:find(“%d+”) ) )
local minute = tonumber(startTime:sub(-2))
if hour and minute then
startTime = hour * 100 + minute
else
luup.log(“ERROR: invalid start time”)
return false
end
if startTime <= endTime then
– Both the start time and the end time are in the same day:
– if the current time is in the given interval, run the scene.
if startTime <= currentTime and currentTime <= endTime then
return true
end
else
– The start time is before midnight, and the end time is after midnight:
– if the current time is not outside the given interval, run the scene.
if not (endTime < currentTime and currentTime < startTime) then
return true
end
end
I tried to figure out how luatest Works, but i didnt succeed
My code is af copy of your code, just with changes in time and day:
return checkTime() and checkDay()
local function checkTime()
local pStart = “05:15” – Start of time period
local pEnd = “07:30” – End of time period
local allow = true – true runs scene during period, false blocks it
local hS, mS = string.match(pStart,“(%d+)%:(%d+)”)
local mStart = (hS * 60) + mS
local hE, mE = string.match(pEnd,“(%d+)%:(%d+)”)
local mEnd = (hE * 60) + mE
local tNow = os.date(“*t”)
local mNow = (tNow.hour * 60) + tNow.min
if mEnd >= mStart then
return (((mNow >= mStart) and (mNow <= mEnd)) == allow)
else
return (((mNow >= mStart) or (mNow <= mEnd)) == allow)
end
end
local function checkDay()
local dFirst = 2 – Start day of period (1-7) Sunday = 1
local dLast = 6 – End day of period (1-7) Sunday = 1
local allow = true – true runs scene during period, false blocks it
local tNow = os.date(“*t”)
local dNow = tNow.wday
if dLast >= dFirst then
return (((dNow >= dFirst) and (dNow <= dLast)) == allow)
else
return (((dNow >= dFirst) or (dNow <= dLast)) == allow)
end
end