Create lua array from uservariable with multiple elements

Moderator: leecollings

Post Reply
bickel
Posts: 12
Joined: Wednesday 14 January 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sirjansland, Netherlands
Contact:

Create lua array from uservariable with multiple elements

Post by bickel »

Hi,
Does anyone have an idea on how to fill a lua array with multiple elements from 1 uservariable containing these elements?

If i define the array in lua it works fine:

local radi={}
radi[1]={'string 1',number1,'something else'}
radi[2]={'string 2',number2,'something else'}
print(radi[1][1]) --output = string 1

Now if i put the values in a uservariable of type string and fill like this
In gui:
Uservar1 = {'string 1',number1,'something else'}
Uservar2 = {'string 2',number2,'something else'}

local radi={}
local i = 1
while uservariables["Radi"..i] do
radi=uservariables["Radi"..i]
i = i + 1
end
print(radi[1][1]) --outputs all elements including {} = {'string 1',number1,'something else'}

I also tried filling the array like this:

In gui:
Uservar1 = 'string 1',number1,'something else'
Uservar2 = 'string 2',number2,'something else'

radi={uservariables["Radi"..i]}

I think i can probably get it to work to read the uservar as string and then loop through al parts and recreate it into an array, but that's not as nice as i want it too.

Gr,
Ronald
jmleglise
Posts: 192
Joined: Monday 12 January 2015 23:27
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: FRANCE
Contact:

Re: Create lua array from uservariable with multiple elements

Post by jmleglise »

Hi,

What you want is called : serialization.

Have a look here : http://stackoverflow.com/questions/6075 ... -functions
and here : http://lua-users.org/wiki/TableSerialization

A nice way may be to serialize in Json with the common library : json.lua (encode / decode function).

Good luck :-)
My script : https://github.com/jmleglise
RFXTRX433E: Blind Somfy RTS, Portal Somfy Evolvia, chacon IO, Oregon, PIR sensor PT2262
My Last project : Location de vacances a Ouistreham vue mer
KMTronic USB relay
Chinese Z-WAVE: Neo CoolCam
bickel
Posts: 12
Joined: Wednesday 14 January 2015 22:16
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Sirjansland, Netherlands
Contact:

Re: Create lua array from uservariable with multiple elements

Post by bickel »

Hi, tnx for your reply!
I surely can get this to work this way, i was only hoping for a shorter way to program this...some kind of oneliner, but i will start working on it this way so i can get on.

Gr,
Ronald
commodore white
Posts: 63
Joined: Sunday 14 July 2013 11:19
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Ipswich, UK
Contact:

Re: Create lua array from uservariable with multiple elements

Post by commodore white »

Might be an idea to have a look a dzVents. It has the capability of storing variables from one invocation of your script to the next, and probably what you're looking for, the ability to share variables between scripts, so-called global variables.

You'll be able to put the complex structures in the variables that you're after. You should be able to mix-and match between regular LUA scripts and dzVents ones so you needn't recode everything. They work in parallel.

If you've got lots of scripts, you'll probably see a performance boost too.

Peter
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest