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

I can’t find any documentation on my router (Netgear Nighthawk R8300) but I would be surprised if it doesn’t support multicasting. Like Daniel, I had everything working previously then at some point it all just stopped working. And I’m sure I have the key right because I am able to see the subdevices connected to the gateway (motion sensors), although updates to their state are not passed along.

Got the same problem here. It cost me some time, but I finally got the Gateway in the HASS front-end (although it fails a LOT when trying to discover the device when starting HASS).

Althought I can’t control it’s light, and the sensors get their value once when starting HASS, and then never update.

But like Gaggle331 above me: My router is dumbed down by my ISP, so I wouldn’t know if multicast is even on or not. Could it be that? Or would HASS not be able to find the gateway at all at that point?

that’s exactly what my story with xiaomi gateway is like.

Okay: I went and bought a new router that I thought would probably support multicast.

And that worked. So if anyone else is having these problems: get yourself a router that supports multicast or enable it in your current router if it is supported.

But: I don’t really understand why the gateway would report “Invalid Key” when multicast isn’t supported in the network. Not really good situation reporting.

1 Like

I created an issue and will try to implement some logging to address multicast issues in future:

https://github.com/Danielhiversen/PyXiaomiGateway/issues/26

Sorry for cross posting. I’m trying to spread some background informations.

try change wifi chanel, or set for 20MHz .
its work for me

source:https://bbs.hassbian.com/thread-804-1-1.html

by they said, cuz unknown reason,sometimes getway’s heartbeat multicast goes wrong.

I have the same issue.
I have three xiaomi gateways, but only one of them doesn’t work well. When I change the light on the gateway in Mijia App, I can see the status change on home assistant.
But if I operator the light by the switch on home assistant, it (one of the three gateways) doesn’t work, and accompanied with the “invalid key” error. The other two gateways work well, can operator lights by the switches on home assistant and no error message.

  • I tried to regenerate a key, didn‘t work.
  • I tried change the channel on router, didn’ work too.
  • I tried to change the sequence of my three gateways in configuration.yaml, still this gateway doesn’t work.

update what i found:
i only have one xiaomi getway,and i have a router and an AP ,set by same ssid and password ,router ip set 192.168.1.254 and AP ip set 192.168.1.253
when my getway connect though router itself,it works well ,and if getway connect though AP ,“invalid key” error happens

I have the same issue, my gateway is on a separate VLAN to my HA instance (0.60 under docker)

Here’s what I have configured:

xiaomi_aqara:
  gateways:
   - mac: 286c078592fd
     host: 192.168.30.5
     key: jy97hrvoszpyxc46

But here’s what tcpdump sees:

19:26:56.883914 IP 192.168.29.3.36336 > 192.168.30.5.monkeycom: UDP, length 111
E.....@.?.............&..w..{"cmd": "write", "sid": "158d000126b7cd", "data": {"status": "off", "key": "05193d32d8ac70c6f1074d4b1e302dc6"}}
19:26:56.890057 IP 192.168.30.5.monkeycom > 172.26.0.4.36336: UDP, length 79
E..kw...............&....W.s{"cmd":"write_ack","sid":"158d000126b7cd","data":"{\"error\":\"Invalid key\"}"}
19:26:58.044051 IP 192.168.29.3.36336 > 192.168.30.5.monkeycom: UDP, length 111
E.....@.?.............&..w..{"cmd": "write", "sid": "286c078592fd", "data": {"rgb": 3036676095, "key": "05193d32d8ac70c6f1074d4b1e302dc6"}}
19:26:58.047531 IP 192.168.30.5.monkeycom > 172.26.0.4.36336: UDP, length 77
E..iw...............&....U..{"cmd":"write_ack","sid":"286c078592fd","data":"{\"error\":\"Invalid key\"}"}

Note how the key sent from HA to the gateway is the same every time, but seems to be ascii-encoded. Is this expected?

I cannot change the color of gateway or play a ringtone, I keep getting “invalid key error” when I try to do that. I can only get the initial status of my lights in the States section of HA but I cannot get any update at all after that. I tried:

  • Getting new password from the android simulator
  • Entering mac with lower case letters
  • Changing the wireless channel to 4

Do we have a solution for this?

Update: I just changed the wifi channel back to Auto and it is started working :slight_smile: I can change the color of the light and play a ring tone

If the captured key doesn’t change over time your network is not multicast capable. A heartbeat updates the key periodically. If home assistant doesn’t receiver the updates a “invalid key error” will pop up.

@syssi do you know how much time the key will be expired?

Unfortunately, no. It can be discovered just by trying. May be there a few valid tokens at a time because UDP has no error correction. Missing a broadcast(=heartbeat=update) would lead to a broken system for a few minutes.

If our network doesn’t support multicasting, what options do we have for getting the xiaomi gateway integrated into HomeAssistant?

I’m experiencing the same issues, the initial data transmission works and all devices linked to the aqara appear in HA, but after that i’m receiving the invalid key error.

When i run a tcpdump on the machine that HA is running on, capturing all packets coming from the aqara, i can see the heartbeat data that announces the token change coming in.

Although the heartbeat is send by the aqara, and visible in a tcpdump, the token is never updated by HA.

HA keeps using the same key value for each command over and over…
It appears multicast is working as expected, as they are visible in a tcpdump, but HA is not picking up it.

Any suggestions?

Could you please dump the traffic by ngrep and post the communication here?

Sure :slight_smile: I hope this helps, otherwise i can capture more if that would help: https://gist.github.com/fliphess/f82ef187bc454e8e6d52e2bda33166a7

Edit: I added some extra ngrep and tcpdump output in the gist

For some reason i got it working by upgrading to the latest development version of homeassistant.
A lot of changes were made in the last few days according to the commit history and apparently something recent fixed it :slight_smile: Thanks guys :slight_smile:

A last question: Where did you capture the network traffic? Is the host “flip” the same host where your home assistant instance is running?

Jep it’s the same machine: It’s a linux machine serving as a router.
the server is named flip but resolves internally to server.home…

But all is well now: I got it working by upgrading to the latest development version and additionally adding the interface to the settings.

After the upgrade I noticed some traffic going out the wrong interface, which i didn’t see before the upgrade… Not sure if it’s related, but i’m very happy i got it working! :slight_smile:

You earlier posts about how it works helped me a lot!