return multiple values from function

Moderator: leecollings

Post Reply
manjh
Posts: 725
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

return multiple values from function

Post 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?
Hans
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: return multiple values from function

Post 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
manjh
Posts: 725
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: return multiple values from function

Post 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.
Hans
User avatar
jvdz
Posts: 2199
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: return multiple values from function

Post 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.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
manjh
Posts: 725
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: return multiple values from function

Post 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?
Hans
User avatar
jvdz
Posts: 2199
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: return multiple values from function

Post 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. :-)
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
habahabahaba
Posts: 201
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: return multiple values from function

Post 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
manjh
Posts: 725
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: return multiple values from function

Post 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.
Hans
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest