Page 1 of 1

[Solved] Looping through global variable ?

Posted: Sunday 13 June 2021 19:04
by Diplo95
Hello,

I'm trying to loop through global variables without knowing if it's even possible. I want to get back the values stored in a global variable by another script to update a custom sensor. I tried that :

Code: Select all

local capteurs = {
       {  custom = 'Custom_Test1', GV = 'Value1' },
       {  custom = 'Custom_Test2', GV = 'Value2' },
       {  custom = 'Custom_Test3', GV = 'Value3' },
    }
Considering ValueX is declared in global_data.lua

Then tried that :

Code: Select all

for _,sonde in ipairs(capteurs) do 
       	
		local X = tostring(sonde.GV) 
		local temp = dz.globalData.X.getLatest()
		dz.devices(sonde.custom).updateCustomSensor(temp)
		
    end

and

Code: Select all

for _,sonde in ipairs(capteurs) do 
        	 
		local temp = dz.globalData.(sonde.GV).getLatest()
		dz.devices(sonde.custom).updateCustomSensor(temp)
		
    end
but both end with errors.

Do I try to do something possible or not ?
Thx

Re: Looping through global variable ?

Posted: Sunday 13 June 2021 23:58
by waaren
Diplo95 wrote: Sunday 13 June 2021 19:04 Do I try to do something possible or not ?
Yes. That is possible.
Search for "Global persistent variables" in the dzVents wiki

or build further based on below examples.
userScript.lua

Code: Select all

return 
{
    on = 
	{
        devices = 
		{
            'global*', -- example wildcarded trigger device
        },
    },
	
    logging =
	{
        level = domoticz.LOG_DEBUG,
        marker = 'global',
    },
	
    execute = function(dz, item)
        dz.globalData.tst.add(item.state)
        dz.log(dz.globalData.tst.getLatest().data, dz.LOG_DEBUG)
    end
}
global_data.lua

Code: Select all

return 
{
    data = 
    {
        tst = 
        { 
            history = true, 
            maxItems = 100, 
            maxHours = 168,
        }
    },
}

Re: Looping through global variable ?

Posted: Monday 14 June 2021 9:37
by Diplo95
Thx Waaren for your reply but I think I was not clear. I'd like to loop in a collection of global persistent variables.

I have 3 global persistent variable. I declared them in the global_data.lua file :

globla_data.lua

Code: Select all

data = {
		Value1 = { history = true, maxHours =6 },
		Value2 = { history = true, maxHours =6 },
		Value3 = { history = true, maxHours =6 },
	},
Then, I have a first user script wich calculate some data to store it in them.

I'd like to use this global persistent variable in a second script, but I'd like to loop through them (as in the wiki in the "looping trough the data" section). As an example, I'd like to get back the last value of ValueX to update the custom sensor Custom_TestX, but I'm stuck there, failing to find the way to name the global variable in my loop :
I tried like this :

User_script.lua

Code: Select all

return {
     on = {
			timer = { 'every 1 minutes' }
		},
	
   execute = function(dz, device)

		local capteurs = {
     			 {  custom = 'Custom_Test1', GV = 'Value1' },
       			 {  custom = 'Custom_Test2', GV = 'Value2' },
       			 {  custom = 'Custom_Test3', GV = 'Value3' },
  		}
  		
 		for _,sonde in ipairs(capteurs) do 
        		local X = tostring(sonde.GV) 
			local temp = dz.globalData.X.getLatest()
			dz.devices(sonde.custom).updateCustomSensor(temp)
	end
end
}   
I also tried this version :

Code: Select all

return {
     on = {
			timer = { 'every 1 minutes' }
		},
	
   execute = function(dz, device)

		local capteurs = {
     			 {  custom = 'Custom_Test1', GV = 'Value1' },
       			 {  custom = 'Custom_Test2', GV = 'Value2' },
       			 {  custom = 'Custom_Test3', GV = 'Value3' },
  		}
  		
 		for _,sonde in ipairs(capteurs) do 
        		
        		local temp = dz.globalData.(sonde.GV).getLatest()
			dz.devices(sonde.custom).updateCustomSensor(temp)
	end
end
}   
But no success.

Re: Looping through global variable ?

Posted: Monday 14 June 2021 22:30
by boum
For indexing dynamically, use the bracket operator []. Like this:

Code: Select all

local varName = sonde.GV 
local temp = dz.globalData[varName].getLatest()

Re: Looping through global variable ?

Posted: Monday 14 June 2021 23:42
by Diplo95
Thx a lot : I didn't know that and it's now working as a charm !!