[Solved] Looping through global variable ?

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

Moderator: leecollings

Post Reply
Diplo95
Posts: 15
Joined: Friday 26 July 2019 10:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Melun France
Contact:

[Solved] Looping through global variable ?

Post 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
Last edited by Diplo95 on Monday 14 June 2021 23:43, edited 3 times in total.
Domoticz v2021.1 on Raspberry Pi 3B+
Raspbian Buster
ConBee II + Xiaomi devices
Zwave
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Looping through global variable ?

Post 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,
        }
    },
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Diplo95
Posts: 15
Joined: Friday 26 July 2019 10:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Melun France
Contact:

Re: Looping through global variable ?

Post 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.
Domoticz v2021.1 on Raspberry Pi 3B+
Raspbian Buster
ConBee II + Xiaomi devices
Zwave
User avatar
boum
Posts: 130
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Looping through global variable ?

Post by boum »

For indexing dynamically, use the bracket operator []. Like this:

Code: Select all

local varName = sonde.GV 
local temp = dz.globalData[varName].getLatest()
Diplo95
Posts: 15
Joined: Friday 26 July 2019 10:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: Melun France
Contact:

Re: Looping through global variable ?

Post by Diplo95 »

Thx a lot : I didn't know that and it's now working as a charm !!
Domoticz v2021.1 on Raspberry Pi 3B+
Raspbian Buster
ConBee II + Xiaomi devices
Zwave
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest