is it possible to use an array to declare persistent data?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

is it possible to use an array to declare persistent data?

Post by papoo »

I want an easy way to declare my persistent data
the devices are declared at the beginning of the script in MyDevices array
how can i do a loop on this array to declare the devices in data ={ }

Code: Select all

local MyDevices             = {'device1','device2','device3'}
return {
    active  = true,
    on = {
        timer   = { 'every 5 minutes' },
    },
    data = { 
    -- here i want to do a for loop to declare device1 device2 and device3
    },    

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: is it possible to use an array to declare persistent data?

Post by boum »

Using the local array should work.
Of course you should put it into form first.

Code: Select all

local MyDevices             = {'device1','device2','device3'}
local myData = {}
for i,v in ipairs(MyDevices) do
  myData[v] = { initial = -1 } -- put your initial value here if there is one
end
return {
    active  = true,
    on = {
        timer   = { 'every 5 minutes' },
    },
    data = myData,    
Last edited by boum on Thursday 04 July 2019 22:15, edited 1 time in total.
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: is it possible to use an array to declare persistent data?

Post by papoo »

Unfortunately no
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: is it possible to use an array to declare persistent data?

Post by boum »

I tried and the persistent data is not written to disk unless there is an initial value set. So I modified the script in my first post to reflect it
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: is it possible to use an array to declare persistent data?

Post by boum »

I did a few more tries with persistent datas (never tried them before).
It looks like you can pass directly MyDevices to data, but the data will be written to disk only when you set a value in the execute function. I suppose when your script has ended, the dzVents system go through the data table and if something has been set in your domoticz.data table, then the new value is serialized to disk. (If you do a domoticz.data.glop = "foo" in your execute function but glop is not in the declared data, it won't get dumped).
User avatar
papoo
Posts: 126
Joined: Friday 22 January 2016 22:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10
Location: France
Contact:

Re: is it possible to use an array to declare persistent data?  [Solved]

Post by papoo »

thanks
work fine
Post Reply

Who is online

Users browsing this forum: RonkA and 1 guest