Page 1 of 1

Can this be simplified?

Posted: Tuesday 28 August 2018 20:51
by DanM
I have the following script I'm hoping to simplify. I'm defining a table of devices at the start.. then using a standard dzvents device script. Is there any way to not have to repeat the devices in the device section of the script (so to read them from devicesToCheck object).

Thanks.

Code: Select all

local devicesToCheck = {
	{ ['name'] = 'Conservatory Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Kitchen Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Bedroom Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Living Room Motion Sensor (Main)', ['true_prob'] = 0.7,['false_prob'] = 0.7 }
}

local prior = 0.5

return {
	on = {
		devices = {
			'Conservatory Motion Sensor', 
            'Kitchen Motion Sensor', 
	        'Bedroom Motion Sensor', 
            'Living Room Motion Sensor (Main)'
		}
	},
	execute = function(domoticz, device)
		print('stuff here')
	end
}

Re: Can this be simplified?

Posted: Wednesday 29 August 2018 16:12
by emme
I do use 2 arrays: the first with the names, the second with the parameters....
this way I can use the first array as trigger

so it would be something like:

Code: Select all

local devNames = {
	 'Conservatory Motion Sensor',
	 'Kitchen Motion Sensor',
	 'Bedroom Motion Sensor',
	 'Living Room Motion Sensor (Main)'
}

local devicesToCheck = {
	{ ['name'] = 'Conservatory Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Kitchen Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Bedroom Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Living Room Motion Sensor (Main)', ['true_prob'] = 0.7,['false_prob'] = 0.7 }
}

local prior = 0.5

return {
	on = {
		devices = {
			devNames 
}
	},
	execute = function(domoticz, device)
		print('stuff here')
	end
}
just ensure the sequence is the same....

you could also define a function by whitch you gather the devices names into a new table to return to the device trigger... but the solution I posted is more easy :P

Re: Can this be simplified?

Posted: Friday 31 August 2018 19:13
by waaren
DanM wrote: Tuesday 28 August 2018 20:51 I have the following script I'm hoping to simplify. I'm defining a table of devices at the start.. then using a standard dzvents device script. Is there any way to not have to repeat the devices in the device section of the script (so to read them from devicesToCheck object).

Thanks.

Code: Select all

local devicesToCheck = {
	{ ['name'] = 'Conservatory Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Kitchen Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Bedroom Motion Sensor', ['true_prob'] = 0.7,['false_prob'] = 0.7 },
	{ ['name'] = 'Living Room Motion Sensor (Main)', ['true_prob'] = 0.7,['false_prob'] = 0.7 }
}

local prior = 0.5

return {
	on = {
		devices = {
			'Conservatory Motion Sensor', 
            'Kitchen Motion Sensor', 
	        'Bedroom Motion Sensor', 
            'Living Room Motion Sensor (Main)'
		}
	},
	execute = function(domoticz, device)
		print('stuff here')
	end
}
Would the use of a wildcard string simplify it enough ?

Code: Select all

on = { devices = { '*Motion Sensor*' }},