Lua Code checking bulb status not quite right ... can you help? [SOLVED]

Hi Guys,

Trying to absorb what I can do in Lua and not making much progress beyond the simplest of commands.

I am trying to use a 4 in 1 (Aeon) motion detector to turn on dimmable z-wave bulb if the bulb is presently OFF

Tried this:

[code]
local dID = 6 – Device ID of the dimmable bulb
local tID = 24 – Device ID of the Daytime plugin

local switchstatus = luup.variable_get(“urn:schemas-upnp-org:device:DimmableLight:1”,“LoadLevelStatus”,dID)
local daynight = luup.variable_get(“urn:rts-services-com:serviceId:DayTime”,“Status”,tID)

if tonumber(switchstatus) = 0 and daynight == “0”
then
return (true)
else
return (false)
end[/code]

I get a Lua error … I’m almost certain it’s in trying to read the dimmer bulb level to see if it is OFF because I have tried the DayTime on it’s own and I’m able to turn the light on and off with that as the only switch in place. I also tried a local version of a switch to test … and that only had a “Status” variable and I seemed to make it work OK. It was when I tried to modify the code to use the dimmable bulb instead that it started throwing errors.

Can somebody help me understand the variable and comparison I need to do to figure out if the Bulb is OFF?

Thanks

You almost got it right

if tonumber(switchstatus) = 0  and daynight == "0"

should be

if tonumber(switchstatus) == 0  and daynight == "0"

Your original IF conditional expression tries to make an assignment to the return value of the tonumber function and raises an error…

Probably just a type (since you got the equality operator correct later in the conditional expression) 8-}

Thanks cybrmage, to be truthful I still don’t understand the differences in application with some of the subtle things like = vs == and a “0” versus a 0 so you’re quick tip at least got rid of the Lua error.

The code behavior still doesn’t work as expected though. It’s night time on my system, and I have the light OFF, but the function doesn’t return a true and turn on the Light. The dashboard shows the light OFF.

I wonder if there is some problem/challenge reading the value ; like it isn’t updated to Vera what the bulb status is? Does the bulb need to be polled more or something?

Actually I tried to simplify the expression to see why it never resulted in True by just reading the “Status” of the bulb. Should be 0 or 1 …

if tonumber(switchstatus) == 0 then return (true) else return (false) end

This always returns a False, even though I can drill down to the device and look at the variables, Status either shows 0 or 1 in relation to the bulb off or on … but regarless of if I test for the value shown (and known) it still resulst in a false.

If I do the same for the day/night setting it works as expected - I can get a true result if I test for night (=0)

What am I doing wrong or missing here on the bulb Status reading?

I don’t think you are using the right service name. It should be

local switchstatus = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelStatus”, dID)

And the value will be 0-100 not 0 or 1

but 0 does equal off

That’s great shallowearth, you solved the issue. :slight_smile:

How did you get that service name? The one I used was indeed incorrect, but it’s the one I see if I go on the device type box of the settings for the device. For future use, what is the best way to find the right service name to use for a given device if it’s not the one you see on the dashboard?

Thanks

The luup.variable_get function uses the serviceId, not deviceType.

When you go to the settings page for the device, click on the advanced tab (UI5) or the advanced tab then the variables header (UI7). Then find the variable you are interested in and hover your mouse pointer over the variable name and the tooltip will display the serviceId for that variable.

Thanks for the tip to hover - I never tried that and now I see the service names. Great info to know!