Page 1 of 1

Set filter without idx numbers

Posted: Thursday 09 November 2023 9:16
by fvdp80
I'm trying to minimize the usage of idx numbers in my scripts and only want to mention them at the start once.
But with the following script i get stuck on the filter and forEach function as the lights aren't turned off.
Help is appreciated :D

Code: Select all

local w2k1 = 127 --wandschakelaar 2 knops knop 1
local w2k2 = 128 --wandschakelaar 2 knops knop 2
local w2k2dubbel = 137 --wandschakelaar 2 knops knop 2 dubbelklik
local eettafel = 130
local wandlamp = 93
local lichtstraat = 88
local wkhue = 8
local keuken_spots = 73
local kookeiland = 78
local spotshal = 97

return 
{
	on = 
	{
		devices = 
		{
		    w2k1,
		    w2k2,
		    w2k2dubbel
		},
	},
	    logging =  
        {
            level = domoticz.LOG_INFO, -- set to LOG_ERROR when tested and OK
            marker = "Script Woonkamer",
        },
		
	execute = function(dz, item)
    
    w2k1 = dz.devices(w2k1)
    w2k2 = dz.devices(w2k2)
    w2k2dubbel = dz.devices(w2k2dubbel)
    wandlamp = dz.devices(wandlamp)
    lichtstraat = dz.devices(lichtstraat)
    wkhue = dz.devices(wkhue)
    keuken_spots = dz.devices(keuken_spots)
    kookeiland = dz.devices(kookeiland)
    eettafel = dz.devices(eettafel)
    spotshal = dz.devices(spotshal)
    lampenwk = {wandlamp, lichtstraat, wkhue, keuken_spots, kookeiland} --alle lampen in woonkamer
    
--Verlichting hal    
    if item == w2k1 then
        spotshal.switchOn()
    end

    if item == w2k2 then
        spotshal.switchOff()
    end

--Verlichting woonkamer alles uit
    if item == w2k2dubbel then 
        dz.devices().filter(lampenwk).forEach(function(device)
        device.switchOff().checkFirst()
        end)
        dz.openURL({url = 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=130&switchcmd=Off'}) --workaround eettafel
    end

end
}

Re: Set filter without idx numbers

Posted: Thursday 09 November 2023 10:03
by Tukker
You can use the names you gave to the switches or change those.
local fancy_name = "wkhue"
No need for IDX.
Dzvents is so use friendly.

Maybe (I'm not sure) you're mixing up the local names with the switch names right now.

Re: Set filter without idx numbers

Posted: Thursday 09 November 2023 10:13
by fvdp80
The thing i would like to know is:

Code: Select all

local kookeiland = 78

Code: Select all

kookeiland = dz.devices(kookeiland)

Code: Select all

lampenwk = {wandlamp, lichtstraat, wkhue, keuken_spots, kookeiland}
In my script, if I start with declaring idx 78 as kookeiland, I would like to use the definition "kookeiland" in the filter part without looking at idx or switch name.
The only reference should be between the idx and the given name in the script.

Re: Set filter without idx numbers

Posted: Thursday 09 November 2023 13:01
by waltervl
You are now mixing up the variable kookeiland for 2 types.
If you want to use something like this, use 2 different variable names. For example

Code: Select all

dev_kookeiland = dz.devices(kookeiland)
or
kookeiland = dz.devices(kookeiland-idx)

Re: Set filter without idx numbers

Posted: Thursday 09 November 2023 16:36
by Kedi
Why not use the syntax: dz.devices("kookeiland")
I always use the name of the device without ever using the idx of any device

Re: Set filter without idx numbers

Posted: Friday 10 November 2023 12:28
by fvdp80
Oke, i think i understand now. So this should work:

Code: Select all

 
 wkhue = dz.devices(8)
 keuken_spots = dz.devices(73)
 kookeiland = dz.devices(78)
 eettafel = dz.devices(130)
 spotshal = dz.devices(97)
 lampenwk = {wandlamp, lichtstraat, wkhue, keuken_spots, kookeiland} --alle lampen in woonkamer

Re: Set filter without idx numbers  [Solved]

Posted: Friday 10 November 2023 16:34
by Kedi
No this works without ever using the idx

Code: Select all

lampenwk = {"wandlamp", "lichtstraat", "wkhue", "keuken_spots", "kookeiland"}
Or if you want to use variables.

Code: Select all

wkhue = dz.devices("wkhue")
 keuken_spots = dz.devices("keukenspots")
 kookeiland = dz.devices("kookeiland")
 eettafel = dz.devices("eettafel")
 spotshal = dz.devices("spotshal")
 lampenwk = {wandlamp, lichtstraat, wkhue, keuken_spots, kookeiland} --alle lampen in woonkamer
 
Use (ofcourse) the realnames of the devices between "" as seen from the GUI