Initializing tables in the persistent data

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

Moderator: leecollings

Post Reply
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Initializing tables in the persistent data

Post 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!
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Initializing tables in the persistent data

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Initializing tables in the persistent data

Post by dannybloe »

Mm.. interesting.. I'll see if I can find the time to fix some of the bugs the coming days.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Initializing tables in the persistent data

Post 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
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Initializing tables in the persistent data

Post by jsiegmund »

Was this ever solved? I seem to be having the same thing in my install.
JamesParker
Posts: 2
Joined: Sunday 15 October 2017 11:02
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Initializing tables in the persistent data

Post 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?
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Initializing tables in the persistent data

Post 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.
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Initializing tables in the persistent data

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest