Page 1 of 1
return multiple values from function
Posted: Thursday 09 January 2025 15:24
by manjh
According to
Code: Select all
https://stackoverflow.com/questions/63044739/functions-that-return-multiple-values-lua
I can return multiple values from a single function call.
But when I try, I get an error.
this is the skeleton for the function:
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
local switch, volt, watt, kwh = ".."
return switch, volt, watt, kwh
end
this is the call, and print for the results:
Code: Select all
a, b, c, d = ExtractElectrics(dev_name1, dev_name4, dev_name2, dev_name3)
print ("@@@@@ switch :"..a)
print ("@@@@@ volt :"..b)
print ("@@@@@ watt :"..c)
print ("@@@@@ kwh :"..d)
The domoticz log throws an error on the second print line. FIrst print line comes out OK.
Does this suggest I can only return one value from a function?
Re: return multiple values from function
Posted: Thursday 09 January 2025 16:17
by boum
your skeleton function only initialize the first local variables, all other are nil.
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
local switch, volt, watt, kwh = "a", "b", "c", "d"
return switch, volt, watt, kwh
end
Re: return multiple values from function
Posted: Thursday 09 January 2025 18:18
by manjh
OK, so this version of LUA does not follow the general rule that multiple return values are supported.
I guess I'll circumvent by putting the values into an array and return that.
Re: return multiple values from function
Posted: Thursday 09 January 2025 18:32
by jvdz
Multiple values are supported, but in the posted example the 2nd-4h variable will be "nil" since the right-hand side function of the "=" only has one value.
I think it always has worked like this in lua.
Re: return multiple values from function
Posted: Thursday 09 January 2025 19:47
by manjh
jvdz wrote: ↑Thursday 09 January 2025 18:32
Multiple values are supported, but in the posted example the 2nd-4h variable will be "nil" since the right-hand side function of the "=" only has one value.
I think it always has worked like this in lua.
So how would the statement look according to you?
Re: return multiple values from function
Posted: Thursday 09 January 2025 21:59
by jvdz
This should work fine assuming the parameters are all given a value
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
return dev_switch, dev_volt, dev_watt, dev_kwh
end
if there is a change that one is nil you could set the default like this:
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
return (dev_switch or '') , (dev_volt or 0), (dev_watt or 0), (dev_kwh or 0)
end
.. but i guess i am missing the point of this extra function at this moment.

Re: return multiple values from function
Posted: Friday 10 January 2025 7:14
by habahabahaba
boum wrote: ↑Thursday 09 January 2025 16:17
your skeleton function only initialize the first local variables, all other are nil.
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
local switch, volt, watt, kwh = "a", "b", "c", "d"
return switch, volt, watt, kwh
end
I agree with this. It works
Re: return multiple values from function
Posted: Friday 10 January 2025 8:38
by manjh
jvdz wrote: ↑Thursday 09 January 2025 21:59
This should work fine assuming the parameters are all given a value
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
return dev_switch, dev_volt, dev_watt, dev_kwh
end
if there is a change that one is nil you could set the default like this:
Code: Select all
function ExtractElectrics(dev_switch, dev_volt, dev_watt, dev_kwh)
return (dev_switch or '') , (dev_volt or 0), (dev_watt or 0), (dev_kwh or 0)
end
.. but i guess i am missing the point of this extra function at this moment.
As said, it is a skeleton. Purpose of this function will be to extract four values from a Zigbee smart switch with energy measuring function, format the values in a standard way, and return.