zigbee2mqtt icon indicating copy to clipboard operation
zigbee2mqtt copied to clipboard

Aqara switch click received multiple times

Open WalterWampe opened this issue 8 years ago • 35 comments

Messages from my switch are received and translated to mqtt several times, thus making it impossible to toggle entities reliably. Is there any setting I overlooked to debounce?

'2018-08-21T10:16:37.785Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"battery":"100.00","voltage":3065,"click":"single"}'

'2018-08-21T10:16:38.194Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"battery":"100.00","voltage":3065,"click":"single"}'

'2018-08-21T10:16:39.030Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"battery":"100.00","voltage":3065,"click":"single"}'

WalterWampe avatar Aug 21 '18 10:08 WalterWampe

Could you provide your complete zigbee2mqtt (post it on: https://pastebin.com).

Koenkk avatar Aug 21 '18 11:08 Koenkk

log: https://pastebin.com/L3XwpEH9 config: https://pastebin.com/2DJ0scqY

WalterWampe avatar Aug 21 '18 13:08 WalterWampe

Could you set log_level to debug and provide the log when the error happens? (in the current log I dont see any "click":"single")

Koenkk avatar Aug 22 '18 07:08 Koenkk

Sure! My fault, here it is, freshly from restart with debug log_level:

https://pastebin.com/XdSvsTui

WalterWampe avatar Aug 22 '18 08:08 WalterWampe

From the logs I don't see any weird behaviour. The switch report that is has been clicked and zigbee2mqtt passes this through:

2018-08-22T08:30:37.432Z - debug: Recieved zigbee message with data {"cid":"genOnOff","data":{"onOff":1}}    <---- Message from switch that it has been clicked
2018-08-22T08:30:37.437Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"click":"single"}'

Can you try repairing?

Koenkk avatar Aug 24 '18 21:08 Koenkk

I have an idea on how this problem can appear (though probably I am wrong).

I believe the problem is related to how zigbee2mqtt handles mqtt messages when it is reporting to HA. It needs to report the full data set (all the attributes) in each message but the device sends at least sometimes sends just a subset of all attributes in a single message. I think the initial log fragment was taken with reporting to HA enabled and the device sent three separate messages to the bridge (for battery level, for voltage and for click itself), but the bridge reported to HA the full data set with each mqtt message replacing missing attributes with their cached values. So three separate identical messages were sent.

The last log was probably written with reporting to HA switched off, so only click message got sent with mqtt.

I observe some similar behavior with Xiaomi presense and illumination: the sensor sends two different messages in a quick sequence: one for illumination, another for motion. zigbee2mqtt sends two messages with full attribute set so the first message goes with the new illumination value and old presense value (usually false) and immediately a new message is sent with the same illumination and new presense (true). In many cases both illumination values are the same.

Ton1965 avatar Aug 27 '18 19:08 Ton1965

yep that happens also with aqara sensors (temp/hum/press), wondering if it's a linkquality issue as it does not happen with all my sensors

lolorc avatar Aug 28 '18 07:08 lolorc

Can you try repairing?

I was away for a few days - just repaired - unchanged!

The last log was probably written with reporting to HA switched off, so only click message got sent with mqtt.

negative - configuration unchanged despite debug level.

WalterWampe avatar Aug 28 '18 08:08 WalterWampe

I do not quite understand then. Your initial log contains the following:

> 2018-08-21T10:16:37.785Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"battery":"100.00","voltage":3065,"click":"single"}'

You last log shows the following:

2018-08-22T08:30:37.432Z - debug: Recieved zigbee message with data {"cid":"genOnOff","data":{"onOff":1}}    <---- Message from switch that it has been clicked
2018-08-22T08:30:37.437Z - info: MQTT publish, topic: 'zigbee2mqtt/zigbee_switch_bad', payload: '{"click":"single"}'

The format of the MQTT message looks very different. The last message does not have any info about the battery and voltage.

Probably zigbee2mqtt formats messages differently depending on whether the debug mode is set. But then the question arises which message exactly will be sent to HA in both cases?

Ton1965 avatar Aug 28 '18 09:08 Ton1965

Could you share your log when you see these duplicate events? (with debug on)

Koenkk avatar Aug 28 '18 11:08 Koenkk

Sure, anything to help!

https://pastebin.com/wizWKmFE

I marked one of those events at the end.

The format of the MQTT message looks very different. The last message does not have any info about the battery and voltage.

It might be that it is what you said above:

It needs to report the full data set (all the attributes) in each message but the device sends at least sometimes sends just a subset of all attributes in a single message.

I guess after restarting the container ( I run it in docker) the attributes for battery and voltage have not been submitted to zigbee2mqtt before and with pressing the button and thus have not been cached and are not transmitted via the mqtt payload.

But guys.... If I am the only one with this problem, don't bother yourself too much with it. zigbee2mqtt is very nice and flawlessly working despite this bouncy problem I have here.

I managed to implement a debounce in my home assistant instance in the meantime.

WalterWampe avatar Aug 28 '18 18:08 WalterWampe

@Koenkk I do not no the exact reason for the problem @WalterWampe has but I can show something similar that probably should also be taken care of.

Here is a piece of the debug log obtained from my RTCGQ11LM device:

