Page 1 of 1

Initializing tables in the persistent data

Posted: Wednesday 15 March 2017 9:32
by BakSeeDaa
I would like to define and use a lua table in the global_data.lua. It should have 4 elements
I get an error message. It's probably obvious what is wrong but I just can't see it. Can You?

global_data.lua:

Code: Select all

return {
	data = {
		openSections = { initial = {0, 0, 0, 0} },
	}
}
My dzVents script (testScript3):

Code: Select all

return {
	active = true,
	on = {
		'Test Button',
	},

	execute = function(domoticz, device)
		local z = 1
		domoticz.globalData['openSections'][z] = domoticz.globalData['openSections'][z] + 1
		print(domoticz.globalData['openSections'][z])
	end
}}
And the error message in the domoticz log:

Code: Select all

 2017-03-15 08:58:19.916 LUA: =====================================================
017-03-15 09:15:52.934 LUA: >>> Handler: testScript3
2017-03-15 09:15:52.934 LUA: >>> Device: "Test Button" Index: 47
2017-03-15 09:15:52.934 LUA: .....................................................
2017-03-15 09:15:52.935 LUA: An error occured when calling event handler testScript3
2017-03-15 09:15:52.935 LUA: /home/pi/domoticz/scripts/lua/scripts/testScript3.lua:9: attempt to index field 'openSections' (a nil value)
2017-03-15 09:15:52.935 LUA: .....................................................
2017-03-15 09:15:52.935 LUA: <<< Done 

If I alter the script temporarily so it initializes the array before the first use

Code: Select all

domoticz.globalData['openSections'] = {0, 0, 0, 0}
It will work after that.

It seems that the initialization of the variable openSections defined in global_data.lua doesn't work.

I'd like to get a grip of this problem in case that the global data gets "reset" or deleted for some reason.

Any help is very appreciated. Thanks!

Re: Initializing tables in the persistent data

Posted: Wednesday 15 March 2017 10:14
by BakSeeDaa
I've checked further and it seems that /home/pi/domoticz/scripts/lua/scripts/storage/__data_global_data.lua won't hold any variables unless I first assign a value to them in an event script like: domoticz.globalData['test'] = {'Hello'}

Just defining a variable in global_data.lua will not create an entry in storage/__data_global_data.lua, hence referencing it before assigning a value to it will return nil.

Re: Initializing tables in the persistent data

Posted: Saturday 18 March 2017 9:41
by dannybloe
Mm.. interesting.. I'll see if I can find the time to fix some of the bugs the coming days.

Re: Initializing tables in the persistent data

Posted: Saturday 18 March 2017 19:41
by BakSeeDaa
dannybloe wrote:Mm.. interesting.. I'll see if I can find the time to fix some of the bugs the coming days.
That sounds great! :D

Re: Initializing tables in the persistent data

Posted: Friday 29 September 2017 22:06
by jsiegmund
Was this ever solved? I seem to be having the same thing in my install.

Re: Initializing tables in the persistent data

Posted: Sunday 15 October 2017 11:07
by JamesParker
I have exactly the same problem with script level persistent variables so I guess this has not yet been solved. Is anyone else using tables in persistent data?

Re: Initializing tables in the persistent data

Posted: Tuesday 14 November 2017 22:23
by jsiegmund
JamesParker wrote: Sunday 15 October 2017 11:07 I have exactly the same problem with script level persistent variables so I guess this has not yet been solved. Is anyone else using tables in persistent data?
Yeah, I finally figured it out. It's very important where you place your scripts. The global_data.lua script and all of the scripts you want to use with it need to go in the domoticz\scripts\dzVents\scripts path. If you edit scripts via the UI, they are NOT placed in this folder and therefore it doesn't work. This is very counter intuitive.

Re: Initializing tables in the persistent data

Posted: Wednesday 15 November 2017 15:54
by dannybloe
Well, it doesn't matter where you place the global_data.lua script (either in scripts/dzVents/scripts or in scripts/dzVents/generated_scripts (through the GUI editor). Just as long as there is only one! Lua loads the file based on the order of the package paths but it loads only one.
I'll add this to the documentation to make this more clear.