Set filter without idx numbers  [Solved]

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

Moderator: leecollings

Post Reply
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Set filter without idx numbers

Post 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
}
HvdW
Posts: 504
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Set filter without idx numbers

Post by HvdW »

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.
Bugs bug me.
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Re: Set filter without idx numbers

Post 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.
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Set filter without idx numbers

Post 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)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Set filter without idx numbers

Post 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
Logic will get you from A to B. Imagination will take you everywhere.
fvdp80
Posts: 69
Joined: Tuesday 14 August 2018 8:22
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10303
Contact:

Re: Set filter without idx numbers

Post 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
Kedi
Posts: 536
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Set filter without idx numbers  [Solved]

Post 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
Logic will get you from A to B. Imagination will take you everywhere.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest