This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Create table within another table
#1
Good companion. I hope they are excellent. My question is this. It is possible to create a table within another table as you will see in the image that I attach. I want to create a parent table that will be "climate profiles" and within this, the user can create create different tables such as "comfort" with different settings as if the user wants to turn on the air, what is the temperature you want configure, fan mode etc. The idea is that I can have several tables such as Climate profiles, Occupation profiles, curtain profiles, etc., but that the client can add internal tables and configure what they want. This information should ideally be stored in the storage. Currently I have been able to store tables but independent, but not tables within tables as seen in the image. Thank you for your cooperation.

Attached Files Thumbnail(s)
   
Reply
#2
Hi,

That is rather easy to do:

mytable = { mysecondtable = { mythirttable = { final = 'end' } } }

Code:
--or add afterwards

mytable = {}
mytable.mysecondtable = {}
mytable.mysecondtable.mythirttable = {}
mytable.mysecondtable.mythirttable.final = 'end'

log(mytable)
Sample:
Code:
profiles = {
  climate = {
    mode = 'comfort',
    temperature = 21.3,
    fanmode = 1
  },
  occupation = {
    unoccupied = {
        cool = 25,
        heat = 18
    },
    occupied = {
      cool = 25,
        heat = 18
    }
  },
  curtain = {
    runtime = 90,
    closed = {
      hour = 17,
      min = 45
    },
    opened = {
      hour = 7,
      min = 30
    }
  },
  reserved = 'not used yet'
}

log(profiles)
log(profiles.climate)
log(profiles.curtain.runtime)
See also: https://www.lua.org/pil/2.5.html

BR,

Erwin
Reply
#3
Hi Erwin, thanks for responding and for your help. I tried what you told me and it worked. Now, I want the same user to be the one to put the names to each subtable that will be a profile so that it can be stored and managed. I tried to use the .. to concatenate it, but it gives me a syntax error. I enclose the code to see where I am going wrong. Thank you and I will be attentive.

Code:
    Nombre_modo = grp.getvalue('32/1/21')
    encendido_clima = grp.getvalue('32/1/22')
    temperatura_clima = grp.getvalue('32/1/23')
    velocidad_clima = grp.getvalue('32/1/24')

mytable = {}
mytable...Nombre_modo = {}
mytable...Nombre_modo..encendido = encendido_clima
mytable...Nombre_modo..temperatura = temperatura_clima
mytable...Nombre_modo..velocidad = velocidad_clima
mytable.mysecondtable = {}
mytable.mysecondtable.encendido = encendido_clima
mytable.mysecondtable.temperatura = temperatura_clima
mytable.mysecondtable.velocidad = velocidad_clima
log(mytable)

resultado = storage.set('Perfiles Climas', mytable)
Reply
#4
Hi,

Try this (enclose what you want in [ ... ] ):
Code:
Nombre_modo = grp.getvalue('32/1/21')
encendido_clima = grp.getvalue('32/1/22')
temperatura_clima = grp.getvalue('32/1/23')
velocidad_clima = grp.getvalue('32/1/24')

mytable = {}
mytable[Nombre_modo] = {}
mytable[Nombre_modo].encendido = encendido_clima
mytable[Nombre_modo]['temperatura'] = temperatura_clima
mytable[Nombre_modo]['velocidad'] = velocidad_clima
mytable.mysecondtable = {}
mytable.mysecondtable.encendido = encendido_clima
mytable.mysecondtable.temperatura = temperatura_clima
mytable.mysecondtable.velocidad = velocidad_clima
log(mytable)
But be carefull with this methode, your address 32/1/21 can hold a duplicate value and you might overwrite another table

Sample:
Code:
Nombre_modo = grp.getvalue('32/1/21') -- lets assume this holds value 'Test'
encendido = grp.getvalue('32/1/22') -- lets assume this also holds value 'Test'
mytable = {}
mytable[Nombre_modo] = {} -- Add a new empty sub tabel called 'Test'
mytable[Nombre_modo].value = 100 -- Add a new entry inside table 'Test' -> value with value 100
mytable[encendido] = {} -- This wil overwrite previous Nombre_modo as this is 'Test' and we add now a empty table {} 'Test'
                        -- so value = 100 is deleted
log(mytable)
BR,

Erwin
Reply
#5
Hello Erwin, thanks for answering, with your help I have been able to progress to store and even obtain a part of the table (Conf) and pass it to a Group address without having to touch the other tables (mysecondtable). My question is if I can delete one of the subtables, without having to delete the parent table ´Perfiles Climas´ or the other child table mysecondtable. I have used this code and others, but modifying it, but it did not work for me and even in some it generates an error.

Code:
Nombre_modo = grp.getvalue('32/1/21')
datotabla=storage.get('Perfiles Climas')
log (datotabla.Nombre_modo)
storage.delete(datotabla[Nombre_modo])
I attach a photo of the only thing I want to delete.

Some errors are:

Event for Pruebas Storage (32/1/20) 10.08.2020 17:52:58
Library genohm-scada:0: attempt to call method 'sub' (a nil value)
stack traceback:
Library genohm-scada: in function ''
Library genohm-scada: in function 'delete'
User script:21: in main chunk

I clarify that in the communication object is the name of ´Conf´ which is the table I want to eliminate.

Thank you

Attached Files Thumbnail(s)
   
Reply
#6
Hi,

What you can do is this:
Code:
Nombre_modo = grp.getvalue('32/1/21')
datotabla=storage.get('Perfiles Climas')
datotabla[Nombre_modo] = nil -- this will delete the sub table
storage.set('Perfiles Climas', datotabla)
BR,

Erwin
Reply


Forum Jump: