Author Topic: Event only if light is low (3 in 1 sensor)  (Read 18859 times)

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Event only if light is low (3 in 1 sensor)
« on: May 24, 2011, 12:36:29 pm »
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 ;-)



Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #1 on: May 24, 2011, 03:00:34 pm »
[...]the light is always turned on even if it is day (light sensor over 20).
Was the light level also over 40 / what would you like the light to do when the level is in between 20 and 40?
Dezwaved at the moment...

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #2 on: May 24, 2011, 03:04:36 pm »
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.
« Last Edit: May 24, 2011, 03:18:06 pm by marcxroma »

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #3 on: May 24, 2011, 03:22:41 pm »
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.
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?

Code: [Select]
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
Dezwaved at the moment...

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #4 on: May 24, 2011, 03:30:54 pm »
How can I do some debugging but putting the currentLevel value into a log?
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.)
« Last Edit: May 24, 2011, 03:37:47 pm by oTi@ »
Dezwaved at the moment...

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #5 on: May 24, 2011, 03:38:09 pm »
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: [Select]
[quote]
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

[/quote]

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #6 on: May 24, 2011, 03:47:01 pm »
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.


Dezwaved at the moment...

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #7 on: May 24, 2011, 03:57:29 pm »
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
Quote
Code: [Select]
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


and this one returns: code failed
Quote
Code: [Select]
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
« Last Edit: May 24, 2011, 04:06:12 pm by marcxroma »

Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #8 on: May 24, 2011, 04:05:33 pm »
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)?
See a few posts back.
Dezwaved at the moment...

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #9 on: May 24, 2011, 04:16:25 pm »
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"
Quote
Code: [Select]
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

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

Offline marcxroma

  • Newbie
  • *
  • Posts: 7
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #10 on: May 24, 2011, 04:22:34 pm »
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  :)


Offline oTi@

  • Community Beta
  • Master Member
  • ******
  • Posts: 4041
  • Karma: +32/-6
  • UI what ?!
Re: Event only if light is low (3 in 1 sensor)
« Reply #11 on: May 24, 2011, 04:33:14 pm »
WORKS!
Beautiful!

Quote
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.)
Dezwaved at the moment...

Offline huogas

  • Sr. Member
  • ****
  • Posts: 301
  • Karma: +0/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #12 on: May 25, 2011, 12:48:56 am »
@mcvflorin

Is it possible to add this small script to the collection: http://wiki.micasaverde.com/index.php/Scripts_for_scenes ?
Vera2 UI4(1.1.1245/3.2), Kwikset x1, HM-DW001 x1, ZRP100 x1, HA-04WD x1, ZDP100 x6, HSM100 x2, VRI06-1LX x8, ZW5301 x1, HM-FS001 x2, ZRW103W x1, GE 45606 x2, HA05C x1, TED5000 x1

Offline Henk

  • Hero Member
  • *****
  • Posts: 820
  • Karma: +3/-0
Re: Event only if light is low (3 in 1 sensor)
« Reply #13 on: May 25, 2011, 03:48:01 am »
@mcvflorin

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


@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.
| Vera2 @ UI4 1.1.1350 / 3.20 | Vera Lite @ UI5 | Vera 3 @ UI5 | 2x Merten  504519 | 1x Duewi  064374 | 1x Everspring SM103 doorbell mod |1 Y-cam IP cam | various LUUP plugins |

Offline mcvflorin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1755
  • Karma: +11/-3
Re: Event only if light is low (3 in 1 sensor)
« Reply #14 on: May 25, 2011, 03:48:18 am »
Is it possible to add this small script to the collection: http://wiki.micasaverde.com/index.php/Scripts_for_scenes ?

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

@mcvflorin, is there someone managing the scene scripting pages?

Anyone with editing rights can add scripts there. The only requirement is to make sure that the script works before adding it there.
« Last Edit: May 25, 2011, 03:52:22 am by mcvflorin »