zigbee2mqtt icon indicating copy to clipboard operation
zigbee2mqtt copied to clipboard

zigbee2mqtt v1.7.0 issue with friendly names

Open pkedvessy opened this issue 7 years ago • 10 comments

Sending zigbee message to Xiaomi wall switches using friendly name in the mqtt topic stopped working with 1.7.0. There was no issue in 1.6.0 at all.

Zigbee2Mqtt & coordinator versions: {"version":"1.7.0","commit":"551a58a","coordinator":{"type":"zStack12","meta":{"transportrev":2,"product":0,"majorrel":2,"minorrel":6,"maintrel":3,"revision":20190608}},"log_level":"debug","permit_join":true}

Device configuration in configuration.yml (base_topic: zigbee2mqtt):

  '0x00158d0003151f2f':
    friendly_name: ground_floor/kitchen/wall_switch/2
    retain: true

Device: Aqara double key wired wall switch (QBKG12LM)

Mqtt topic zigbee2mqtt/ground_floor/kitchen/wall_switch/2/left/set

Data {"state": "OFF"}

Error:

zigbee2mqtt:error 2019-11-10T19:23:01: Failed to call 'EntityPublish' 'onMQTTMessage' (TypeError: Cannot read property 'ID' of undefined
    at Object.entries.find (/opt/zigbee2mqtt/lib/zigbee.js:184:106)
    at Array.find (<anonymous>)
    at Zigbee.resolveEntity (/opt/zigbee2mqtt/lib/zigbee.js:184:76)
    at EntityPublish.onMQTTMessage (/opt/zigbee2mqtt/lib/extension/entityPublish.js:55:36)
    at Controller.callExtensionMethod (/opt/zigbee2mqtt/lib/controller.js:320:44)
    at Controller.onMQTTMessage (/opt/zigbee2mqtt/lib/controller.js:239:14)
    at MQTT.emit (events.js:189:13)
    at MQTT.onMessage (/opt/zigbee2mqtt/lib/mqtt.js:86:14)
    at MqttClient.emit (events.js:189:13)
    at MqttClient._handlePublish (/opt/zigbee2mqtt/node_modules/mqtt/lib/client.js:1162:12))

Using the device id as a topic is still workin eg.: zigbee2mqtt/0x00158d0003151f2f/left/set

Status reports are still available with the friendly name eg.: zigbee2mqtt/ground_floor/kitchen/wall_switch/2

I tried to rejoin the switch but friendly name issue is still exists.

pkedvessy avatar Nov 10 '19 19:11 pkedvessy

I am also using '/' as a room separator and had some issues during the dev phase, for me it was working after some changes done by Koen. But I only have 2 levels while you seem to have 4... this may be the problem.

Can you try with the friendly_name: ground_floor/kitchen/wall_switch_2 to see if it is a divider limit or something else.

sjorge avatar Nov 10 '19 21:11 sjorge

Good catch @sjorge ! Reducing the number of separators in the friendly name solved my issue. @Koenkk is there a reason on limiting the depth of mqtt topic? If so, can we have a notice in the documentation?

pkedvessy avatar Nov 10 '19 22:11 pkedvessy

https://github.com/Koenkk/zigbee2mqtt/blob/dev/lib/extension/entityPublish.js#L24 Seems to hint that it will accept 20 x '/' but something else might be causing the issue... I think it might be the end point selector since the switch seems to have a left and right endpoint.

I have no such devices so I did not run into it during the dev phase.

sjorge avatar Nov 10 '19 22:11 sjorge

@sjorge I'm fine with reducing the depth of mqtt topics. But this restriction should be documented or fixed if there is no reason. Thanks for your help!

pkedvessy avatar Nov 10 '19 22:11 pkedvessy

I think the idea still is to limit it to 20ish, yeah the exact value should be documented. But I don't think you should be hitting it yet either. I'm poking around in entityPublish a bit.

Not seeing an immediate issue 🤔 I was thinking the regex parser for topic might be chocking on it, but it's fine.

regex:

^(.+?)(?:\/(left|right|center|bottom_left|bottom_right|l1|l2|default|top_left|top_right|white|rgb|system|top|bottom|center_left|center_right|ep1|ep2))?\/(get|set)(?:\/(.+))?

result for your string:

ID: zigbee2mqtt/ground_floor/kitchen/wall_switch/2
postfix: left
type: set
attribute: ''

sjorge avatar Nov 10 '19 22:11 sjorge

@sjorge Yeah, 20ish should be more then enough 😄 Thanks again for your quick and usefull response.

pkedvessy avatar Nov 10 '19 22:11 pkedvessy

After more digging it fails here: https://github.com/Koenkk/zigbee2mqtt/blob/dev/lib/zigbee.js#L184, specifically endpoint.ID as endpoint doesn't seem to have an ID property.

That's where I have to throw in the towel I am afraid, I'm still learning my way around zigbee2mqtt and the multiple endpoints and how the name e.g. left is mapped to the actual endpoint is where I got hopelessly lost, hopefully Koen's fix will teach me more so next time I can get further.

It looks to be either the postfix -> postfixByNumber mapping thing around line 145-149 or 167-177 where it is mapped to the actual endpoint. But on the surface with my knowledge it looks OK.

Bit surprising that one less '/' would make the difference here though 🤔

My long shot is that key = key.replace(`/${postfix}`, ''); might match both '/left' in something like 'my_room/left/my_device/left'. But that is not the case with your friendly_name.

sjorge avatar Nov 10 '19 22:11 sjorge

Fixed in the latest dev, the amount of slashes are not the cause of this issue, the bug occured due to /2/.

Koenkk avatar Nov 11 '19 16:11 Koenkk

After upgrading to the latest version I'm experiencing somewhat a similar issue.

I have a light bulb with a friendly_name: middle/livingroom/bulb/1 and it's registering and reporting the state properly, but failing when I try to change it (either by manually publishing or through the Homeassistant) z2m complains:

2019-11-27T19:24:34: Received MQTT message on 'zigbee2mqtt/middle/livingroom/bulb/1/set' with data '{"brightness": 100}'
2019-11-27T19:24:34: Entity 'middle/livingroom/bulb/1' is unknown

I see that it cuts /1 due to this (hence it can't find the entity further down): https://github.com/Koenkk/zigbee2mqtt/blob/b459c352191091af9b9eafec82a98f73069797e8/lib/zigbee.js#L146-L149

...I don't really know the back story of that postfix(es) logic, but would it make sense instead of trying to find a parsed topic in devices list try to find id/friendly_name in a given topic, somemthing like (simplified):

const entity = devices.find(d => key.startsWith(d.id) || key.startsWith(d.friendlyName))
const postfix = entity ? key.replace(entity.friendlyName, '').replace(entity.id, '') : ''

In this case a whitelist of postfixes should not be needed, @Koenkk what do you think?

viart avatar Nov 27 '19 20:11 viart

Zigbee2mqtt interpets the 1 as the endpoint. Allowing this would cause a lot of ambiguity, so friendly names ending with only a /NUMBER should not be allowed. Todo: add to docs and update code.

Koenkk avatar Nov 28 '19 18:11 Koenkk