michbeck100 / pimatic-hap

Pimatic homekit bridge
GNU General Public License v2.0
30 stars 10 forks source link

Flickering lights/lights turning on on startup #43

Closed thexperiments closed 7 years ago

thexperiments commented 7 years ago

Hi, for some time now my hues were turned on when I restarted pimatic. I now found out that this doesn't happen when I disable the hap plugin. Today it even startet toggeling my Kitchen light like crazy when I restarted.

17:57:08.647 [pimatic] info: New device "Stehlampe Schlafzimmer"...
17:57:08.658 [pimatic-hap] debug: successfully added device Stehlampe Schlafzimmer

Turning on the hue in my bedroom

17:57:24.045 [pimatic-hap] debug: switching device Stehlampe Schlafzimmer to false
17:57:24.059 [pimatic-hap] debug: changing dimlevel of Stehlampe Schlafzimmer to 100

and the others also (It is setting them to false but I think changing the dimlevel causes them to turn on)

17:57:24.045 [pimatic-hap] debug: switching device Stehlampe Schlafzimmer to false
17:57:24.059 [pimatic-hap] debug: changing dimlevel of Stehlampe Schlafzimmer to 100
17:57:24.073 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 1 to false
17:57:24.076 [pimatic-hap] debug: changing dimlevel of Indirekt Wohnzimmer 1 to 100
17:57:24.083 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
17:57:24.085 [pimatic-hap] debug: changing dimlevel of Indirekt Wohnzimmer 2 to 100
17:57:24.093 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 4 to false
17:57:24.097 [pimatic-hap] debug: changing dimlevel of Indirekt Wohnzimmer 4 to 0
...
17:58:02.451 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.502 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.541 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.584 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.619 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.658 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.701 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.741 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.782 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.829 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.871 [pimatic-hap] debug: switching device Licht Küche to false
17:58:02.916 [pimatic-hap] debug: switching device Licht Küche to true
17:58:02.961 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.001 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.041 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.080 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.130 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.168 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.214 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.251 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.290 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.331 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.369 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.412 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.456 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.494 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.534 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.571 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.614 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.661 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.708 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.752 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.788 [pimatic-hap] debug: switching device Licht Küche to false
17:58:03.832 [pimatic-hap] debug: switching device Licht Küche to true
17:58:03.883 [pimatic-hap] debug: switching device Licht Küche to false
...

Why is it changing something in the startup state? I would guess the first thing the plugin should do after startup is to communicate the current states to homekit.

michbeck100 commented 7 years ago

It doesn't and never did. It just reacts to events sent from either HomeKit or pimatic. Which plugin do you use to switch and dim your lights? Please don't say pimatic-hap, I mean the plugin which adds the devices to pimatic ;-).

michbeck100 commented 7 years ago

Oh, and about flickering: I'm currently investigating this, it happened to me, most times when I switched my homeduino lights using the original remote really fast from on to off. I think it's a racing condition here.

rspaargaren commented 7 years ago

This is related to my issue posted earlier. I also have this behaviour with some of my homeduino switches. Onnis activated by a contact switch and sometimes start oscillating on off on off etc reboot is the only way. Also here I think to fast switching of homeduino and hap is in counter phase

thexperiments commented 7 years ago

Dimming lights is happening via hue-zll plugin and there are rules which react on "recieves on" from the pimatic-homeduino but they don't explicitly change the dimlevel. I however have some buttons devices which change the dimlevel via rules.

For the Kitchen light which flickered I use pimatic-zway and this light is only triggered by a pimatic-mysensors PIR motion sensor.

I had some flickering with my entrance lights some time ago which are using my orvibo-plugin and a pimatic-mysensors PIR motion sensor.

So my educated guess is: Flickering --> Maybe some general problem with how rules are evaluated or plugins are feeding back the device state. Turning hues on when starting pimatic --> Issue with pimatic HAP as this doesn't happen anymore as soon as I disable the pimatic-hap plugin.

michbeck100 commented 7 years ago

