Problems with simple LUA

Hi.

I was trying out some simple LUA script just to test how my LED strips were dimming. The script is:

local ID = 597 fibaro:call(ID, 'startLevelIncrease', 1000, 0) fibaro:sleep(4000) fibaro:call(ID, 'startLevelDecrease', 1000, 100)

Running this in the test lua code section just threw an error, so I installed the latest LuaTest 1.7. Running the code gives me the following error:

LuaTest 1.7

Lua file: /etc/cmh-ludl/luatest.lua

Results
Runtime error: Line 2: attempt to index global ‘fibaro’ (a nil value)

Locals
[main]
ID=597

Print output
(none)

Code
1 local ID = 597
2 fibaro:call(ID, ‘startLevelIncrease’, 1000, 0)
3 fibaro:sleep(4000)
4 fibaro:call(ID, ‘startLevelDecrease’, 1000, 100)

Anyone know how to fix this?

Thanks!

Just to mention it - what I’m trying to do is slowly dim up and then down the lights. ID 597 is a Fibaro Dimmer 2 with a Bypass 2 connected.

The error message indicates that the fibaro library is not available. The docs say that the Fibaro Lua library is for the Home Center 2.

For the Vera start here maybe?

[quote=“Selkirk, post:3, topic:198822”]The error message indicates that the fibaro library is not available. The docs say that the Fibaro Lua library is for the Home Center 2.

For the Vera start here maybe?[/quote]

Thanks!

Anyone see what I’m doing wrong here?

[code]local ID = 597

luup.variable_set(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “1”}, ID)
luup.variable_set(“urn:upnp-org:serviceId:Dimming1”,“SetLoadLevelTarget”, {newLoadlevelTarget= “0”}, ID)
luup.variable_set(“urn:upnp-org:serviceId:Dimming1”,“SetRampRate”,{newRampRate= 1000}, ID)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “StartRampUp”, ID)
luup.sleep(4000)
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “StartRampDown”, ID)
luup.variable_set(“urn:upnp-org:serviceId:SwitchPower1”, “SetTarget”, {newTargetValue = “0”}, ID)
[/code]

