[SOLVED] How to iterate through own list of values or hardware

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

Moderator: leecollings

Post Reply
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

[SOLVED] How to iterate through own list of values or hardware

Post by zavjah »

Hi folks,

I want to set some parameter for few hardware that I have using the openURL method. For this, I need hardware indexes of relevant hardware so i can compile the correct URL.

I've tried two approaches how to iterate thorough those:

1. I've created an own list of values, eg. local list = {1,2,3,4,5} with idx of hardware, but as this are nor devices I am not sure how to use dz.devices().filter(function(device) here to get the respective idx of each hardware. For devices it works but how do I iterate the list to get 1 or 2 or 3 returned, so i can use it to compile a correct URL

2. I've see two methods in the documentation, domoticz.hardware().forEach(..) and for i, j in pairs(domoticz.hardware()) do .. end, but to be honest I am not quite sure how to use it

Any ideas how to accomplish this?

thx,
Zavjah
zavjah
Posts: 36
Joined: Tuesday 13 August 2019 10:20
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: [SOLVED] How to iterate through own list of values or hardware

Post by zavjah »

For interested, this is how i solved it:

Code: Select all

local devIdx =  {93,94,95,96,97,98,99,100,101}
for i, dev in pairs(devIdx)
  do
    dz.log(dev)
end
cheers,
Zavjah
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: [SOLVED] How to iterate through own list of values or hardware

Post by rrozema »

Documentation: Looping_through_the_collections

Here's an example how to use .filter() and .forEach().

Code: Select all

local SWITCHES = { 'MYSWITCH1', 'MYSWITCH2', 205 }

	domoticz
		.devices()
			.filter( SWITCHES )
				.forEach(
			                        function( device )
							if device.active then
								device.switchOff()
							end
						end
					)
The above example has a simple task: switch off 3 switches only when they are on. It will retrieve from domoticz all devices(), then filters to only the 3 devices in the SWITCHES set. The devices can be idenitfied by the name of the device or its idx value. Then for each device found, the function is called. The function in this example checks if the active state of the switch is true and if it is, it will call the switchOff() method on the device.

Also, the .filter() part can be extended, changed or left off completely, it just decides for which devices your function will get called. You can easily add whatever criteria you need inside the function to decide if an action is needed or not. You can also decide not to include the filter() call, and check inside the function if the device needs to be checked or not. And furthermore you can of course change whatever action you need done on the devices found.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest