Help me debug my code

I’ve added a variable to a few switch devices in my system so that I can keep track of -how- the switch was turned on. I’ve written a scene that’s triggered by a motion detector that will turn on a light. I’ve added Lua code to validate whether the scene should actually run or not, and it absolutely will not result in a “True” no matter what I do.

I have the following global variables defined:

DayNight_ID = 6
FrontLight_ID = 9
FrontLight_PowerOn = 0
PowerOn_0 = 0
PowerOn_2 = 2

Here’s my code:

local allow = true
local DayNight_Status = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,DayNight_ID)
local FrontLight_Status = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,FrontLight_ID)
local FLPO = FrontLight_PowerOn
if FLPO == PowerOn_0 then
if DayNight_Status == “1” then
if FrontLight_Status == “0” then
FrontLight_PowerOn = 2
return ((FrontLight_PowerOn == PowerOn_2) == allow)

The switch is turned off when the scene is triggered and the DayNight module is “forced” to Night. No matter what I do, the scene always evaluates to false.


Not exactly sure what you are trying to do here (code appears overly complex) but:

Where are your Globals declared: in the startup code or in the scene?

Also once FrontLight_PowerOn is set to 2 and if it is truely global, it will stay that way forever and your condition FLPO == PowerOn_0 will never again be true. Or perhaps other code changes it?

Is the variable “allow” for future expansion of the code? If not, then it’s not needed. Just use:

return (FrontLight_PowerOn == PowerOn_2)

Also your condition would be more clear, if written like so:

if (FLPO == PowerOn_0) and (DayNight_Status == "1") and (FrontLight_Status == "0") then luup.variable_set("urn:somedomain-com:serviceId:PoweredOnBy1","powerOnType","2",FrontLight_ID) FrontLight_PowerOn = 2 end

Also do these need to be global - will they be used by other code elsewhere?
PowerOn_0 = 0
PowerOn_2 = 2

Thanks for the reply.

Globals are declared in LUA Startup. FrontLight_PowerOn is controlled by various scenes. I use the value as a global so that other scenes can easily use it determine whether to override the settings or not. 0 generally means that no automation turned on the light. 1 means that it was turned on by a scene and it should be left to be turned back off by a “related” scene. 2 is similar.

A setting of 1 would mean that the lights (in this particular case) were turned on after dark when the garage door opened and they should be turned back off by the scene that runs when the garage door is closed. A setting of 2 would mean that the lights were turned on as a result of the driveway sensor being tripped. As soon as they turn on, if the setting is “2”, a corresponding scene will run to shut them off in two minutes unless the garage door is subsequently opened.

I’m pulling out the “allow” statements all over the place… It’s a leftover that I don’t wish to use any more.

I’ve actually gotten this scene to run and will post up the updated code shortly.

local FrontLight_Status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",FrontLight_ID) local DayNight_Status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",DayNight_ID) local FLPO = FrontLight_PowerOn if FrontLight_Status == "0" then if DayNight_Status == "0" then luup.variable_set("urn:embernetworks-com:serviceId:PoweredOnBy1","powerOnType","2",FrontLight_ID) FrontLight_PowerOn = 2 end end return (FrontLight_PowerOn == PowerOn_2)

Great to see it’s now working:

[code]local FrontLight_Status = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”,FrontLight_ID)
local DayNight_Status = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,DayNight_ID)

if (FrontLight_Status == “0”) and (DayNight_Status == “0”) then
FrontLight_PowerOn = 2

return (FrontLight_PowerOn == PowerOn_2)[/code]

Cleaned it up further like you posted. Thanks for the reminder.

One other thing I’m having an issue with, though, is a follow-on event.

When the driveway sensor is tripped, it sets the PowerOn value to 2 (in the variable and in the device) and then the scene is allowed to run and turn on the light.

Once that light turns on, I have another scene that fires that will possibly turn the light back off. Since I want to be able to change the PowerOn value from 2 to 1 by opening the Garage Door, I have set the scene for immediate execution (turn the light off) and am using a sleep delay in the Lua code. As a result, the front lights show “Waiting for device to reply after 0 retries” until the sleep delay ends. This is bad.

Here’s the code:

luup.sleep(120000) if FrontLight_PowerOn == '2' then luup.variable_set("urn:embernetworks-com:serviceId:PoweredOnBy1","powerOnType","0",FrontLight_ID) FrontLight_PowerOn = 0 end return (FrontLight_PowerOn == '0')

It seems that the sleep function actually puts Vera to sleep, not just the script. How can I install a delay that won’t prevent Vera from doing other things?

Fixed it. I moved the commands to a function and used the call_delay option.

[code]luup.call_delay (“eval_light”, 120, “”)

function eval_light (empty)
if FrontLight_PowerOn == ‘2’ then
FrontLight_PowerOn = 0

You should NEVER call luup.sleep with an argument more than 1000.

Thanks… Makes sense as it seems to align with what I was witnessing. The function call is working well, so it appears I have the functional aspect that I needed without breaking Vera. :slight_smile: