HA not talking with Xiaomi Gateway

Hey guys! I figured to post this problem to the forum as well in case someone else is experiencing problems

Home Assistant release with the issue:
0.88.0

Last working Home Assistant release (if known):
0.87.1

Operating environment (Hass.io/Docker/Windows/etc.):
Hassio running on a VM:
arch x86_64
dev false
docker true
hassio true
os_name Linux
python_version 3.7.2
timezone Europe/Madrid
version 0.88.0
virtualenv false

Component/platform:

Description of problem:
Xiaomi Gateway (v2) is working correctly as I can see all the devices responding well from the Mi Home app. The problem is when Home Assistant tries to connect with the gateway. Some of the devices will work (like the Aqara wall switches) but some other won’t like contact sensors or motion sensors.

For example, when I try to turn on a light through the Aqara wall switch on HA it works just fine. If I try to turn on/off the Xiaomi gateway light I get an invalid key error
Thu Feb 21 2019 09:04:15 GMT+0100 (Central European Standard Time) Got error element in data {"error":"Invalid key"}

If I move in front of a motion sensor or open a window it won’t report the state on HA (but it will on Mi Home app)

I’ve tried the following things:

  • Created another key through the Mi Home app for the gateway
  • Power cicle the gateway and the entire VM running Hassio
  • Tried different configurations on configuration.yaml

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):
Key was changed from original but still shows the format I’m currently using. I’ve also tried adding the mac and some other preferences like interface, etc with no luck.

xiaomi_aqara:
  gateways:
    - mac:
      key: CACAE1C33CDC41281

Traceback (if applicable):
I get an Invalid Key when turning on the light using the Xiaomi Aqara Wall Switch, but the light actually turns on and off perfectly

2019-02-21 13:54:07 DEBUG (SyncWorker_12) [homeassistant.components.xiaomi_aqara] Expecting 1 gateways
2019-02-21 13:54:07 INFO (SyncWorker_12) [homeassistant.components.xiaomi_aqara] Discovering Xiaomi Gateways (Try 1)
2019-02-21 13:54:12 DEBUG (SyncWorker_12) [homeassistant.components.xiaomi_aqara] Gateways discovered. Listening for broadcasts
2019-02-21 13:54:57 ERROR (SyncWorker_4) [xiaomi_gateway] Got error element in data {"error":"Invalid key"}

Additional information:
As I said, I had this gateway working for years now. Today it just stop working so I’m trying to find out what’s going on.

Edit to add a video: when I turn on the Aqara Wall Switches lights will turn on even if I get a invalid key error. Also it seems some other devices are working like temperature sensors. However all the contact sensors seems to not be reporting anything. See video here https://d.pr/v/ckqd7j

Does rolling back to 0.87.1 fix issue ?

Good question, I’ve tried but no… still not working on 0.87.1. Something else is happening. Thanks!

I am not home but checking it remotely and it seems to work fine…I see statusupdate for motion sensor and window/door sensors. Also turning on and off gateway light seems to work as there is no error.

Has the Xioami gateway updated ? . Had issues with 1.4.1_164.0158. Do not update to this version see

I got that version indeed, but it’s been up and working (can’t remember exactly when I did the upgrade)

I’m also checking that I have the port 9898 UDP open

**jonatan: sudo nmap -PN -sU -p 9898 10.0.1.11**

Starting Nmap 7.70 ( https://nmap.org ) at 2019-02-21 14:53 CET

Nmap scan report for lumi-gateway-v3_miio54968337.home (10.0.1.11)

Host is up (0.025s latency).

PORT STATE SERVICE

9898/udp open monkeycom

MAC Address: 28:6C:00:00:C0:A2 (Xiaomi Electronics,co.)

Nmap done: 1 IP address (1 host up) scanned in 0.10 seconds

There’s a new firmware update for the gateway btw!

The thing is that I kind of control some of the devices like the wall switches, so it’s actually connecting to the gateway

There’s a new firmware update for the gateway btw!

Mi Home app shows new version indeed. Did You update it?

Nope, not sure what’s the changelog :frowning:

BAH I’m going to update now

And what about new in 0.88 eventListener? Does opening a window for example produses any kind of event?

umm not entirely sure how to check that but will try to find out

No, all contact sensors are dead. But again using the Wall Switches through HA works and I get the event published

Event 10 fired 15:16:
{
    "event_type": "state_changed",
    "data": {
        "entity_id": "switch.wall_switch_left_158d00014a9626",
        "old_state": {
            "entity_id": "switch.wall_switch_left_158d00014a9626",
            "state": "off",
            "attributes": {
                "battery_level": 100,
                "friendly_name": "Luz Oficina",
                "icon": "mdi:toggle-switch",
                "homebridge_switch_type": "light"
            },
            "last_changed": "2019-02-21T14:11:57.531203+00:00",
            "last_updated": "2019-02-21T14:11:57.531203+00:00",
            "context": {
                "id": "XXXXXX",
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "switch.wall_switch_left_158d00014a9626",
            "state": "on",
            "attributes": {
                "battery_level": 100,
                "friendly_name": "Luz Oficina",
                "icon": "mdi:toggle-switch",
                "homebridge_switch_type": "light"
            },
            "last_changed": "2019-02-21T14:16:35.082498+00:00",
            "last_updated": "2019-02-21T14:16:35.082498+00:00",
            "context": {
                "id": "XXXX",
                "user_id": "XXXX"
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2019-02-21T14:16:35.082521+00:00",
    "context": {
        "id": "XXX",
        "user_id": "XXX"
    }
}

Updated to 1.4.1_167.0158, still the same

port 9898 still close?

My port is open

Hopefully this supports tradfri bulbs in Xiaomi gateway :slight_smile:

Edit Tradfri works in Mihome app but not in HA :disappointed_relieved:

see Support new Ikea Tradfri · Issue #142 · Danielhiversen/PyXiaomiGateway · GitHub

Okay this is crazy. This thing wasn’t working, I went to walk the dogs and when I’m back and check the log EVERYTHING is working back again. WTF!

2019-02-21 16:20:02 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Wall Switch Left_158d00013098c7: off>: {'voltage': 3300, 'channel_0': 'off', 'channel_1': 'off'}
2019-02-21 16:20:10 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Illumination_286c07f0c0a2: 992>: {'rgb': 1694498815, 'illumination': 1292}
2019-02-21 16:20:10 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Gateway Light_286c07f0c0a2: on>: {'rgb': 1694498815, 'illumination': 1292}
2019-02-21 16:20:33 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Motion Sensor_158d000130ec5f: off>: {'status': 'motion'}
2019-02-21 16:21:34 DEBUG (MainThread) [homeassistant.components.xiaomi_aqara] PUSH >> <Entity Motion Sensor_158d000130ec5f: on>: {'status': 'motion'}

Today is failing again, same invalid key error. Something is going on probably :thinking:

network issues

Has anything changed with your wireless network ?

Umm not really! Now it’s working again… very weird :man_shrugging:t3: