If light level is lower then 50 turn on light

Hi all ,

I am trying to switch on my light is the light level is lower then 50 but can’t get it to work what am i doing wrong in my code

[code]local lul_light = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”,“CurrentLevel”, 34)
light_min = “50”
if (lul_light < light_min ) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 29)

end[/code]

Try converting the string returned by luup.variable_get(…) to a number before comparing that with an integer.

[code]local lul_light = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”,“CurrentLevel”, 34)
local light_min = 50
if (tonumber(lul_light) < light_min ) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 29)

end[/code]

This is not working

You will have problems in LUA with Vera if you use “<”

In Vera, LUA is wrapped in XML. and “<” opens an XML clause.

Just flip your comparison around to use a “>”

[quote=“RichardTSchaefer, post:4, topic:184773”]You will have problems in LUA with Vera if you use “<”

In Vera, LUA is wrapped in XML. and “<” opens an XML clause.

Just flip your comparison around to use a “>”[/quote]

I now try this :

[code]local lul_light = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”,“CurrentLevel”, 34)
local light_min = 50
if (tonumber(lul_light) “<” light_min ) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 29)

end[/code]

But this is not working also

That’s not legal LUA syntax.

i did what you sugested put the < between "

Or did you mean somting else ?

if (light_min > tonumber(lul_light))

  • Garrett

Interesting. I have never had a problem with having a < in lua code. I guess it depends on how you are getting the LUA code uploaded to Vera (I use a copy and paste operation from a text file to the LUA code section of a scene).

You can see what is legal in the LUA language at www.lua.org.

basd82, when you say it is not working, how is it not working? If I was approaching this problem, I probably would put a few logging statements in to see what is exactly happening. The most simple way to do this is using “kwikLog”, as described in the “Conditional Scene Execution” thread in the scene scripting section.

Interesting. I have never had a problem with having a < in lua code. I guess it depends on how you are getting the LUA code uploaded to Vera (I use a copy and paste operation from a text file to the LUA code section of a scene).

It causes a problem in plugin Implementation files because they are treated as XML. I have also not found it to be a problem in scene Lua (at least not in UI5!).

Hi,

I am trying to switch on my light is the light level is lower then 50 but can't get it to work what am i doing wrong in my code

Code:
local lul_light = luup.variable_get(“urn:upnp-org:serviceId:LightSensor1”,“CurrentLevel”, 34)
light_min = “50”
if (lul_light < light_min ) then
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = “100”}, 29)
end

So, you already know that “if (lul_light < light_min ) then” is not going to work, because of the XML wrapping.

You could use however “if (light_min > lul_light) then”, that will work, but can be confusing reading back your code.

You can also escape the smaller than.
Try to use “if (lul_light < light_min )”, you will see it works also!

regards

I have found the sollution.
this is luup code i use to switch my light when its dark engough so every one who need can use it

[code]local LOW_LEVEL = 300 – the light level threshold for night
local DEVICE_NO = 34 – the light sensor device number
local LS_SID = “urn:micasaverde-com:serviceId:LightSensor1” – the LightSensor service ID
local LIGHT_DEVICE_NO = 29 – the light device number
local LIGHT_DIM_LEVEL = 60 – the dim level of the light

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:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = LIGHT_DIM_LEVEL}, LIGHT_DEVICE_NO)
else
return false
end[/code]