How to reference devices table from within execute function  [Solved]

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

Moderator: leecollings

Post Reply
ugge
Posts: 6
Joined: Friday 20 July 2018 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version: 12741
Location: Gothenburg
Contact:

How to reference devices table from within execute function

Post by ugge »

I want to calculate the average barometer pressure whenever any of my barometers change.
The script triggers via the devices table inside on.
How can I reference the list/table of devices in the on section?

This is my code so far, but it tells me
bad argument #1 to 'pairs' (table expected, got nil)

Code: Select all

return {
	on = {
		devices = {
			773,
			786,
			747,
			871
		},
		timer = {
		    'every minute'
		},
	},
	logging = {
	    level = domoticz.LOGINFO,
	    marker = 'Tryck',
	},
	execute = function(domoticz, device)
		local height = 124
		local pressure = 0
		-- Calculate mean pressure and correct for height
		for _, p in pairs(devices) do
		    pressure = pressure + domoticz.devices(p).barometer / #devices
		    domoticz.log('Average pressure: ' .. pressure, domoticz.LOGINFO)
		end
		-- Update virtual barometer
		-- domoticz.devices('Lufttryck').updateBarometer(pressure)
	end
}
}
As far as I understand the devices specified in the on section is not the same as devices (global?) referenced by me that I reference in the code.
Is there a way to reference devices = { 773, 786, 747, 871} as a table to calculate the average similar to what I try in my code?
mgugu
Posts: 218
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: How to reference devices table from within execute function  [Solved]

Post by mgugu »

You have to initialize table of devices before:
local onDevices_table = {773,786,747,871}
return {
on = {devices = onDevices_table,
timer = {
'every minute'
},
},
logging = {
level = domoticz.LOGINFO,
marker = 'Tryck',
},
execute = function(domoticz, device)
local height = 124
local pressure = 0
-- Calculate mean pressure and correct for height
for _, p in pairs(onDevices_table) do
pressure = pressure + domoticz.devices(p).barometer / #devices
domoticz.log('Average pressure: ' .. pressure, domoticz.LOGINFO)
end
-- Update virtual barometer
-- domoticz.devices('Lufttryck').updateBarometer(pressure)
end
}
}
ugge
Posts: 6
Joined: Friday 20 July 2018 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version: 12741
Location: Gothenburg
Contact:

Re: How to reference devices table from within execute function

Post by ugge »

Thanks, worked like a charm.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest