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.

Use of a table as a global variable for several scripts
#1
Hi everyone!

I try to create a global variable, a table, so that several scripts insert new records. But everytime a script adds a new record, next time, I find the table empty. 

In a library declare the tables (withouth the local modificator)
---------------------------------------------------------------
tablaknx = {}


devices = {
    { DIRETS = '1xx', VALOR = '17east' },
    { DIRETS = '2xx', VALOR = '27west' },
    { DIRETS = '3xx', VALOR = '37mid' }
}
--------------------------------------------------------------- 

The other scripsts would be like this:

function add_tablaknx(direts, nombre, valor)
  log(tablaknx)
  table.insert(tablaknx, {DIRETS = direts, NOMBRE = nombre, VALOR = valor})
  table.insert(tablaknx, {DIRETS = direts, NOMBRE = nombre, VALOR = valor})
  log(tablaknx)
 
 
return 1
end


function add_devices(direts, valor)
  log(devices)
  table.insert(devices, 1, 11111111111)

  log(devices)
 
 
return 1
end



add_tablaknx('10/www0/0', 'fghqwrewehsg', 'twwwrue')
add_devices('10/www0/0',  'twwwrue')


It works, the register is added, but the next time it tries to add a new register the table it's empty.

Obviously I'm doing it the wrong way. It's possible to implement this with permanent changes? I don't want to use storage becouse the info in this table is going to change quickly and I don't want to destroy the SD Card

Thanks!!!

Kike
Reply
#2
You must use storage for data exchange between scripts
------------------------------
Ctrl+F5
Reply
#3
Use storage. It's only written to the SD card once every 30 minutes.
But keep in mind that it's possible to lose some data if several scripts write to the same storage key in parallel.

Describe what exactly you need to store and how this data will be used.
Reply
#4
(28.01.2026, 09:30)admin Wrote: Use storage. It's only written to the SD card once every 30 minutes.
But keep in mind that it's possible to lose some data if several scripts write to the same storage key in parallel.

Describe what exactly you need to store and how this data will be used.

I'm logging in a database some objects, with additional info and works perfect, but it consume a lot of resources when too many varibles change at the same time.

To avoid this I pretend that events scripts instead of publishing in the database, they could storage in memory somehow a list with the name, the updatetime and the value of the monitorized variables. 
Then define a period to publish this list of register in the database WHEN THE DEVICE has low loadl

I thought that a global variable will works ...
Reply
#5
By database do you mean Object logs or some external database? In LM both main database and storage are placed in RAM and don't differ much in terms of speed.

This solution might be helpful: https://forum.logicmachine.net/showthread.php?tid=350
Reply
#6
(28.01.2026, 09:51)admin Wrote: By database do you mean Object logs or some external database? In LM both main database and storage are placed in RAM and don't differ much in terms of speed.

This solution might be helpful: https://forum.logicmachine.net/showthread.php?tid=350

A mariaDB database in other device. But I will use storage, as you say it's memory and the info writen every 30 min in the SDCard will be small, it will work! (I thought that everytime I use storage it will write inmediatly ... I was wrong)

Thanks for the quick awnser!!
Reply


Forum Jump: