how to create a unique parameters table?

Easy to use, 100% Lua-based event scripting framework.
Post Reply
User avatar
papoo
Posts: 97
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: France
Contact:

how to create a unique parameters table?

Post by papoo » Tuesday 08 May 2018 18:30

Hello,
in a DZvents script I wanted to simplify my declaration of the devices and their parameters
i do that :

Code: Select all

local DEVICES = {
	['device_1'] = 'device_1 Act',
	['device_2'] = 'device_2 Act'
}
local TimeOut = {
	['device_1 Act'] = 6,
	['device_2 Act'] = 10
}

local Min = {
	['device_1 Act'] = 4,
	['device_2 Act'] = 3
}
local Max = {
	['device_1 Act'] = 15,
	['device_2 Act'] = 100
}
local Notify = {
	['device_1 Act'] = 'No',
	['device_2 Act'] = 'Yes'
}
return {

	on = {
		devices = {							
			'device_1',
			'device_1 Act',
			'device_2',
			'device_2 Act'
		},
	},


	execute = function(domoticz, device)

		if (DEVICES[device.name] ~= nil) then

			local switch = domoticz.devices(DEVICES[device.name])
			local timeout = TimeOut[DEVICES[device.name]]
			local Min = Min[DEVICES[device.name]]
			local Max = Max[DEVICES[device.name]]
            
        end
I would like to replace

local Min = {}, local Max = {}, local Notify = {}, local TimeOut = {},...

by something like

Code: Select all

local parametres = {
	['device_1 Act'] = {[TimeOut] = 6, [Min] = 4, [Max] = 15, [Notify] = 'No'},
	['device_2 Act'] = {[TimeOut] = 7, [Min] = 3, [Max] = 100, [Notify] = 'Yes'}
}
is the syntax good? how to recover the parameters of each device
I tried a lot of things like

Code: Select all

local timeout = parametres.TimeOut[DEVICES[device.name]]
but nothing works

SweetPants
Posts: 2250
Joined: Friday 12 July 2013 21:24
Target OS: Linux
Domoticz version: V4.9999
Location: The Netherlands
Contact:

Re: how to create a unique parameters table?

Post by SweetPants » Wednesday 09 May 2018 7:34

Should {[TimeOut] = 6 not be {["TimeOut"] = 6 etc?
Ubuntu 16.04.3 LTS, Intel NUC, MySensors 2.3.0-alpha, Pro Mini 3.3/8M RFM69(HW), ESP8266 (SDK2.2.0), Sonoff, RFLink, RFXcom, MQTT, NodeRed, Domoticz Beta (from source)

User avatar
waaren
Posts: 908
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to create a unique parameters table?

Post by waaren » Wednesday 09 May 2018 7:47

Hi papoo,


just to give you an idea.

Code: Select all

  local DEVICES = {
	['device_1'] = 'device_1 Act',
	['device_2'] = 'device_2 Act'
}

    local TimeOut = {
	['device_1 Act'] = 6,
	['device_2 Act'] = 10,
	['device_3_Act'] = 15
}

    local Min = {
	['device_1 Act'] = 4,
	['device_2 Act'] = 3
}

    local Max = {
	['device_1 Act'] = 15,
	['device_2 Act'] = 100
}
    local Notify = {
	['device_1 Act'] = 'No',
	['device_2 Act'] = 'Yes'
}
   dz.log("DEVICES.device_1: " .. DEVICES.device_1,dz.LOG_INFO)                         -- ===>> device_1 Act
     
   dz.log("TimeOut[\"device_1 Act\"] ".. TimeOut["device_1 Act"],dz.LOG_INFO)           -- ===>> 6
   
   dz.log("Timeout[DEVICES.device_2]: " .. TimeOut[DEVICES.device_2],dz.LOG_INFO)       -- ===>> 10 
   
   -- dz.log("Timeout.DEVICES.device_2: " .. TimeOut.DEVICES.device_2,dz.LOG_INFO)     -- ===>>: Does not work 
   -- dz.log("Timeout.DEVICES.device_3: " .. TimeOut.DEVICES.device_3,dz.LOG_INFO)        -- ===>>: Does not work
   
  dz.log("TimeOut[DEVICES[\"device_2\"]]: " ..  TimeOut[DEVICES["device_2"]],dz.LOG_INFO)        -- ===>>: 10

   
    end
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

dannybloe
Posts: 1473
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: how to create a unique parameters table?

Post by dannybloe » Wednesday 09 May 2018 8:58

You can also consider to define groups of devices in Domoticz GUI and use them in you scripts: domoticz.groups('myGroup').devices().forEach(..) etc.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
papoo
Posts: 97
Joined: Friday 22 January 2016 23:14
Target OS: Raspberry Pi
Domoticz version: 4.9
Location: France
Contact:

Re: how to create a unique parameters table?

Post by papoo » Wednesday 09 May 2018 14:25

I misunderstood.
I want to make it easier to declare parameters and devices by only one time in a single table like

Code: Select all

local parametres = {
['device_1 act'] = { ['TimeOut'] = 6, ['Min'] = 4, ['Max'] = 15, ['Notify'] = 'Yes', ['SubSystem'] = 'domoticz.NSS_HTTP'},
['device_2 act'] = { ['TimeOut'] = 6, ['Min'] = 4, ['Maxt'] = 15, ['Notify'] = 'Yes', ['SubSystem'] = 'domoticz.NSS_PUSHBULLET'}
}
and stop using and delete the tables
local TimeOut = {}
local Min = {}
local Max = {}
local Notify = {}
...
i made a function, but it is not very easy to use in other scripts without changing everything each time

Code: Select all

function lesparametres(tableau)

for t, i in pairs(tableau) do
    if t == "TimeOut" then  timeout = i end
    if t == "Min" then min = i end
    if t == "Max" then max = i end
    if t == "Notify" then notify = i end 
    if t == "SubSystem" then subsystem = i end
    end

return timeout,min,max,notify

end
I would prefer to access the desired parameter simply by naming it for example

Code: Select all

TimeOut.parametres[device.name]
or something like that

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests