Luup code for turning lights on conditional to ambiant light

Hi,
Wanted to use the aeotec multisensor 6 to turn on the light when motion is detected ONLY is the ambient light is below a threshold.

Realise this needs some Luup code :frowning: . I have never done any Luup code…
Looking around on various help pages I have come up with this (my device number is 61 for the lightsensor). I do not know:
1- Is the code/syntax good? (does not seem to work :-[ )
2- Where should I put this code in the scene that turns the light on when motion is detected? - Attached to the trigger device (motionsensor) or at the scene level so that when the scene executes it also executes this code?

local lul_tmp = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”, “CurrentLevel”, 61)
if (tonumber(lul_temp) > 10) then
return False
end

Thks for any guidance

[quote=“charpin, post:1, topic:199723”]Hi,
Wanted to use the aeotec multisensor 6 to turn on the light when motion is detected ONLY is the ambient light is below a threshold.

Realise this needs some Luup code :frowning: . I have never done any Luup code…[/quote]

You can do this without Lua using any of the various logic plugins: Reactor, Rules Engine, PLEG

Looking around on various help pages I have come up with this (my device number is 61 for the lightsensor). I do not know: 1- Is the code/syntax good? (does not seem to work :-[ )

local lul_tmp = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”, “CurrentLevel”, 61)
if (tonumber(lul_temp) > 10) then
return False
end

Your code needs a “return true” as the very last statement (after the “end”), and “False” should be lowercase (false). Alternately, you could write it like this:

local lul_tmp = luup.variable_get("urn:upnp-org:serviceId:LightSensor1", "CurrentLevel", 61) return tonumber(lul_temp) <= 10

This means directly return the value of the test, which will always be true or false. The sense of the test has to be reversed in this case, so you return true when the light level is at or below your threshold.

2- Where should I put this code in the scene that turns the light on when motion is detected? - Attached to the trigger device (motionsensor) or at the scene level so that when the scene executes it also executes this code?

Code goes into the scene that turns on the light when motion is detected. Scene Lua runs before the scene actions; if it returns true, the actions are done, otherwise, the scene aborts.

EDIT: clarity

Thks Rigpapa

also think my command was wrong ( urn:upnp-org:serviceId:LightSensor1) and should have been : urn:micasaverde-com:serviceId:LightSensor1

understand better how it works now. not completely sure I understand how the ‘return’ command works yet … do you need ‘end’ at all? what is the next action when false is returned vs allow?

[quote=“charpin, post:3, topic:199723”]Thks Rigpapa

also think my command was wrong ( urn:upnp-org:serviceId:LightSensor1) and should have been : urn:micasaverde-com:serviceId:LightSensor1

understand better how it works now. not completely sure I understand how the ‘return’ command works yet … do you need ‘end’ at all? what is the next action when false is returned vs allow?[/quote]

Yes, when you’re first learning the environment, the service IDs are the devil.

The “end” in your original code was a match for the “if…then”. Since my abbreviated version took that “if” statement out, no “end” is needed there.

If the scene Lua returns false, nothing else happens… the scene run just ends. The scene actions (including all delayed actions) aren’t run, so no devices are changed.

[quote=“rigpapa, post:4, topic:199723”]If the scene Lua returns false, nothing else happens… the scene run just ends. The scene actions (including all delayed actions) aren’t run, so no devices are changed.[/quote]One subtlety that we learned recently is that false will stop actions, but it will not stop scene notifications.

thks guys for the help

Another thing…seems when motion is triggered, only the last reported light reading is used, not the current one… Since the reporting only happens once every hour, is there a way to force a current reading?

Maybe this thread will help: http://forum.micasaverde.com/index.php?topic=52830.0

In theory, according to the technical doc it reports light levels whenever it changes by 100 lux. This may be different, however, if it’s not plugged into power and is using batteries instead.

yeah but mine will never ever change by 100 Lux…looks like the operating range is 0-70LUX!

[quote=“charpin, post:9, topic:199723”]yeah but mine will never ever change by 100 Lux…looks like the operating range is 0-70LUX![/quote] Ah - is your sensor indoors by any chance?