Need help to understand the use of global persistant data...

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

Moderator: leecollings

Post Reply
Wilop91
Posts: 48
Joined: Tuesday 05 March 2024 17:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: France
Contact:

Need help to understand the use of global persistant data...

Post by Wilop91 »

Hello
I wrote a simple program that has 2 timers and 2 callbacks.
Each timer launches a database query (one to select Temperature and one to select KWH) with a callback

Code: Select all

if (MyMinute == MyMinute2Run ) then --Get Temperature data
   --Prepare CMD with SELECT-FROM-WHERE
   dz.executeShellCommand( {   command = cmd, callback = 'myTMPcallback', })
end
Each callback processes the data and stores it in tables (one for Temperature and one for KWH)
I defined those tables as "global" in the "data" section

Code: Select all

data = {global_myKWHTable =  { initial={} }, global_myTMPTable =  { initial={} },},
I re-assigned a specific name to the tables at the start of the program

Code: Select all

execute = function(dz, triggeredItem, triggerInfo)
   myKWHTable = dz.data.global_myKWHTable
   myTMPTable = dz.data.global_myTMPTable
And I initialized the tables in each callback

Code: Select all

if (triggeredItem.statusCode == 0) and (triggerInfo.trigger == "myTMPcallback") then
   myTMPTable={}
   myTMPTable[1]={}
   myTMPTable[2]={}
   local myPayload = triggeredItem.data
   --Format of received data: 4|TempHumBaro|10.80|2026-03-05
   --I store the data in myTMPTable table - index [1] holds Temperature and index [2] holds dates
   --I can print out the contents of the myTMPTable table
end
And I do the same in the KWH callback

Since both tables are global I thought I would be able to use its contents everywhere in my program. But that's not the case. Those tables seems to be "local" to each callback.
What I'm doing wrong here?
In advance many thanks for your help
Willy
User avatar
RonkA
Posts: 141
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: Need help to understand the use of global persistant data...

Post by RonkA »

Is there somewhere in your script where you take your new received data and fill the global data with it?
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
willemd
Posts: 793
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2026.3
Location: The Netherlands
Contact:

Re: Need help to understand the use of global persistant data...

Post by willemd »

A bit confusing to use the term global while you actually define the persistent variables as "script level" persistent variables (see wiki). Global persistent variables can be defined in dzVents to use between different scripts but that is not the case here and you don't define them as such, so the naming is confusing.

But even at script level you should be able to use the variables between different runs of the script. That is the whole point of persistent variables.

I think it might have to do (but I am just guessing) with your re-assignment to a different name. Maybe this is creating a copy of the persistent data. You then initialize and add contents to the copy, but not to the persistent variables themselves. I would rewrite the script just using the names of the persistent data and test whether it makes a difference.
Wilop91
Posts: 48
Joined: Tuesday 05 March 2024 17:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: France
Contact:

Re: Need help to understand the use of global persistant data...

Post by Wilop91 »

Hi RonkA
Thanks for your reply
RonkA wrote: Sunday 08 March 2026 16:44 Is there somewhere in your script where you take your new received data and fill the global data with it?
I populate the tables within each callback (myTMPcallback and myKWHcallback) with the data i got from the SQL queries.
I get this error:

Code: Select all

attempt to concatenate a nil value (field '?')
I concluded my tables are "local" to each callback routine and not "global" as expected.
My SQL queries work as expected and in each callback I get the data I see in Domoticz database. As a result I can printout the contents of the table which within each callback.
But I cannot printout the other global table.

This is an extract of my program - sorry I'm not a good programmer:

Code: Select all

