Condition for running scene

Hi, I use the VariableContainer. When most lights are on in the living room the variable no. 1 called ‘AltLysON’ is set to 1.

I want to let a few lamps turn on when we walk through the living room at night unless most lights are already on and ‘AltLysON’ is 1.

This is my luup code for the scene:

– Don’t run if all lights are on (somebody here awake)
local vcvar1 = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, dID)
if (vcvar1() == 1) then
return false
end

I have also tried this:
if (AltLysON() == 1) then
return false
end

Neither works. The scene is triggered by movement sensors and changes the light no matter which of the above luup I use.

What am I doing wrong?

Thanks.

What am I doing wrong?

A couple of things:

You need to set the variable dID to the device number of your VariableContainer device. You have defined vcvar1 as a local variable but you are referencing it as a function by the syntax vcvar1(). The result of a luup.variable_get(…) is a string value so you must either compare it to a string constant or convert it to a number.

Try this (after changing 123 to your VC device number):

-- Don't run if all lights are on (somebody here awake) local dID = 123 -- Change to the device number of your VariableContainer local vcvar1 = luup.variable_get("urn:upnp-org:serviceId:VContainer1", "Variable1", dID) if (vcvar1 == "1") then return false end

You make also find some help in Conditional Scene Execution.

It is so easy when you know how :wink: Thank you. It worked.

Now I am trying two conditions using functions as you decribe in your excellent guide.

But I get this message: Error in lua for scenes and events

– To allow the scene to run only at night
local function CheckNight()
return luup.is_night()
end

– Don’t run if all lights are on (somebody here awake)
local function CheckForAllLightsOn()
local dID = 51 – Change to the device number of your VariableContainer
local vcvar1 = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, dID)
if (vcvar1 == “1”) then
return false
end

return CheckNight() and CheckForAllLightsOn()

I also tried replacing ‘return luup.is_night()’ with ‘if luup.is_night() then return false’; same error.

Why is that, I wonder?

Why is that, I wonder?

You are missing an end statement to close the second function. The single one is closing the if. Change to:

[code]-- To allow the scene to run only at night
local function CheckNight()
return luup.is_night()
end

– Don’t run if all lights are on (somebody here awake)
local function CheckForAllLightsOn()
local dID = 51 – Change to the device number of your VariableContainer
local vcvar1 = luup.variable_get(“urn:upnp-org:serviceId:VContainer1”, “Variable1”, dID)
if (vcvar1 == “1”) then
return false
end
end

return CheckNight() and CheckForAllLightsOn()
[/code]

Programmers often use indenting (as in the code above) to keep track of code blocks. It helps to avoid missing ends.

Thank you again :slight_smile: I have spent so much time over the last few years, but it seems your guide and your additional help was what I needed. Now I hope that I can use the functionality of Vera so much more than before :). A debugging feature would be nice to help us newbies find the errors ourselves. But maybe there is one?

A final question for my scene: I want to prevent the scene from running during day (no need for light) and when all lights are already on (no need for more lights), but shouldn’t I actually use ‘OR’ in my last statement then? However it does seem to work now in the daytime.

A debugging feature would be nice to help us newbies find the errors ourselves. But maybe there is one?

See if you think LuaTest would help you.

A final question for my scene: I want to prevent the scene from running during day (no need for light) and when all lights are already on (no need for more lights), but shouldn't I actually use 'OR' in my last statement then? However it does seem to work now in the daytime.

To put it another way, you want the scene to run at night AND when the lights are not all on. The CheckNight() function returns true when it is night. The CheckForAllLightsOn() function returns false if the lights are all on - it should also return true if the lights are not all on (see below). So in this case, AND is the correct logical operator.

Right now CheckForAllLightsOn() will return nil if the lights are not all on. It would be better practice to have it return true in this case. The modification is simple:

-- Don't run if all lights are on (somebody here awake) local function CheckForAllLightsOn() local dID = 51 -- Change to the device number of your VariableContainer local vcvar1 = luup.variable_get("urn:upnp-org:serviceId:VContainer1", "Variable1", dID) if (vcvar1 == "1") then return false else return true end end

or you could write it like this:

-- Don't run if all lights are on (somebody here awake) local function CheckForAllLightsOn() local dID = 51 -- Change to the device number of your VariableContainer local vcvar1 = luup.variable_get("urn:upnp-org:serviceId:VContainer1", "Variable1", dID) return (vcvar1 ~= "1") end

Thank you :slight_smile: