Why is dzvents device lookup using name instead of idx

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

Moderator: leecollings

User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by waltervl »

IDX is unique for devices. But there is also an IDX for scenes and groups, one for cameras, schedules , hardware gateways etc.

So from dzvents perspective the IDX should work as most commands address as specific group of Domoticz objects (devices, scenes, cameras etc). But again it does not work that way...
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
solarboy
Posts: 367
Joined: Thursday 01 November 2018 19:47
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.6
Location: Portugal
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by solarboy »

Ragdag wrote: Monday 29 June 2026 19:23
waltervl wrote: Monday 29 June 2026 19:14
solarboy wrote: Monday 29 June 2026 18:58 I always use device Id's for the reasons mentioned above.
IDX is unique, the name could not be. If the name is not unique then dzvents gets confused as it does not know what unique IDX to map to the name....

I use IDX in my dzvents scripts but you have to be aware you have to have unique device names too.
IDX is not unique, it is only unique for all devices under a single hardware item.
Also (the reason I started this thread to begin with), dzvents only uses the IDX to look up the device name, which is then used in the script. I learned that the hard way with device having the same name and dzvents not triggering the device I expected.
This is not the case. Each Idx corresponds with a unique device/endpoint regardless of hardware device. A device name however can (but should not) be re-used and not be unique.
Intel NUC with Ubuntu Server VM (Proxmox),mosquitto(docker),RFXtrx433E,zwavejsUI (docker),Zigbee2mqtt(docker),Harmony Hub plugin, Kodi plugin,Homebridge(docker)+Google Home,APC UPS,SMA Modbus,Mitsubishi MQTT, Broadlink,Dombus
User avatar
Ragdag
Posts: 205
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Ragdag »

IDX are not unique over your entire Domoticz, only within a Hardware Device
Image

Domoticz allows duplicate names, but it will break the dzvents scripts.
It would already be great if IDX was a combination of Hardware ID IDX + Device IDX, that way it would almost always be unique in the DB and we could move dzvents to purely look up and work on IDX instead of names.
The problem then is still scenes and switches, we coul maybe prefix a Scene IDX with a 2 and a Switch with a 1 or something
Kedi
Posts: 622
Joined: Monday 20 March 2023 14:41
Target OS: Linux
Domoticz version: latest
Location: Somewhere in NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Kedi »

Would that change not break that a lot of current scripts?
I am in favour of using names in dzVents script. It is more readable that way.
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
Ragdag
Posts: 205
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Ragdag »

Kedi wrote: Thursday 02 July 2026 12:58 Would that change not break that a lot of current scripts?
I am in favour of using names in dzVents script. It is more readable that way.
Yes that could break, but there are ways that we could fix that. Maybe, instead of hardware ID IDX being a number we could do letters that way if sdzvents encounters an didx that is only numbers it is old format, if it encounters AA2354 it is the new format. Or a splitting Characters like "-", so it becomes 1-2354
Just thinking out loud

I understand plain names are more human readable but since Domoticz allows duplicate names that is a sure way to get weird behaviour in dzvents.
You can still then do by name in your script you would just declare

Code: Select all

local DEVICE = {
    WASHING_MACHINE = 2357,
}
Kedi
Posts: 622
Joined: Monday 20 March 2023 14:41
Target OS: Linux
Domoticz version: latest
Location: Somewhere in NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Kedi »

I have over 400 devices in my Domoticz of which over 300 devices are used (some of them with an '$' infront) I hardly every experianced 'duplicate names' Once you know that that problem is there, you can just check with every device that is added.
Your solution of declare devices is just a lot of work for people. I have some 100+ dzVents scripts running.
A perhaps a better way is ask for a warning if you create a duplicate name for a device.
So I don't see any problem that justifies the change(s). ;)
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by waltervl »

Indeed, a duplicate device name check would solve this issue. I dont know if the name check has to go over all idx entries, so including Hardware gateways, scenes, groups, camera, timeplans, rooms etc. etc.
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: 622
Joined: Monday 20 March 2023 14:41
Target OS: Linux
Domoticz version: latest
Location: Somewhere in NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Kedi »

I think such a check takes micro-seconds over a database with 400 devices.
Just a warning (with idx of the duplicate) when the device is created would do the trick.

Code: Select all