local MyHour2Run    = "10"
local MyMinute2Run  = "53" --01  
local MyMinute2Stop = "54" --25
local debugLinky = false 
return {
	on = { timer = { 'every minute between ' .. MyHour2Run .. ':' .. MyMinute2Run .. ' and ' .. MyHour2Run .. ':' .. MyMinute2Stop }, 
	                 shellCommandResponses = {'myTMPcallback','myKWHcallback', } },
	data = {global_myKWHTable =  { initial={} }, global_myTMPTable =  { initial={} },},
	logging = {level = domoticz.LOG_INFO, marker = 'CalculateAverages',},
	
	execute = function(dz, triggeredItem, triggerInfo)
	    myKWHTable = dz.data.global_myKWHTable
	    myTMPTable = dz.data.global_myTMPTable
        
        if triggeredItem.isTimer then
            MyMinute = os.date("%M")
            
            if (MyMinute == MyMinute2Run ) then --Get Temperature data
                --Prepare CMD with SELECT-FROM-WHERE
                dz.executeShellCommand( {   command = cmd, callback = 'myTMPcallback', })
            end

            if (MyMinute == MyMinute2Stop ) then --Get KWH data
                --Prepare CMD with SELECT-FROM-WHERE
                dz.executeShellCommand( {   command = cmd, callback = 'myKWHcallback', })
            end
            
        elseif triggeredItem.isShellCommandResponse then
            dz.log("Received ShellCommandResponse callback = " .. triggerInfo.trigger, dz.LOG_INFO)
            
            if (triggeredItem.statusCode == 0) and (triggerInfo.trigger == "myTMPcallback") then
                dz.log("Received TMP callback is OK", dz.LOG_INFO)
                myTMPTable={}
                myTMPTable[1]={}
                myTMPTable[2]={}
                local myPayload = triggeredItem.data
	         --Format of received data: 4|TempHumBaro|10.80|2026-03-05
                --I store the data in myTMPTable table - index [1] holds Temperature and index [2] holds dates
                --I can print the contents of myTMPTable table
            end
            
            if (triggeredItem.statusCode == 0) and (triggerInfo.trigger == "myKWHcallback") then
                dz.log("Received KWH callback is OK", dz.LOG_INFO)
                myKWHTable={}
                myKWHTable[1]={}
                myKWHTable[2]={}
                local myPayload = triggeredItem.data
                --Format of received data: 13|KWH|14843|2020-11-17
                --I store the data in myKWHTable table - index [1] holds KWH and index [2] holds dates
                --I can print the contents of myKWHTable table
                --
                --But I cannot print myTMPTable which is supposed to be a global table 
            end
            
        end
	end
}
Wilop91
Posts: 48
Joined: Tuesday 05 March 2024 17:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: France
Contact:

Re: Need help to understand the use of global persistant data...

Post by Wilop91 »

Hi Willemd
willemd wrote: Sunday 08 March 2026 17:20 A bit confusing to use the term global while you actually define the persistent variables as "script level" persistent variables (see wiki). Global persistent variables can be defined in dzVents to use between different scripts but that is not the case here and you don't define them as such, so the naming is confusing.

But even at script level you should be able to use the variables between different runs of the script. That is the whole point of persistent variables.

I think it might have to do (but I am just guessing) with your re-assignment to a different name. Maybe this is creating a copy of the persistent data. You then initialize and add contents to the copy, but not to the persistent variables themselves. I would rewrite the script just using the names of the persistent data and test whether it makes a difference.
You are right - I should rather use "persistent". I made the "re-assignment" to make reading of the program easier.
I will give this a try and revert back later
Thanks
Willy
User avatar
RonkA
Posts: 141
Joined: Tuesday 14 June 2022 12:57
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Harlingen
Contact:

Re: Need help to understand the use of global persistant data...

Post by RonkA »

i am not a programmer! but in my logic you state:
But I cannot printout the other global table.
I Don't see anywhere in your script the table is actually written into the persistent variable.. (like to dz.data.global_myKWHTable)
So there is nothing to be printed there.
SolarEdge ModbusTCP - Kaku - Synology NAS - Watermeter - ESPEasy - DS18b20
Work in progress = Life in general..
Wilop91
Posts: 48
Joined: Tuesday 05 March 2024 17:14
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: France
Contact:

Re: Need help to understand the use of global persistant data...

Post by Wilop91 »

Hello RonkA and Willemd
Thanks for your replies
Using everywhere in my program dz.data.global_myTMPTable and dz.data.global_myKWHTable to access the tables makes them persistent.
I will try to make the table names a bit shorter to make my program more readable
Best regards
Willy
Post Reply