Can this be simplified?

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

Moderator: leecollings

Post Reply
DanM
Posts: 79
Joined: Thursday 23 October 2014 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Can this be simplified?

Post 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
}
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Can this be simplified?

Post 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
The most dangerous phrase in any language is:
"We always done this way"
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Can this be simplified?

Post 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*' }},
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest