Using an array/table with luup.variable_get

Not sure what i’m doing wrong, as I’ve tried things a number of different ways - but please could someone have a look at the following and let me know why I’m not returning the values when using a table to populate the luup.variable_get information ? (See below)

local mytable = {	

--confirm the variable_get requests work..
local temp = luup.variable_get("urn:upnp-org:serviceId:TemperatureSensor1","CurrentTemperature",44)
local light = luup.variable_get("urn:micasaverde-com:serviceId:LightSensor1","CurrentLevel",45)
print("temp = ", temp)
print("light = ", light)

--now use the table to construct the variable_get request--
for k, v in pairs( mytable ) do
	local value1 = luup.variable_get('"'..v[1]..'","'..v[2]..'",'..v[3])
	local value2 = luup.variable_get(v[1]..','..v[2]..','..v[3])

I’m sure it’s something obvious but I’m just not seeing it…

There is no need for the quotes as the table values are strings and integers.

for k,v in pairs( mytable ) do
	local value1 = luup.variable_get(v[1],v[2],v[3])

More neatly, you could, of course, do

local value1 = luup.variable_get(unpack(v))

…there’s really no substitute for learning Lua properly.

Many thanks all,

That’s very interesting as I had actually started off with the following that didn’t work…

for k, v in pairs( mytable ) do
	local value0 = v[1],v[2],v[3]
	local value0a = luup.variable_get(value0)
	print("value0a = ", value0a)

Please could you help me understand why the above did not return anything,

Agreed, and I’m continually learning, doing the best I can, with the time I have available…

Two points of interest, here.

Multiple assignments:

You know that you can say:

local a = 1
local b = 2

You perhaps know that you can equivalently write:

local a,b = 1, 2


local value0 = v[1],v[2],v[3]

only assigns to one variable, so it is the same as:

local value0 = v[1]

Variadic parameters:

Functions can, of course, take different parameter lists. They don’t try to enforce number, or type, of parameters. This call

	local value0a = luup.variable_get(value0)

has only one parameter, since value0 is just a single variable

this call, however,

local value1 = luup.variable_get(unpack(v))

has as many parameters as v has elements, since the function unpack returns all the elements of v as separate parameters.

Thanks @akbooer, now that you put it that way, it makes perfect sense - I don’t know why I didn’t get that, I was scratching my head why only the first one was returned (I think pulling from an array/table threw me)

Quick question regarding this…

I assume the above pulls each ‘v’ in order, so in this case, if the table rows were ordered in another way it wouldn’t work.

Oh yes, of course. Details matter.

If only Vera / MiOS had defined the Luup functions with named parameters, then we could have written:

luup.variable_get {
  device = 42,
  serviceID = “...whatever...”,
  variable = “variable_name_xxx”}

…in any order. But they didn’t, so we can’t.

Best Home Automation shopping experience. Shop at getvera!

© 2021 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules