zigbee2mqtt icon indicating copy to clipboard operation
zigbee2mqtt copied to clipboard

Proposal how to handle duplicate zigbeeModels

Open middelink opened this issue 6 years ago • 48 comments

Greetings,

I recently bumped into an Aliexpress Lonsonho? 1-gang switch which reports TS0001 as its zigbee model ID. So I joined it to my network and it reported itself as a "CR Smart Home Valve Control". That cannot be right... Well, it is. It seems there is no handling for duplicate zigbee Models, the assumption being, like model numbers, that all manufactures work together to have each zigbeeModel unique.. Right :) For now it looks like TS0001 will be the one device having this issue, with 3 manufactures, as there is already a different TS0001 commented out. But there is no guarantee it will not happen for other devices.

So I can see only one way to solve this, by including the manufacturer Name in the FindByZigbeeModel() search. Downside is that existing devices don't have manufactures listed, so it needs to fall back to the current behavior if that is the case. We can change the uniqueness test to assert that the zigbeeModel is unique per manufacturer.

So, how to add this manufacturer name? Keep in mind, as we allow multiple zigbeeModels per device, we need multiple zigbeeManufactuerers as well, which need to pair up. E.g. { zigbeeModel: ['TS0012', 'TS0001'], zigbeeManufactuerer: ['Something', 'Spooky'], } or we encode it in the model name with a special separator. E.g. { zigbeeModel: ['TS0012/Something', 'TS0001/Spooky'], } or, uhm. tuples? E.g. { zigbeeModel: [ ['TS0012','Something'], ['TS0001','Spooky'] ], }

I think I prefer the first, as it reads the simplest and as devices.js needs to be maintained by non-programmers, simplest is a good criteria...

Any thoughts? Suggestions? I'm happy to provide patches if this meets approval.

middelink avatar Jan 19 '20 10:01 middelink