EDIT: I’ve tried using print(luup.variable_get… for some of them, but I don’t get anything printed. Also, nothing happens. If I use only luup.call_action it will dim up quickly and then instantly dim down. How do I know when to use luup.call_action and luup.variable_set?

All your [tt]luup.variable_set()[/tt] calls should be [tt]luup.call_action()[/tt] calls.

You are already using (mostly) the correct argument list for action calls, except for some missing empty parameters.

You should never (well, hardly ever) directly set a device’s variables, unless it is your own plugin that you have written.

local ID = 597
local SID = {
  power = "urn:upnp-org:serviceId:SwitchPower1",
  dimming = "urn:upnp-org:serviceId:Dimming1",
}
local act = luup.call_action
act (SID.power, "SetTarget", {newTargetValue = "1"}, ID)
act (SID.dimming,"SetLoadLevelTarget", {newLoadlevelTarget= "0"}, ID)
act (SID.dimming,"SetRampRate",{newRampRate= 1000}, ID)
act (SID.dimming, "StartRampUp", {}, ID)
luup.sleep(4000)
act (SID.dimming, "StartRampDown", {}, ID)
act (SID.power, "SetTarget", {newTargetValue = "0"}, ID)

I’m no expert on dimming devices, but this looks closer to what you need.

Next problem: you should NEVER use [tt]luup.sleep()[/tt], but use [tt]luup.call_delay()[/tt] or [tt]luup.call_timer()[/tt] callbacks instead.

[quote=“akbooer, post:6, topic:198822”]All your [tt]luup.variable_set()[/tt] calls should be [tt]luup.call_action()[/tt] calls.

You are already using (mostly) the correct argument list for action calls, except for some missing empty parameters.

You should never (well, hardly ever) directly set a device’s variables, unless it is your own plugin that you have written.[/quote]

Thanks! This is actually quite close to my starting point - except for your nifty way of shortening down the names and such. Also, I didn’t know I had to include the empty arguments. I think I learned a lot from this post (I say “think” because next time will prove if I learned anything or not :stuck_out_tongue: ).

Seems the startRampUp function is too fast, so I might have to play with the delta value or do a while loop, but this was big help :slight_smile:

You’re welcome.

You could, of course, simplify further…

local ID = 597
local SID = {
  power = "urn:upnp-org:serviceId:SwitchPower1",
  dimming = "urn:upnp-org:serviceId:Dimming1",
}
local function power     (act, var) luup.call_action(SID.power,   act, var or {}, ID) end
local function dimming (act, var) luup.call_action(SID.dimming, act, var or {}, ID) end

power ("SetTarget", {newTargetValue = "1"})
dimming ("SetLoadLevelTarget", {newLoadlevelTarget= "0"})
dimming ("SetRampRate", {newRampRate= 1000})
dimming  "StartRampUp"
luup.sleep(4000)
dimming  "StartRampDown"
power   ("SetTarget", {newTargetValue = "0"})

However, as a matter of urgency, please do investigate the replacements I suggested for [tt]luup.sleep()[/tt].

Yes. Thanks. I’ve already switched :slight_smile:

I didn’t find a reason why, though. Could you elaborate?

[tt]luup.sleep()[/tt] actually stalls the thread which the code is running in. This may not be a problem since every device now has two threads, so some other actions can occur asynchronously. However, a LONG sleep time (~60 seconds or more?) can cause Vera to reload spontaneously…

…of course, many other things seem to do this too, but it’s good to minimise this type of behaviour in the hope of achieving greater system stability.

[quote=“akbooer, post:10, topic:198822”][tt]luup.sleep()[/tt] actually stalls the thread which the code is running in. This may not be a problem since every device now has two threads, so some other actions can occur asynchronously. However, a LONG sleep time (~60 seconds or more?) can cause Vera to reload spontaneously…

…of course, many other things seem to do this too, but it’s good to minimise this type of behaviour in the hope of achieving greater system stability.[/quote]

Thanks! Great answer :slight_smile:

Could I pick your brain about something else, though?

I’m trying to read some parameters and they all come back as ‘-115’, but if I try to do something with them I get that hey’re nill values.

This is what I’m trying

print("GetStatus: " .. act (SID.power, "GetStatus",{}, ID)) print("GetRampRate: " .. act (SID.dimming,"GetRampRate",{}, ID)) print("GetStepDelta: " .. act (SID.dimming,"GetStepDelta",{}, ID)) print("GetRampTime: " .. act (SID.dimming,"GetRampTime",{}, ID)) print("GetLoadLevelTarget: " .. act (SID.dimming,"GetLoadLevelTarget",{}, ID))

I’ve also tried putting them in variables first and then do a format on them, but that’s when LuaTest tells me I’m playing with nills.

If you look at the [tt]luup.call_action()[/tt] documentation (yes, there actually is some) then you will see:

-- function: call_action
-- parameters: service (string), action (string), arguments (table), device (number)
-- returns: error (number), error_msg (string), job (number), arguments (table)

So you’re using the returns wrongly. You should see this if you try:

local err, msg, job, args = act (...)

and look at the content of the arg table.

However, it may also be that this particular device does not implement those actions (you could check with a [tt]/port_3480/data_request?id=lu_invoke&[/tt] call to Vera from a browser.)

In contrast to setting data, getting is far better (more efficiently) accomplished with a [tt]luup.variable_get()[/tt] call.

local ID = 597
local SID = {
power = “urn:upnp-org:serviceId:SwitchPower1”,
dimming = “urn:upnp-org:serviceId:Dimming1”,
}
local function power (act, var) luup.call_action(SID.power, act, var or {}, ID) end
local function dimming (act, var) luup.call_action(SID.dimming, act, var or {}, ID) end

power (“SetTarget”, {newTargetValue = “1”})
dimming (“SetLoadLevelTarget”, {newLoadlevelTarget= “0”})
dimming (“SetRampRate”, {newRampRate= 1000})
dimming “StartRampUp”
luup.sleep(4000)
dimming “StartRampDown”
power (“SetTarget”, {newTargetValue = “0”})

is this code working?
I wanted to do same thing to my dimmable lights