Flickering is also discussed in #39. -> i have a feeling what the problem is and I'm on it Turning on the hues when starting is pretty complicated when just looking at the code of the pimatic-hue-zll plugin. This might be related to https://github.com/markbergsma/pimatic-hue-zll/blob/master/hue-zll.coffee#L266, but i can't verify this

rspaargaren commented 7 years ago

I have seen same issue related to homeduino mentioned here: https://github.com/dgmltn/android-pimatic-app/issues/35 (no replies to it)

thexperiments commented 7 years ago

@michbeck100 I think I can now confirm that the flickering is a general problem had it today with pimatic-mysensors switch when "identifying" the device via homekit which initiates a quick toggle of the switch.

michbeck100 commented 7 years ago

Yes, I will probably remove the identification code for switches and lights. Was just a gimmick anyway.

thexperiments commented 7 years ago

I think in general it is very good for usability but it should only be active for lights and the interval should be a bit longer. For example it toggled my door opener and some sockets for appliances when going through homekit setup.

michbeck100 commented 7 years ago

Since iOS 10 the devices are called to identify during the homekit setup. This wasn't the case before iOS 10. Now it's pretty annoying.

michbeck100 commented 7 years ago

Please checkout https://github.com/michbeck100/pimatic-hap/tree/request_queue and test if the issue is resolved for you.

michbeck100 commented 7 years ago

FYI: I left the identification code as it was. I think a switch also should be able to identify itself by switching.

michbeck100 commented 7 years ago

did you test the branch in https://github.com/michbeck100/pimatic-hap/tree/request_queue? the flickering should be fixed with the changes.

thexperiments commented 7 years ago

doesn't help :( just restarted with it and had to kill pimatic as one of my hues would not stop flickering

maybe a patch for the huezll plugin is needed?

19:28:18.681 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:18.716 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:18.749 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:18.783 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:18.815 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:18.846 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:18.878 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:18.909 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:18.940 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:18.972 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.003 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.036 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.102 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.144 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.190 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.232 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.274 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.309 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.344 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.411 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.445 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.478 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.512 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.546 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.583 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.618 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.654 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.695 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.732 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.768 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
19:28:19.807 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to true
19:28:19.844 [pimatic-hap] debug: switching device Indirekt Wohnzimmer 2 to false
thexperiments commented 7 years ago

48

fixes the problems for me not noticed side effects yet.

I found out that at the moment the plugin tries to determine if it should switch the state @_state was null and it was only written in the constructor at this point in time. I think it is legit to get the device state on every new set which comes in from homekit and use this for evaluation. In addition to that I think there can be the case that a device still was not yet able to update its internal state if a "set" comes from homekit. We should not switch a device in this state and thus I added a null check.

michbeck100 commented 7 years ago

I don't see why your change helps. I would say it must be of no interest if the state of the device is still null. What if you change the state from pimatic itself. Then it works, too. Additionally, because not all plugins set the state of the device correct, pimatic-hap saves its own state. So even if a device always has a null state, there should be no infinite loop.

thexperiments commented 7 years ago

I have to admit I need to debug it again. What you are saying sounds correct but doing the changes I did was totally obvious when I was doing them. Sadly I will not be home for some time now.

Maybe the changes I made maybe only mask the real problem.

When I was debugging the function for switching the hues was called with ON before the hues were even initialized. They were clearly off before restarting pimatic and also displayed like that in homekit.

It then looked like an endless loop...

Maybe I get the chance to get hold of some hues here the next days and supply more info.

Nevertheless, my fix/workaround has been working great with my system.

michbeck100 commented 7 years ago

Did you have the time to debug some more? I still don't see why your changes make any difference. Are you using hue lamps? And the hue-zll plugin? I will see if there might be an issue with their implementation regarding the order of switching and state saving.

michbeck100 commented 7 years ago

With https://github.com/michbeck100/pimatic-hap/commit/a076a592a492bdd34caa28f07fd520daab7d34a6 i've done some changes, that will hopefully fix the infinite loops. Please test and reopen this issue if it doesn't help. The original problem (that the lights are switched on at startup) might be fixed by https://github.com/markbergsma/pimatic-hue-zll/issues/15