These devices are actually all from TUYA, the problem is that the manufacturerName is also the same :(

Koenkk avatar Jan 19 '20 12:01 Koenkk

Ah, you're saying the Zemismart TS0001 == CR Smart Home TS0001 == my "Wireless Smart Switches" TS0001? :(

If so, can we at least combine to one entry with the same fromZigbee and toZigbee entries? or do they differ as well? :(

Let me for prosperity sake include the database entry, so we can figure out if the others are really identical or not:

{"id":43,"type":"Router","ieeeAddr":"0xec1bbdfffe0f96d1","nwkAddr":21142,"manufId":0,"manufName":"_TYZB01_seqwasot","powerSource":"Mains (single phase)","modelId":"TS0001","epList":[1],"endpoints":{"1":{"profId":260,"epId":1,"devId":256,"inClusterList":[0,3,4,5,6],"outClusterList":[25],"clusters":{"genBasic":{"attributes":{"modelId":"TS0001","manufacturerName":"_TYZB01_seqwasot","powerSource":1,"zclVersion":3,"appVersion":66}},"genOnOff":{"attributes":{"onOff":0,"onTime":0,"offWaitTime":0}}},"binds":[{"cluster":6,"type":"endpoint","deviceIeeeAddress":"0x00124b001949ee82","endpointID":1}]}},"appVersion":66,"zclVersion":3,"interviewCompleted":true,"meta":{},"lastSeen":null}

middelink avatar Jan 19 '20 12:01 middelink

What type of device is your TS0001? Can you provide an URL?

Koenkk avatar Jan 20 '20 16:01 Koenkk

It's a 1-gang, L+N switch for EU, (so it fits existing EU home wall mounts) https://www.aliexpress.com/item/4000298926256.html

middelink avatar Jan 20 '20 16:01 middelink

Taken from here: https://github.com/Koenkk/zigbee-herdsman-converters/issues/810#issuecomment-565612267

database.db for Zemismart:

{"id":2,"type":"Router","ieeeAddr":"0xec1bbdfffe687fad","nwkAddr":45897,"manufId":0,"manufName":"_TYZB01_3xe7wzpw","powerSource":"Mains (single phase)","modelId":"TS0001","epList":[1],"endpoints":{"1":{"profId":260,"epId":1,"devId":256,"inClusterList":[0,3,4,5,6],"outClusterList":[25],"clusters":{"genBasic":{"attributes":{"modelId":"TS0001","manufacturerName":"_TYZB01_3xe7wzpw","powerSource":1,"zclVersion":3,"appVersion":66}}},"binds":[]}},"appVersion":66,"zclVersion":3,"interviewCompleted":true,"meta":{}}

As you can see the manufacturerName is the same, AFAIK the part after _TYZB01_ is always random and can even differs per device.

Koenkk avatar Jan 21 '20 19:01 Koenkk

Yeah, I guess there is not much to gain except for making the name look prettier. Ow well, lets table this then till we really bump into a double modelID :)

Feel free to close the topic if you agree.

middelink avatar Jan 21 '20 22:01 middelink

I would like to keep this open to gather more database.db entries for these devices (to see if we can find a pattern in it).

Koenkk avatar Jan 22 '20 16:01 Koenkk

@Koenkk just a note for Z-Stack 3.1.0-3.5.0, Model ID is not a require one, you can see it here http://e2e.ti.com/support/wireless-connectivity/zigbee-and-thread/f/158/t/875025 They have other way to handle what a device is, you can check TI Sensor to cloud project.

dzungpv avatar Jan 24 '20 19:01 dzungpv

Hmm, that is interesting. Looking through the the NXP ZCL, I also see the attribute is indeed not mandatory. (my only reference i had handy).

I'm unable to find anything useful on "TI Sensor to cloud" except a bunch of Bluetooth and 1352 development boards. Do you have a link handy?

middelink avatar Jan 25 '20 01:01 middelink

I was thinking that maybe we can distinguish between devices when they have different endpoints, or different supported clusters, appVersion, zclVersion, etc.

But even this is identical for these 2 devices, however this may not be the case if two devices are sharing the modelID where one is a bulb and the other one is just a switch. The switch would then e.g. not support the brightness cluster.

Koenkk avatar Jan 25 '20 09:01 Koenkk

That's how SmartThings is working... there's a feature called fingerprinting which is doing that: https://docs.smartthings.com/en/latest/device-type-developers-guide/definition-metadata.html#fingerprinting

carldebilly avatar Jan 25 '20 12:01 carldebilly

Hi,

Interesting, but that will be a fairly big configuration shift. Instead of well, findByModelID(), the function now needs to be given the full device, in order to match on some (/every possible [1])? attribute. It would save a tremendous amount of support though, as most devices would just fall into place and one when ppl complain "the battery voltage is always empty", we can hunt down what is special about it.

So the device list will now be a list of "handlers", say "Generic Light", with the proper fromZigbee and toZigbee lists and whats not. Such a handler will also have a (list of) finger prints, which needs to be intelligently matched against the devices. And the more matches a finger print has the more preferential it will be, say the Common Light matches just on inclust[6], a Common Dimmable Light matches on incluster [6,8] and a "Common Dimmable Colortemp Light` maches on incluster [6,8,0x300]. But since Ikea has to be special, it can have a "Ikea Dimmable Colortemp Light" which might have a specialized fromZigbee in there.

What will be crucial here is that at interview time, when we have a bunch of more information than we retain in the database (I think), we need to find the correct handler and either retain it's handler_id in the device database, or keep enough of the attributes[1] around so we can find the same handler for the device over and over again.

It might be possible to go a step further and discover multiple handlers for a device, for example, if a device is both a relay (on/off) and a switch (on/off switch) or even multiple switches. That way most of out current devices would collapse into a few on/off handlers (a single ssIasZone and ssIasWd handler, yes!) and some specialized battery voltage/percentage handlers. Downside is that the configuration would be harder to explain to a casual "editor", as their device would be all over N handlers. On the other hand, it should be easy enough to make a test which yells loudly when two handlers have the same fingerprints. Maybe we should even require some database entry to be added for each device, so we can assert each device will always end up with at least one handler.

I guess the handlers also need to way to inform the higher up zigbee2mqtt stack what this handler supports (not just a text field supports, but a list of constants), so the correct home assistant configuration objects can be created. Hopefully these are pretty generic (should be as the from/toZigbee should have massaged the device into a "normal" workflow.

~Maybe we can even leverage the full SmartThings definitions, their capabilities can then project into Home Assistant items, possible augmented by attributes. Although I could not find their devices database online, so that might prove futile if it isn't public and freely available. Oh, and it would need some kind of specialized parser for the format as well. So scrap that.~

Thoughts?

[1] looking at the smartthings page, it seems they only use the in/out clusters and the manufacturer and modelid as entries. Which might be identifying enough.

middelink avatar Jan 25 '20 17:01 middelink

I don't think we should go that far and I want to go for a much simpler approach as the zigbeeModel has worked fine for now (with over 550+ supported devices). It's also much more readable and easier to understand. There are 2 known cases now where it was not sufficient:

  • OP of this issue -> turns out that even the endpoints and supported clusters are the same -> impossible to distinguish
  • https://github.com/Koenkk/zigbee2mqtt/issues/2677 : possible to distinguish using fingerprint below

Let's say we have 2 device definitions:

// device A
{
  zigbeeModel: ["mymodelID"],
  ...
},
// device B
{
  zigbeeModel: ["mymodelID"],
  fingerpint: {endpoints: {1: {inputClusters: ["genBasic", "genLevelCtrl"]]}}
  ...
}
  • fingerprint is optional and will only be added once we run into zigbeeModel duplications (so only very few devices will have this)
  • following device {modelID: "mymodelID", endpoints: {1: {inputClusters: ["genBasic", "genLevelCtrl", "lightingColorCtrl"]}}} -> will match with device A
  • following device {modelID: "mymodelID", endpoints: {1: {inputClusters: ["genBasic", "genLevelCtrl", ]}}} -> will match with device B

Koenkk avatar Jan 27 '20 11:01 Koenkk

I'm not sure if this is going to work for my device from #2677. The device also identifies itself as being capable of handling HSV attributes. I'd need to check logs/traffic, but I'm pretty sure that the Hue bridge doesn't have a DB like Z2M.

Probably, Paul Neuhaus just uses the same Jennic Zigbee interface over and over again. (Pretty bad for a €329 light...)

TilmanK avatar Jan 29 '20 18:01 TilmanK

@TilmanK so it also has lightingColorCtrl (aka 768) in inClusterList?

Koenkk avatar Jan 29 '20 19:01 Koenkk

I'd need to capture that traffic again, since I didn't get the appropriate message (I need to move the stick closer...).

But like I said, my Hue bridge also thinks that it supports color and happily reads color properties: Frame 1013: 98 bytes on wire (784 bits), 98 bytes captured (784 bits) on interface \.\pipe\zboss_sniffer, id 0 ZBOSS dump, IN, page 0, channel 25 IEEE 802.15.4 Data, Dst: 0x0001, Src: 0x000a ZigBee Network Layer Data, Dst: 0x0001, Src: 0x000a ZigBee Application Support Layer Data, Dst Endpt: 64, Src Endpt: 2 ZigBee Cluster Library Frame, Command: Read Attributes Response, Seq: 121 Frame Control Field: Profile-wide (0x18) Sequence Number: 121 Command: Read Attributes Response (0x01) Status Record Attribute: Color Temperature (0x0007) Status: Success (0x00) Data Type: 16-Bit Unsigned Integer (0x21) Color Temperature: 250 [Mired] (4000 [K]) Status Record Attribute: Enhanced Current Hue (0x4000) Status: Success (0x00) Data Type: 16-Bit Unsigned Integer (0x21) Enhanced Hue: 7072 Status Record Attribute: Current Saturation (0x0001) Status: Success (0x00) Data Type: 8-Bit Unsigned Integer (0x20) Saturation: 171 Status Record Attribute: Current X (0x0003) Status: Success (0x00) Data Type: 16-Bit Unsigned Integer (0x21) X: 0,4110 Status Record Attribute: Current Y (0x0004) Status: Success (0x00) Data Type: 16-Bit Unsigned Integer (0x21) Y: 0,3958 Status Record Attribute: Color Mode (0x0008) Status: Success (0x00) Data Type: 8-Bit Enumeration (0x30) Color Mode: Hue and Saturation (0x00) Status Record Attribute: Color Loop Active (0x4002) Status: Success (0x00) Data Type: 8-Bit Unsigned Integer (0x20) Active: False

I'll be sure 100% when I find the time to capture the logs for the timeout issue.

TilmanK avatar Jan 29 '20 20:01 TilmanK

No need to sniff, you can find it in database.db.

Koenkk avatar Jan 29 '20 20:01 Koenkk

{"id":29,"type":"Router","ieeeAddr":"0x00158d0002a6d294","nwkAddr":41527,"manufId":4151,"manufName":"Neuhaus Lighting Group ","powerSource":"Mains (single phase)","modelId":"NLG-RGBW light ","epList":[1,2],"endpoints":{"1":{"profId":49246,"epId":1,"devId":4096,"inClusterList":[4096],"outClusterList":[4096],"clusters":{},"binds":[]},"2":{"profId":49246,"epId":2,"devId":528,"inClusterList":[0,4,3,6,8,5,768],"outClusterList":[0,25],"clusters":{"genBasic":{"attributes":{"modelId":"NLG-RGBW light ","manufacturerName":"Neuhaus Lighting Group ","powerSource":1,"zclVersion":1,"appVersion":1,"stackVersion":2,"hwVersion":1,"dateCode":"20150821","swBuildId":"1001-0007"}}},"binds":[]}},"appVersion":1,"stackVersion":2,"hwVersion":1,"dateCode":"20150821","swBuildId":"1001-0007","zclVersion":1,"interviewCompleted":true,"meta":{},"lastSeen":1579971066266}

Yes :/ Like I said, I believe they are using the same Zigbee module for every device.

TilmanK avatar Jan 30 '20 17:01 TilmanK

So it has 768 (Colour Control Cluster) which includes attributes for HSV. So I think it would work. In this case the device fingerprint would contain something like this:

{
  zigbeeModel: ["NLG-RGBW light"],
  fingerpint: {endpoints: {2: {inputClusters: ["genBasic", "genOnOff", "genLevelCtrl", "lightingColorCtrl"]}}
  ...
}

it seems that endpoint 1 only contains touchlink and endpoint 2 the interesting parts of the device. I also find it interesting the inclusters on ep2 are completely unsorted. guess they made their own zigbee stack. Which seems reasonable for the 329 euri expense :)

middelink avatar Jan 30 '20 18:01 middelink

So, to return to the original thread, I summarize:

  1. add fingerprint attributes to all duplicates entries, allowing them to specify all attributes from a device. Note I'm allowing for all kind of attributes, even though many of them never be used, but for the user to bump into an identical device (model, clusters), they want to discern on say appversion or datecode without waiting for a new dev release.
  2. new function in zigbee-herdsman-converters to find a device by well, device(entity? what is the right nomenclature here?). It will try a normal map-lookup on just the zigbeeModel, if it hits more than one device, it checks the fingerprints.
  3. adjust tests to yell about devices which are not unique, either by zigbeeModel without fingerprints or by multiple zigbeeModels with fingerprints which match.

And finally, as a general statement, zigbeeModel is actually just another fingerprint attribute, it does not require the special handling we give it here.

Ok? good plan?

middelink avatar Jan 30 '20 19:01 middelink