Event only if light is low (3 in 1 sensor)

Hi, I’m trying to do a Luup script inside a Scene that has a Command to turn on a light when a Door sensor is triggered. I want that the light is only turned on if it is dark. So what I did is enter a Luup script inside the Event that triggers the door sensor adding a Luup script as follows:


local LOW_LEVEL = 20 – the light level threshold for night
local HIGH_LEVEL = 40 – the light level threshold for day
local DEVICE_NO = 33 – 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
return true
elseif currentLevel >= HIGH_LEVEL then
return false
end

But the script does not work the light is always turned on even if it is day (light sensor over 20). What am I doing wrong? How can I do some debugging but putting the currentLevel value into a log?
Thanks for every help :wink:

Was the light level also over 40 / what would you like the light to do when the level is in between 20 and 40?

the light level was at 11. now its at 0. I used a LOW_LEVEL = 5 for testing.
The values were just for testing. What I want is that the lights only go on if the level is under 20 else stay off.

[quote=“marcxroma, post:3, topic:168394”]the light level was at 11. now its at 0. I used a LOW_LEVEL = 5 for testing.
The values were just for testing. What I want is that the lights only go on if the level is under 20 else stay off.[/quote]
OK, but with those test values, there is a gap between 20 and 40 where it’s not clear what gets returned.

Just to be clear, does the following work?

local LOW_LEVEL  = 20 -- the light level threshold for night
local DEVICE_NO  = 33 -- 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
        return true
else
        return false
end

The luup.log command will let you print something to the log.

(Alternatively, you could use the WAI plugin to print something to the GUI.)

Because my sensor now is showing level 0 I have inverted the IF ELSE. The light should NOT go on now but they still do

[code]

local LOW_LEVEL = 20 – the light level threshold for night
local DEVICE_NO = 33 – 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
return false
else
return true
end

[/code]

Mmm, are you sure you’re reading the right sensor (i.e. device number)?
I’m also assuming you didn’t manually associate the light into Group 1, so the sensor is controlling the light directly.

You can test the code in the sandbox under MIOS developers → Test Luup code (Lua)

If the code returns false you will get “Code failed”
If the code returns true you will get “Message sent successful”

If that works, then it should also work ok with commands in a scene.

How can I “print” the value of the light sensor to screen or a log so that I can debug if the value is returned (and if the device id is the right one)?
If I use the code in the developer-> test lua code (without specifying any device in “Device number”) I get Success.

this code returns: Successful send

[code] local LOW_LEVEL = 20 -- the light level threshold for night local DEVICE_NO = 33 -- 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
return false
else
return true
end
[/code]

and this one returns: code failed

[code] local LOW_LEVEL = 20 -- the light level threshold for night local DEVICE_NO = 33 -- 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
return true
else
return false
end
[/code]

See a few posts back.

So now my light sensor level is at 27.

if I use the code that works in the devel section with the result “code failed”

[code] local LOW_LEVEL = 20 -- the light level threshold for night local DEVICE_NO = 33 -- 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
return true
else
return false
end
[/code]

inside the scene event-> luup event my light still goes on?!

WORKS! The reason why the last try in the above post seemed not to work was because I executed the Scene by licking on Run in the Web UI4. I tried to trigger the sceen with the door sensor instead and it works.
The light stays off :slight_smile:

Beautiful!

The reason why the last try in the above post seemed not to work was because I executed the Scene by licking on Run in the Web UI4. I tried to trigger the sceen with the door sensor instead and it works. The light stays off :)
Yup, exactly. (If you put the code in the Luup tab, it would have worked through the Run button as well.)

@mcvflorin

Is it possible to add this small script to the collection: http://wiki.micasaverde.com/index.php/Scripts_for_scenes ?

[quote=“huogas, post:13, topic:168394”]@mcvflorin

Is it possible to add this small script to the collection: http://wiki.micasaverde.com/index.php/Scripts_for_scenes ?[/quote]

@huogas
Should be possible to do so, not a problem.

@mcvflorin, is there someone managing the scene scripting pages? If not, ill be glad to add some stuff the (and broaden my knowledge in that field as i go along), just let me know if you want it formatted/sorted/captioned/boxed or whatever.

Sure. I confirmed your Wiki account, so if you see any useful scripts you can add them to that page.

I added the script to the collection. Here is the link:
http://wiki.micasaverde.com/index.php/Scripts_for_scenes#Scene_that_turns_on_the_light_when_a_door_sensor_is_tripped.2C_if_the_light_level_is_below_a_user_set_threshold

Anyone with editing rights can add scripts there. The only requirement is to make sure that the script works before adding it there.

@marcxroma

Thanks for this piece of code. Very helpful.

Sure. I confirmed your Wiki account, so if you see any useful scripts you can add them to that page.[/quote]

How can I add a new page ? (a new ToDo)

To create a page you must access that page with this link:
http://wiki.micasaverde.com/index.php/
followed by the page name.

e.g.: to create a page named Tips_and_Tricks you use this link:
http://wiki.micasaverde.com/index.php/Tips_and_Tricks
The following message will appear:
There is currently no text in this page, you can search for this page title in other pages or edit this page.
Click on edit this page and start having fun. :slight_smile:
At the top of the page put the categories:
[[Category:<category_name1>]]
[[Category:<category_name2>]]

@mcvflorin

Thanks. First page created :slight_smile:

I know I’m off topic, but, last question, can I modify the main navigation menu to add something more meaningful, like “How to”, and “Luup examples”, etc. ?

[quote=“huogas, post:19, topic:168394”]@mcvflorin

Thanks. First page created :slight_smile:

I know I’m off topic, but, last question, can I modify the main navigation menu to add something more meaningful, like “How to”, and “Luup examples”, etc. ?[/quote]

@hugoas, yes please do.
I edited your page by moving the categories from bottom to the top of the page and it will now also show up in the category lists.

Please find a format i created for this here:
http://wiki.micasaverde.com/index.php/User_talk:Henk