Please help linking functions together

Hi, I would really appreciate if you could help me to get this to work. I have 3 conditions that need to happen for this scene to run but get stuck on how to link them together. I red through many posts to try to figure it out and learned a lot from you already. Thank you for everything and I hope someone can help.

local function checkTime()
local pStart = “17:00” – Start of time period
local pEnd = “17:35” – 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 checkHum()
local dID = 105 – Device ID of Hum sensor
local lLow = 80 – Lowest level of range
local lHigh = 100 – Highest level of range
local allow = true – true runs scene when in range, false blocks it
local lCurrent = tonumber((luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”,dID)))
return (((lCurrent >= lLow) and (lCurrent <= lHigh)) == allow)
end
local function checkAway()
local deviceNo = 4 – Device ID of armed sensor
local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local armed = luup.variable_get (SS_SID, “Armed”, deviceNo)
local allow = false – true runs scene if switch is on, false blocks it
local status = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“Status”,dID)
return ((status == “1”) == allow)
end

Hi,

You would add a line like this at the end of your example to have the scene continue if all three conditions are true.

return (checkTime() and checkHum() and checkAway())

If you need different condition state it could read.

if  (checkTime() and (checkHum() or checkAway())) then
  return false
else
  return true
end

Have fun.

Cheers Rene

Thank you so much Rene. I have been out of town traveling and just got back to this. Code passes the test in Lua test app but the scene dos not work. I must be missing something. If you see something obvious please help again. Thank you!!

function checkTime()
local pStart = “17:15” – Start of time period
local pEnd = “17:35” – 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 checkHum()
local dID = 105 – Device ID of your Hum sensor
local lLow = 50 – Lowest level of range
local lHigh = 100 – Highest level of range
local allow = true – true runs scene when in range, false blocks it
local lCurrent = tonumber((luup.variable_get(“urn:upnp-org:serviceId:TemperatureSensor1”,“CurrentTemperature”,dID)))
return (((lCurrent >= lLow) and (lCurrent <= lHigh)) == allow)
end
local function checkAway()
local deviceNo = 4 – Device ID of your MultiSwitch
local SS_SID = “urn:micasaverde-com:serviceId:SecuritySensor1” – Security Sensor Service ID
local armed = luup.variable_get (SS_SID, “Armed”, deviceNo)
local allow = false – true runs scene if switch is on, false blocks it
local status = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“Status”,dID)
return ((status == “1”) == allow)
end
if (checkTime() and (checkHum() and checkAway())) then
return true
end

There’s a possible exit from the scene Lua without a return value, which Luup may/will interpret as “false” and not run your scene actions. You’re missing the “else” cause Rene suggested at the end.

Hi, stupid question but I can’t get this to work. It clears the Lua code test with no errors like everything is okay but when I add it to luup section in my scene nothing ever happens? No errors but also no results. Does this code needs to be added to startup Lua? Thank you in advance.

Hi,

It needs to go in your scene LUA code and the scene needs a trigger. This code will just determine if the rest of the scene (activities) will be completed (when returned true) or not (when returned false).

You have to change the last bit so you always return a value:

if (checkTime() and (checkHum() and checkAway())) then return true else return false end

Cheers Rene

Hi, When I add the last few lines as suggested I can’t get this to pass the Luup code test. Without the 3 last lines it passes the test. What is returning an error in these lines please?? Also, should there be another parenthesis before and after checkAway? Thank you so much.

if (checkTime() and (checkHum() and checkAway())) then
return true
else
return false
end

It means the return value is false, i.e. your conditions are not all true.

Cheers Rene