dale3h / hue-mqtt-bridge

MQTT bridge for Philips Hue Dimmer and Hue Tap
MIT License
51 stars 6 forks source link

Publish only if sensor.state changed (fixes #12) #15

Closed ristomatti closed 6 years ago

ristomatti commented 6 years ago

Currently the switch/sensor state is published if any property of the sensor has changed (eg. sensor.config.reachable or sensor.config.battery).

With these changes publish should only occur only if the sensor.state property has changed.

This fixes #12.

anarro commented 6 years ago

or publish only some type of sensors?, ex. a Motion Sensor, Switch, etc..

ristomatti commented 6 years ago

@anarro that would not solve the issue.This issue is about switch button presses being (to my understanding) incorrectly published when the button state has not actually changed.

While trying to figure out what causes the random switches of lights I set up debug printing to show me what the Hue Hub returned as the switch state before and after hue-mqtt-bridge publishes a button press (when no button is pressed).

Initial switch state:

{ buttonevent: 2002, lastupdated: '2018-02-09T16:06:53' },
  config: { on: true, battery: 100, reachable: true, pending: [] },
  name: 'Hue dimmer switch',
  type: 'ZLLSwitch',
  modelid: 'RWL021',
  manufacturername: 'Philips',
  swversion: '5.45.1.17846',
  uniqueid: '00:17:88:01:02:1b:5b:af-02-fc00' }

Switch state change that triggered a publish:

{ state: { buttonevent: 2002, lastupdated: '2018-02-09T16:06:53' },
  config: { on: true, battery: 100, reachable: false, pending: [] },
  name: 'Hue dimmer switch',
  type: 'ZLLSwitch',
  modelid: 'RWL021',
  manufacturername: 'Philips',
  swversion: '5.45.1.17846',
  uniqueid: '00:17:88:01:02:1b:5b:af-02-fc00' } 

hue-mqtt-bridge periodically polls the Hue Hub to see if anything on the above response has changed. This works ok as long as the only changing part is this:

state: { buttonevent: 2002, lastupdated: '2018-02-09T16:06:53' }

...but if you look at the responses above, you'll see the changing part is this:

config: { on: true, battery: 100, reachable: true, pending: [] }
...
config: { on: true, battery: 100, reachable: false, pending: [] },

In this case the change on the reachability of the switch triggered a publish of buttonevent 2002.

My proposed change would change this so that publish is only sent if something in the state part of the response changes (buttonevent or lastupdated in this case).

dale3h commented 6 years ago

Closing in favor of #17 (less code).

ristomatti commented 6 years ago

@dale3h fair enough. Tried to add a bit of self documenting to the code as size is not so much of an issue with Node stuff as it is for web. It took some time to understand what's going on in the code :) Main thing it got fixed and I can stop using the git version and have it auto update in the future .