2018-8-28 21:49:46 - info: MQTT publish, topic: 'zigbee2mqtt/bridge/state', payload: 'online'
2018-8-28 21:49:47 - info: MQTT publish, topic: 'zigbee2mqtt/test_aqara', payload: '{"illuminance":213,"linkquality":98,"occupancy":true,"battery":"100.00","voltage":3035}'
2018-8-28 21:50:03 - debug: Recieved zigbee message with data {"cid":"msIlluminanceMeasurement","data":{"measuredValue":221}}
2018-8-28 21:50:03 - info: MQTT publish, topic: 'zigbee2mqtt/test_aqara', payload: '{"illuminance":221,"linkquality":96,"occupancy":true,"battery":"100.00","voltage":3035}'
2018-8-28 21:50:03 - debug: Recieved zigbee message with data {"cid":"msOccupancySensing","data":{"occupancy":1}}
2018-8-28 21:50:03 - info: MQTT publish, topic: 'zigbee2mqtt/test_aqara', payload: '{"illuminance":221,"linkquality":96,"occupancy":true,"battery":"100.00","voltage":3035}'
2018-8-28 21:50:03 - debug: Recieved zigbee message with data {"cid":"msIlluminanceMeasurement","data":{"measuredValue":221}}
2018-8-28 21:50:03 - debug: Recieved zigbee message with data {"cid":"msOccupancySensing","data":{"occupancy":1}}
2018-8-28 21:51:04 - info: MQTT publish, topic: 'zigbee2mqtt/test_aqara', payload: '{"illuminance":221,"linkquality":96,"occupancy":false,"battery":"100.00","voltage":3035}'

At the start zigbee2mqtt announces that it is online, then immediately it sends the cached state of the device with all the attributes. After that it gets two zigbee messages in a quick succession: one for an illuminance measurement, another for occupancy. On each of these messages the bridge immediately publishes corresponding mqtt messages with the full state. In this particular case there some minor differences (like different illuminance values and link quality) but in general we have three almost identical messages sent to HA.

The first one of these messages (the cached state) is probably ok since it only happens at the very start of the operation but later when any movement is detected the device sends two zigbee messages and the bridge translates them into two full mqtt messages. Probably at least for this device type these two zigbee messages should be combined into one mqtt message.

What do you think?

Ton1965 avatar Aug 28 '18 19:08 Ton1965

The idea of zigbee2mqtt is that each zigbee message will produce one mqtt message. I don't want to wait for the device to check if it will send anymore updates, as this may introduce lag.

Koenkk avatar Aug 29 '18 06:08 Koenkk

I would agree to that but... :-)

Original Xiaomi hub sends just a single update message to clients with both illumination and occupancy. It looks like it is by design that at least this particular device measures illuminance and occupancy at the same time moment but for some reason splits the info into two zigbee messages.

My suggestion is to allow a device zigbee message converter to return an empty message (nothing is to be sent to mqtt). This way the converter for this particular device could just cache the illuminance and send nothing. On receive of occupancy message the converter would send the same message it sends now.

No wait time will be introduced in this way. It is documented for this particular device that it does not send anything before occupancy is triggered, collects both data pieces at the moment, sends them and after that it sleeps for a minute.

I do not propose to do this for all devices but it might give some additional flexibility.

Ton1965 avatar Aug 29 '18 07:08 Ton1965

That means that no illuminance updates will be send until the occupancy changes, I think that's also not a solution.

What is the problem of having 2 seperate MQTT messages?

Koenkk avatar Aug 29 '18 07:08 Koenkk

@Koenkk is this behaviour new (because of the caching) or would it always have happened?

Sent with GitHawk

ryanbeaton avatar Aug 29 '18 08:08 ryanbeaton

@Koenkk, Sorry for the delay, I just wanted to do some experiments. Here are the results:

This device (RTCGQ11LM) does not send any illuminance updates without occupancy changing. I set the log level to debug and had it run for several hours without triggering occupancy. The only message it sends is the general state every 55 minutes. As far as I understood from zigbee2mqtt code the only attribute that gets decoded from this message is battery voltage. There is no illuminance sent.

I am actually fine with the two messages. One just needs to keep in mind that not all values are really relevant when a message is received: you cannot rely on illuminance unless you have occupancy triggered. So you can run your automations only based on occupancy trigger and use illuminance only as a condition. This is actually why I am currently looking at some other illuminance sensors: I want to have the current illuminance in real time.

Ton1965 avatar Aug 30 '18 10:08 Ton1965

@ryanbeaton the separate messages were always there. I think there is a config option to avoid resending previous values on new messages just because some didn't want the values resent.

ciotlosm avatar Sep 03 '18 11:09 ciotlosm

looks like https://github.com/Koenkk/zigbee2mqtt/commit/04b2f24d6b2d811ad31bb0b23d11f73e7067b391 is the culprit. I think it's a good idea to store values in a cache, but it's not a good idea to update every mqtt messages with outdated data. see my comments https://github.com/Koenkk/zigbee-shepherd-converters/pull/63#issuecomment-418763082

The idea of zigbee2mqtt is that each zigbee message will produce one mqtt message. I don't want to wait for the device to check if it will send anymore updates, as this may introduce lag.

agreed ! ;-) guess it's a tricky subject, I don't need the persistence of one mqtt message with all attributes merged from all the device's clusters.

lolorc avatar Sep 05 '18 15:09 lolorc

@ciotlosm what @lolorc has written is what I was getting at. Happy to get seperate message for each attribute but each message should not have cached attributes put in to make a full set

Sent with GitHawk

ryanbeaton avatar Sep 05 '18 15:09 ryanbeaton