SELECT COUNT(*) AS aantal FROM DeviceStatus WHERE Name = 'IngevuldeNaam';
Logic will get you from A to B. Imagination will take you everywhere.
User avatar
Ragdag
Posts: 205
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Ragdag »

Let me revisit my own opening posts here, because after digging into the code I was partly wrong and partly right, and I want to correct the record.

Where I was wrong: I said IDX is only unique within a hardware device. That is not correct. Device idx (DeviceStatus.ID) is a primary key, so it is unique across every device in the whole system, plugins included. Two devices can never share an idx. What I was actually looking at in my screenshot is that Scenes and Groups live in a separate table with their own id sequence, so a device idx and a scene/group idx can be the same number. That is a separate namespace overlap, not two devices sharing an idx. So my "make idx a HardwareID+DeviceIDX composite" idea was solving the wrong problem, and I am dropping it.

Where I was right: dzVents really does end up using the name, not the idx, to act on a device. But it is not the lookup, it is the action. Here is the actual path.

domoticz.devices(3081) returns the correct device by idx. Then .switchOff() throws the idx away. In switch_device.lua:

Code: Select all

function device.switchOff()
    return TimedCommand(domoticz, device.name, 'Off', 'device', device.state)
end
The command is keyed by device.name. That becomes a commandArray entry like ['Slaapkamer Luuk - Nachtlampje'] = 'Off', handed to the C++ side. There it first tries to read the key as a number:

Code: Select all

int deviceNo = atoi(deviceName.c_str());   // a name -> 0
if (deviceNo) { ... schedule by idx ... }  // skipped
Because the key is a name, that idx path is skipped and it falls back to resolving the name in SQL:

Code: Select all

SELECT ID FROM DeviceStatus WHERE (Name == 'Slaapkamer Luuk - Nachtlampje')
and it takes the first matching row. With a duplicate name that first row was my [W] usage sensor (idx 3079), not the switch (idx 3081). That is exactly why I got "sending switch commands for switch type 'sensor' is not supported": idx 3081's switchOff was executed against 3079. So the failure is real, I just had the cause slightly off. It is not idx being non-unique, it is the switch action dispatching by name.

What makes this worse is that it is inconsistent within the same adapter. quietOn()/quietOff() already use idx:

Code: Select all

.../json.htm?type=command&param=udevice&...&idx=' .. device.id
and setpoint uses device.id too. But switchOn, switchOff, dimTo, setLevel, switchSelector, toggleSwitch, open, close and stop all pass device.name.

So, two things that would actually fix this:

1. Make the switch/dim actions in dzVents key by idx instead of name (the C++ side already supports numeric keys). This makes domoticz.devices(idx):switchOff() reliable regardless of duplicate names.
2. waltervl, your duplicate-name warning on create/rename is still worth having as a guard, and it also protects the pure name-based scripts that Kedi and others prefer.
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by waltervl »

QuietOn()/quietOff() already use idx: This seems to be a workaround to use the API instead of the standard integration. User Waaren did that when the standard lua/dzvents integration had no function for this. Making things a little bit slower and not completely integrated.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
eightyeight
Posts: 3
Joined: Saturday 25 February 2023 17:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by eightyeight »

Just kicking this "issue" up. I am running into the same annoyance but, while calling the device with its IDX in dzvents.

zigbee2mqtt auto discovery creates multiple devices with the same name, so far no issue. But when calling the switch with
local TapwaterElement = domoticz.devices(1357)

TapwaterElement.switchOn().checkFirst()
I get
Zigbee2MQTT-AutoDiscovery: sending switch commands for switch type 'sensor' is not supported (yet...) (0xa4c1381a4dcaffff_current_zigbee2mqtt/Tapwater Element Sonoff)

Now I've renamed the auto created devices but I really loved IDX being the unique identifier used throughout all my scripts and having the names be just that, names in a GUI.

Was this behavior known and a related design choice or is this something to fix in domoticz/dzvents?
User avatar
waltervl
Posts: 7020
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2026.3
Location: NL
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by waltervl »

This was a design choice way back in the lua integration (dzvents is a extension oo the lua integration).
So to change a major change in this integration is needed (not impossible with current AI assistance)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
Ragdag
Posts: 205
Joined: Friday 30 March 2018 13:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Why is dzvents device lookup using name instead of idx

Post by Ragdag »

I've opened an issue with the offer to build a PR.
https://github.com/domoticz/domoticz/issues/7143
Post Reply