Author Topic: Problems with simple LUA  (Read 590 times)

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Problems with simple LUA
« on: March 16, 2018, 04:10:43 pm »
Hi.

I was trying out some simple LUA script just to test how my LED strips were dimming. The script is:
Code: [Select]
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:
Quote
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!

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #1 on: March 16, 2018, 04:16:18 pm »
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.

Offline Selkirk

  • Newbie
  • *
  • Posts: 15
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #2 on: March 16, 2018, 06:11:18 pm »
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?

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #3 on: March 16, 2018, 07:07:49 pm »
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?

Thanks!

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #4 on: March 17, 2018, 10:29:41 am »
Anyone see what I'm doing wrong here?

Code: [Select]
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)

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?
« Last Edit: March 17, 2018, 10:32:08 am by tiwas »

Online akbooer

  • Master Member
  • *******
  • Posts: 5964
  • Karma: +258/-69
  • "Less is more"
Re: Problems with simple LUA
« Reply #5 on: March 17, 2018, 12:09:14 pm »
All your luup.variable_set() calls should be luup.call_action() 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.

Code: [Select]
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 luup.sleep(), but use luup.call_delay() or luup.call_timer() callbacks instead.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #6 on: March 17, 2018, 12:35:51 pm »
All your luup.variable_set() calls should be luup.call_action() 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.

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 :p ).

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 :)

Online akbooer

  • Master Member
  • *******
  • Posts: 5964
  • Karma: +258/-69
  • "Less is more"
Re: Problems with simple LUA
« Reply #7 on: March 17, 2018, 12:49:23 pm »
You're welcome.

You could, of course, simplify further...

Code: [Select]
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 luup.sleep().
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #8 on: March 17, 2018, 12:52:46 pm »
However, as a matter of urgency, please do investigate the replacements I suggested for luup.sleep().

Yes. Thanks. I've already switched :)

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

Online akbooer

  • Master Member
  • *******
  • Posts: 5964
  • Karma: +258/-69
  • "Less is more"
Re: Problems with simple LUA
« Reply #9 on: March 17, 2018, 01:01:02 pm »
luup.sleep() 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.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline tiwas

  • Full Member
  • ***
  • Posts: 180
  • Karma: +0/-0
Re: Problems with simple LUA
« Reply #10 on: March 17, 2018, 01:05:52 pm »
luup.sleep() 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.

Thanks! Great answer :)

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
Code: [Select]
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.

Online akbooer

  • Master Member
  • *******
  • Posts: 5964
  • Karma: +258/-69
  • "Less is more"
Re: Problems with simple LUA
« Reply #11 on: March 17, 2018, 01:26:52 pm »
If you look at the luup.call_action() documentation (yes, there actually is some) then you will see:

Code: [Select]
-- 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:
Code: [Select]
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 /port_3480/data_request?id=lu_invoke& call to Vera from a browser.)

In contrast to setting data, getting is far better (more efficiently) accomplished with a luup.variable_get() call.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.