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

I remember you had a working setup in the past. Did you change something?

Yes @syssi that’s correct.

I had a working (Xiaomi) environment with 0.47. Finally I was able to update to 0.57 (was busy with work before) and this broke my Xiaomi installation.

I then updated to Python 3.6 and created a new vEnv and after that at least all my sensors showed up again and my (one) button is working in an automation. But I can’t turn on the gateway light nor can I play a sound :frowning:

Any errors in the log?

Hi @Danielhiversen,

I just tried to turn on the Gateway light via the web GUI and I can see the following:

2017-11-22 11:57:45 DEBUG (SyncWorker_2) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:58:16 DEBUG (SyncWorker_19) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:58:47 DEBUG (SyncWorker_10) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:58:47 DEBUG (SyncWorker_0) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:59:18 DEBUG (SyncWorker_3) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:59:18 DEBUG (SyncWorker_1) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:59:49 DEBUG (SyncWorker_5) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 11:59:49 DEBUG (SyncWorker_17) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 12:00:20 DEBUG (SyncWorker_18) [homeassistant.components.binary_sensor.xiaomi_aqara] Updating xiaomi sensor by polling
2017-11-22 12:00:44 ERROR (SyncWorker_19) [PyXiaomiGateway] Got error element in data {“error”:“Invalid key”}

So the key in the config is wrong or your router does not support multicasting.

It’s just strange that I only updated to 0.57.3 (and Python).

Same gateway like before and same router setup. And it was working like a charm before :frowning:

Key and Mac and not case sensitive and I don’t need the : for the mac, right?

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?