PatchworkBoy / homebridge-edomoticz

Domoticz Homebridge-Plugin
Other
118 stars 44 forks source link

Switching on by setLevel #221

Closed superjunky closed 3 years ago

superjunky commented 3 years ago

Hi,

Occasionally there is a problem with dimmers, when they are switched on by setting a new level. 50% of the time they jump back to the level when last active.

I tested the following scenario:

This does not occur 100% of the time. If it's not reproduced the first time, repeat the above steps a couple of times to reveal the issue.

I attached a GIF to demo the issue: Switch is 'on' at level 83. Switch set to 'off'. Open switch, set level 26. Results in switch set to 'on', and level jumps to 83.

This behaviour is tested with:

Domoticz: 2020.2 Homebridge: 1.1.2 Edomoticz: 2.1.37

RPReplay_Final1598271801

superjunky commented 3 years ago

While I think of it, Chances are it's caused by the way Domoticz returns it's OUT-MQTT messages. The order in which the callbacks are received by Edomoticz can result in this behaviour. (The order may differ on busy moments).

Perhaps Edomoticz should filter out the callback message upon "switching on by setLevel"?

sander1988 commented 3 years ago

I was able to reproduce the issue. This is what causes the issue...

  1. Homekit sends the dim level.
  2. Homekit sends a power on command.

Both of these commands are send to Domoticz through this plugin. But the "set level" API call of Domoticz will also trigger a power on inside the Domoticz API. Now get's bit random (due to timing) which of the two power on commands if processed through MQTT. Sometimes it's the the one triggered by Domoticz and sometimes the on trigger by HomeKit. If the power on command from Homekit is first then Domoticz uses the old dim level in it's MQTT message as the "set level" API call isn't yet processed. I think loop protection prevents the final "set level" command from being send back to HomeBridge (through MQTT) which results in this issue.

I applied a small fix to just ignore the power on command (for 500ms) from Homekit when the device was just turned on by a dimmer command: https://github.com/sander1988/homebridge-edomoticz/commit/4ee69361df1b7361555f91cefe10c2ac38bf913e

Can you test it? It now works fine in my setup.