DZVents: have devices() and groups(), etc. accept sets

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

DZVents: have devices() and groups(), etc. accept sets

Post by rrozema »

I would like it very much if the following syntax could be made possible:

Code: Select all

-- example using an explicit set of names.
local mysensors = domoticz.devices({ 'sensor1', 'sensor2', 'sensor3' })

-- example using a variable that is a set of names
local switch_names = { 'switch1', 'switch2', 'switch8', 'switch 3' } 
local myswitches = domoticz.devices(switch_names)

-- or using idx values:
local myothersensors = domoticz.devices({ 23, 31, 221, 222 })

-- or both mixed:
local mythingies = domoticz.devices( { 'switch3', 43 })
Currently domoticz.devices(<idx>) or domoticz.devices(<name>) returns a single device from the set of devices, and domoticz.devices() returns the complete set of devices. domoticz.devices( <set of device names>) or domoticz.devices( <set of idx values>) would return only those devices from domoticz.devices() that have a name or idx value specified in the set. This allows for much easier coding of set operations in combination with the iterator functions .foreach(), .filter(), .reduce(), etc..

Some additional rules to make the behavior unambigious:
- If an empty set is passed in (f.e. domoticz.devices({})), an empty set should be returned. Do not raise an error like devices( <name/idx> ) does.
- if a name or an idx value is in the specified set that does not exist on a device in domoticz, ignore such non-existent names and idx-es. Do not raise an error. If you want to make sure all devices in a set exist, an easy check can be built using .filter(), or .foreach(). If however domoticz.devices({ <empty set or set with invalid name or idx> }) would raise an error, it is very hard to detect the situation and/or work around the error.
- the set can contain both idx values and names intermixed.
- if the same device can be identified by one or more names or idx values in the set, such device is only included once in the returned set. In other words, domoticz.devices({<set>}) always returns a set of unique devices.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by waaren »

@rrozema
I probably miss something or do not completely understand but if so can you please elaborate a bit on your suggestion ?
In dzVents wiki I found this and isn't this, at least partially, what you are looking for ?

Code: Select all

    local livingLights = {
        'window',
        'couch',
        33, -- kitchen light id
    }
    local lights = domoticz.devices().filter(livingLights)
    lights.forEach(function(light)
        -- do something
        light.switchOn()
    end)

Of course you can chain:

    domoticz.devices().filter(function(device)
        return (device.lastUpdate.minutesAgo > 60)
    end).forEach(function(zombie)
        -- do something with the zombie
    end)
    
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by rrozema »

Yes, that mostly does what I asked for. If however you have to do that several times, the scripts become unreadable very quickly. You have to go through several lines of code before you understand it's not doing anything functionally other than collecting the devices.

My proposal makes for a much more elegant solution, keeping the meaning of the code very clear: "give me this set of devices". It just completes the toolset, devices() = "give me all devices", devices(<idx/name>) = "give me one device", devices(<set>) = "give me these devices". Plus if we make devices(<set>) part of the dzVents toolset, danny may at some point in time find that the internal implementation can be improved on and when he implements it, all our new but also existing scripts would benefit from such improvement right away.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by waaren »

Thanks for clarifying.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by dannybloe »

It's even simpler:

Code: Select all

local devs = domoticz.devices().filter({'deviceA',  'deviceB'})
There is no need for your suggestion as it is already possible imho.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by rrozema »

That is a nice one indeed. Yet it does not fit the pattern set by "getting all devices: .devices()" and "get one device: .devices(key)". Just the mere fact that you need to show this gimmick to us here is proof for that. My proposal is much more intuitive and adding it should not cause any backward compatibilty issues as I see it.

So, please consider adding it in a future release. You'll make a great tool even greater😀
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by dannybloe »

So if I understand you correctly with

Code: Select all

local devs = { 'a', 'b', 3 }
this

Code: Select all

local devices = domoticz.devices(devs)
would return the exact same set as with

Code: Select all

local devices = domoticz.devices().filter(devs)
?
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: DZVents: have devices() and groups(), etc. accept sets

Post by rrozema »

Yes, that's all I'm asking for 👍
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest