Need a Code Check

Hi All

Trying to get a light to switch on when the light level drops below a given level. I’ve put the following together but it does not work ???

–Low Light Level

local lul_temp = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”,“CurrentLevel”, 15)
if (tonumber(lul_temp) < 80) then

luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “1”}, 6)

else return false
end

Can anyone see where i have gone wrong?

Cheers

m0jon

The code looks OK, though I recommend to give lul_temp a default value in case CurrentLevel doesn’t exist. Have you checked the logs to see what errors do you have?

-- Low Light Level
local lul_temp = luup.variable_get("urn:upnp-org:serviceId:LightSensor1","CurrentLevel", 15) or 100
if (tonumber(lul_temp, 10) < 80) then
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1", "SetTarget", {newTargetValue = "1"}, 6)
else
    return false
end

Hi mcvflorin

Thanks for your promt reply and the additions to the code which i have tried but the scene still does not work ??? ???

I dont have access to my error logs. Will keep trying

Cheers

m0jon

Where does your code live? In a Luup plugin, in UPnP space ([tt]RunLua[/tt]), in the ‘Test Luup code (Lua)’ window, in the Luup window for scenes or events, …? Are you absolutely sure that the device IDs are correct?

You could install WAI or SND to debug your code if you cannot access the logfile directly.

The serviceId for LightSensors isn’t a UPnP Standard, so it’s:
[tt]urn:micasaverde-com:serviceId:LightSensor1[/tt]

When in doubt, you can hove the mouse over the Variable in the Device’s Advanced panel and a bubble help will show the corresponding ServiceId.

See also this wiki page > Scene that runs only if the light level is below a user set threshold (currently at the bottom).

Thanks everyone for your input and help. I have tried the wiki link above and have the following

– Low Light Level

local LOW_LEVEL = 30 – the light level threshold for night
local DEVICE_NO = 15 – the light sensor device number
local LS_SID = “urn:micasaverde-com:serviceId:LightSensor1” – the LightSensor service ID

local currentLevel = luup.variable_get (LS_SID, “CurrentLevel”, DEVICE_NO) or 0
currentLevel = tonumber(currentLevel)

if currentLevel <= LOW_LEVEL then

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget", {newTargetValue = "1"}, 6)

else
return false
end

Unfortunately it still does not work ??? ???

I’m adding the code to the LUUP section within a scene. I am using a 3 in 1 sensor and have double checked all if my id numbers but still no joy

m0jon

Are you using the device ID of the parent device or the device ID of the light sensor child device?

Thank you WB-AVL who surrgested using the following code which worked instantly :smiley:

– Getting Dark

local lul_light_level = luup.variable_get (“urn:micasaverde-com:serviceId:LightSensor1”,“CurrentLevel”,38)

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “32”}, 0)

if ( tonumber (lul_light_level) < 20 ) or ( luup.is_night() ) then

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “2”}, 0)

else

return false

end

And thanks to everyone for their input with my little problem

Cheers

m0jon