Complex Conditional Scene help

I have searched frequently for the best solution to what I would like to do and being rather new to programming scenes, etc. I seem to be even more confused the more I read. I suspect the answer is already posted somewhere but I just haven’t searched for the right words.

Here is what I want involved in this kitchen scene
Outdoor Light Sensor
Motion Sensor
Sink Light (DimmerSwitch)
Breakfast Nook Light (BinaryLight)
Kitchen Light (BinaryLight)

What I have right now is a scene triggered by motion which sets the Sink light to 10% and then runs the following LUUP code added to the scene:

local CarportLightLvl = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, 25)
if (tonumber(CarportLightLvl) > 10) then
return false
else
luup.call_delay(‘KitchenSinkLightOff’, 90)
end

function KitchenSinkLightOff()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue=“0”}, 27)
end

And that works well as a “night light” when I go into the kitchen at night, but I’d like to do the following as well:

  1. Not turn on the Sink light if any of the lights are already on.
  2. Not turn off the Sink light if it has been operated manually after it was turned on by the scene.
  3. Turn off the Sink light if either of the other two lights are turned on after the scene is triggered by motion.
  4. And if I can even more fancy, turn off all the lights after X minutes, one at a time followed by a short delay in between each getting turned off (this would be basically a means to “alert” an occupant of the kitchen to do something to reset/restart the scene so they aren’t left in the dark).

Seems to me the best approach would be a While loop that looped through and checked each condition but I wonder what impact that would have on the system. This would be especially true because I have other scenes I would like to do something similar to this with other scenes so more than one could be running simultaneously.

Also, is there a way to have a job constantly running on the system that could act as a clean up process? For instance, if someone accidently unlocked the Schlage lock I have I’d love to have something see that it has been unlocked for at least X minutes and then relock it - which presumably would be longer than a period time someone might intentionally unlocks it to unload a carload of groceries without having to constantly punch a code with each trip. Likewise, if no motion sensed in the house for an hour to turn off a list of lights if they were left on (this could also be nice if the LUUP engine was restarted when a scene was in a delay/wait state.

I think it is time that you considered the Programmable Logic Event Generator (PLEG) plugin. If you try running scene code longer than a few seconds, Vera’s watchdog will force a restart. It would be possible to use luup.call_delay(…) callbacks instead of a while loop but it will all fall apart if Vera restarts.

Given your list of planned automation, PLEG is really the best way forward. See PLEG Basics for an introduction.