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
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
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
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]
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
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.)
@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.
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.
At the top of the page put the categories: [[Category:<category_name1>]] [[Category:<category_name2>]]
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. ?
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.