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

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:

OK Awesome, loaded up bluestack and redid the key and this is now working! You the man!

Do you confirm? A proper “key” retrieved by the android mihome app solved the issue for you?

well, in terms of changes to the environment, this was the only change that i carried out and now the {“error”:“Invalid key”} message no longer appears in the logs. Additionally, I am able to control the light on the gateway, so unless there were some other changes on the infratructure side that I was not aware of, I can confirm that this fix has resolved my issue.

same for me. that costed lots of time. we should definitely make sure, that this makes it to the module docs.

Doubleho7 you made my day!!! works like a charm now :slight_smile:

I have already updated the docs…

1 Like