Activating a scene with 2 conditions

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

hour = tonumber( endTime:sub( endTime:find(“%d+”) ) )
minute = tonumber(endTime:sub(-2))

if hour and minute then
endTime = hour * 100 + minute
else
luup.log(“ERROR: invalid end time”)
return false
end

local currentTime = os.date(“*t”)
currentTime = currentTime.hour * 100 + currentTime.min

luup.log("startTime = " … startTime … "; currentTime = " … currentTime … "; endTime = " … endTime)

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

return false

There is an explanation of how to combine separate conditions here.

Hi,

I’d suggest some simplifications -

You have tNow time - calculate no of minutes from midnight :
tNowMinutes = tNow.hour * 60 + tNow.min

Similar :
tStartTimeMinutes = 560+15
tEndTimeMinutes = 7
60+30

That way you have way simpler comparison :

if tNowMinutes > tStartTimeMinutes and tNowMinutes < tEndTimeMinutes then

end

or directly combining both :

if tNowMinutes > tStartTimeMinutes and tNowMinutes < tEndTimeMinutes and dNow >= dFirst and dNow <= dLast then

end

Hope, this help.

Regards,
Stan

@Rexbeckett

Thanks for pointing me in the right direction.

I put the code from your Multiple Conditions example in a scene, but I got and error sawing the LUA code in the scene: Error in lua and scenes event.

I cant figure out what the error is

If you post your code here, I will tell you what is wrong with it. Alternatively, you could have a go with LuaTest.

I tried to figure out how luatest Works, but i didnt succeed :cry:

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

Because the functions are local, they must be defined before they can be called. Just move:

return checkTime() and checkDay()

to be the last line instead of the first.

I’ll fix my example!

Thanks, the problem is fixed now.