PyXiaomiGateway Problem: {"error":"Invalid key"}

Any more details?

I just set up my gateway this week and am having some similar issues. I will be able to use the light for a short time before it becomes unresponsive and I get the Invalid key error. Sometimes, when I try to turn on the light the switch will turn on, then turn back off. When this happens, the light will sometimes turn on by the 3rd or 4th time, and there are no errors in the log when this happens. The problems are usually fixed by restarting hassio, but today I had to restart my Pi. It doesn’t work for too long before it happens again. I’ve got a RPi 2 running hassio 0.55.0. My gateway has firmware 1.4.1_150.0143.

Don’t recall where I read this, but it requires that your router supports multicast.

1 Like

hello. i spent 2 days with “invalid key”. trying different
versions HA, python. on one installations HA (windows x32) - all good, on other(windows x64) - “invalid key”. and found a solution for me. on second HA also installed Docker. and in system present network adapter from Docker. if i disable this adapters, leaving hardware netcard - all work very GOOD. if enable -" “invalid key”. in configuration.yaml i write and host , and mac , and key for xiaomi_aqara gw. with 2 or >2 adapters NOT WORK for me.
good luck.

1 Like

I am also having invalid key issue as described here: New Xiaomi gateway firmware (xiaomi_aqara component) - #30 by uiguy

Could you possibly describe your docker solution in more detail?

Additionally following my post, i am now receiving this error on my primary docker HA instance:

2017-10-24 00:12:23 ERROR (SyncWorker_9) [PyXiaomiGateway] Cannot connect to Gateway
2017-10-24 00:12:23 ERROR (SyncWorker_9) [PyXiaomiGateway] No data in response from hub None

Getting this in my RPI3 running AIO HA when trying to turn off the light on the gateway

2017-10-24 00:14:05 ERROR (Thread-9) [PyXiaomiGateway] Cannot connect to Gateway
2017-10-24 00:14:05 ERROR (Thread-9) [PyXiaomiGateway] No data in response from hub None

Thanks

Try to add --net=host

I’ve tried binding to a specific already. No change ;-(

How did you install Home Assistant? Are you able to install “ngrep” on the same host? I would like to inspect your network traffic to get a clue what’s happening in your case.

I have this already…

Do you know how to open a shell on your docker container and install/execute “ngrep port 9898”? If not, I will explain it.

I will try that later at home. I think that the issue is between the gateway and infrastructure and not home assistant. I tried to turn off the light on the gateway with the Xiaomi app and this also failed.

The other sensors seem to be working now, so i am less concerned as the gateway light is a “nice to have” in my setup.

Will test tonight and let you know.

Thanks for your help

You are mixing things up. The communication between the MiHome app and your gateway is TCP (more reliable). The local network protocol / the communication between the Gateway and Home Assistant is UDP (suffers on packet loss). If your network isn’t reliable the gateway cannot work properly.

Next thing: For sensor measurements (wireless button, temperature, motion, window/door sensor) there is no security token and key necessary. The information is just published and (sometimes?) received by Home Assistant. To control a device/actor (gateway light, xiaomi zigbee plug) a proper token is needed.

Please also read this explanation of the local network protocol: New Xiaomi gateway firmware (xiaomi_aqara component) - #34 by syssi

And please stick to this topic now and don’t cross post.

I moved the explanation into this thread for completeness:

I will try to explain the local network protocol of the gateway a bit: The communication between Gateway and Home Assistant uses UDP. A protocol without handshaking dialogues and error correction if a packet gets lost. At the discovery of the Gateway a command requests the first token. All further needed tokens are published via a heartbeat message (multicast (udp), port 9898). The latest published token is always needed to execute a command on the gateway. If Home Assistant just receives the first token and none of the heartbeats with new tokens you will receive the “Invalid key” error message and the control of devices will fail.

There are two possibilities now: You network / wireless connection is very bad. There is packet loss and Home Assistant doesn’t receive all needed heartbeat messages. Second possibility: Your network doesn’t transport multicast messages properly and your Home Assistant instance just receives the first token (received directly by UDP (no multicast)) and this token never gets updated because of the missing multicast traffic.

1 Like

thanks,

Happy not to cross post if you can tell me which post this problem should sit in? As far as I am concerned this is a Xiaomi gateway integration issue, that could also be related to firmware… but now looks like this specific issue has it’s own thread…

I am just trying to get this working and joining (already created) other posts where I think issues may be related so that I can get updates from all the threads in order to try and resolve…

Otherwise, thanks for your help, :+1: :slight_smile:

Seems the issue is with the IOS Mi Home App, it gives the incorrect key, with the android App it gives the correct key. Tooks me weeks to figure this out.

noooo…that can’t be…are you sure? Do I have to gat an android phone to double check!!!

I don’t believe this to be the case. I have enabled the gateway and picking up sensors from the gateway and even got an automation with the gateway light working when I first connected… Anyway, @jakob.tewes if it is the case, you don’t need a phone, you can use bluestacks or andy android emulator on pc/mac…

Yeah I have nox player running on my mac, I actually gave my friend an extra gateway, a motion sensor to mess around with hoping he would figure it out as I got so fed up. Hours late he messaged me It’s a bug in the IOS app. Generate the code on a Android and it worked. I’m able interact with my gateway lights/sound and wall switches :smile:

Only regret i have is, I wish I had read all the forums sooner. Xiaomi Gateway Integration

2 Likes

100% certain, PyXiaomiGateway Problem: {"error":"Invalid key"}

ok brilliant!!! awesome work… will give it a try… here is a virtual beer :beer: