Use of table.foreach for device name repetitions

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

Moderator: leecollings

Post Reply
User avatar
Wolly76
Posts: 24
Joined: Sunday 21 March 2021 8:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands / Utrecht region
Contact:

Use of table.foreach for device name repetitions

Post by Wolly76 »

Hi all,

I would like to clean my code a bit by replacing

Code: Select all

local devicesThermostatTemperature = {'Thermostat (Room1) - Temperature', 'Thermostat (Room2) - Temperature', 'Thermostat (Room3) - Temperature', 'Thermostat (Room3) - Temperature'};
with

Code: Select all

local devicesThermostatNames = {'Room1', 'Room2', 'Room3', 'Room4'}
local devicesThermostatTemperature = {}; table.foreach(devicesThermostatNames,  function(k,v) table.insert(devicesThermostatTemperature,'Thermostat ('..v..') - Temperature') end )
at the start of the dzVents-script, i.e. before the "return {....}"
Except for Temperatures, there is a list of Setpoints and Valves as well, and this would make it easier to extend.

Unfortunately, I receive errors "2022-09-29 21:29:36.999 Error: dzVents: Error: (3.1.8) ...ipts/dzVents/generated_scripts/thermostatZoneControl.lua:4: attempt to call a nil value (field 'foreach')", which does not really surprise me but I was wondering if this might work anyhow? Just to note that the code works in a LUA online compiler.

Could this work, with a (small) code modification? Are there any better solutions to avoid these repetitions?

Thanks in advance. Looking forward.
/Wol
mgugu
Posts: 218
Joined: Friday 04 November 2016 12:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: France
Contact:

Re: Use of table.foreach for device name repetitions

Post by mgugu »

According to http://lua-users.org/wiki/TableLibraryTutorial, table.foreach is now deprecated. pair() operator is recommended instead.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest