How to connect Xiaomi Aqara and Mijia ZigBee sensors and switches to Home Assistant without hub

The last thing you want to see setting up your smart home with Home Assistant is definitely huge amount of hubs and separate accounts. But we have some good sensors and switches from Xiaomi in other hand that requires Aqara hub and app to set up and work. And there is several ways to connect those devices to Home Assistant without that hub and app. I want to show you one of those possible solutions based on hass.io and Raspberry Pi 3B+.

ZigBee for Raspberry

Aqara devices communicate with hub using ZigBee so first of all we need some extension for our Raspberry Pi to enable ZigBee. I’m using ZigBee shield from Elelabs for this. It is small and looks nice. And it works. That’s all we need. You can use any you want but it is better to stick to the list of already tested and confirmed modules in official HA docs.

YI DIGITAL CAMERA

To make it work on Raspberry Pi 3 or 3 B+ we need to edit config.txt file placed in /boot of hassio. This is easy to do because /boot is a separated FAT32 partition and it will be visible and editable even on Windows PC. So just insert your SD card with HA into your PC or laptop and add this line to the end of cofig.txt:

dtoverlay=pi3-miniuart-bt

Now we can attach ZigBee shield to our server, bring back the SD card and boot Home Assistant. The last thing we need to do without web UI is to find out device address for our new shield. Usually it is /dev/ttyAMA0. But if you don’t want to guess, device address can be found using ssh connection to your Home Assistant. By default ssh is not enabled in hassio. To enable it you need to install SSH server add-on.

Assuming we can connect to our HA via ssh, the next step is to execute command there:

 hassio hardware info

After executing it we will get some output:

audio:
  "0":
    devices:
    - chan_id: "0"
      chan_type: digital audio playback
    - chan_id: "1"
      chan_type: digital audio playback
    name: bcm2835_alsa - bcm2835 ALSA
    type: ALSA
disk: []
gpio:
- gpiochip0
- gpiochip128
input: []
serial:
- /dev/ttyAMA0
- /dev/ttyS0

Here you can find two serial ports listed under serial:. One of them definitely used by ZigBee shield.

Home Assistant configuration

There is an official component for ZigBee integration in Home Assistant. But it doesn’t work with all Xiaomi ZigBee devices I have. And there is a fork of this component made by Yoda-x and it works with all of them.

We will use this component as a custom one. All we need is to download it from repository and copy custom_components directory to your Home Assistant /config folder. To start using it we also need to add it to our configuration.yaml:

zha_new:
  usb_path: /dev/ttyAMA0 #ZigBee shield device address
  database_path: zigbee.db

And restart HA server.

Note that upon first restart dependency libraries will be downloaded. Sometimes for some reason this libraries can be downloaded successfully only after second restart. To ensure that zha_newcomponent loaded we need to check available services in “Developer tools” of Home Assistant web UI (the first icon in “Developer tools”). zha_new.permit and zha_new.remove should be available there.

Connecting a device

New entity zha_new.controller should also be available now in Home Assistant along with new services. It has Run state when running normally. To make new ZigBee devices connection easier and faster we can add zha_new.controller and zha_new.permit service call to Home Assistant UI. For example, in a separate Lovelace card:

entities:
  - type: call-service
    icon: mdi:plus-network
    name: Joining permission
    action_name: Permit
    service: zha_new.permit
    service_data:
  - entity: zha_new.controller
    icon: mdi:zigbee
title: ZigBee
show_header_toggle: false
type: entities

After clicking “PERMIT” we’ll have 60 seconds to connect a device to ZigBee network.

Important note: Device you want to connect should be placed right near your Raspberry Pi. After successfully connected it can be moved wherever you want in a range of ZigBee operating.

To connect Xiaomi Aqara or Mijia sensor we need:

  1. Place the button as close as possible to your Home Assistant server
  2. Permit joining by calling zha_new.permit service or by using our newly created button for it in Lovelace UI
  3. Hold small button on Aqara switch during 3-5 seconds until blue light will flash several times
  4. Release the button and click it every second to prevent device going to “sleep”
  5. In a normal conditions the state of zha_new.controller will be changed to Joined:[device mac address] and then to Device init: [device mac address]
  6. You can stop clicking a button on Aqara switch when zha_new.controller will go back to Runstate.

All Aqara and Mijia sensors can be connected the same way. After success connection new entities should appear. Usually there are several Home Assistant entities created for each sensor.

Smart switch handling

There is several types of smart switches from Xiaomi. For example – Aqara smart button WXKG11LM. It can be connected the same way as sensors, but you don’t need to click a button on device every second in step #4. Just hold reset button for 3-5 seconds, release it after blue light flashes and wait for successful device initialization.

Smart switches will appear in Home Assistant as binary_sensor but you can’t handle clicks using its state. You need to listen events instead. To handle WXKG11LM smart button click, double click or long click, we can create an automation like this:

- id: bedrum_button_handler
  alias: Bedroom button handler
  trigger:
  - event_data:
      entity_id: binary_sensor.lumi_lumiremoteb1acn01_01ef39e8_1
    event_type: click
    platform: event
  action:
  - data_template:
      entity_id: 'light.bedlight'
    service_template: 'light.toggle'

Here in trigger section we are waiting for event with type click. This will be constant for any WXKG11LM switch. To determine one switch from another we are using entity_id in event_data.

Talking about action, we can find out how the button was clicked by checking trigger.event.data.data. You need to experiment with this variable because different Xiaomi buttons and switches supports different click types, but here is a list of all of them:

  • single
  • double
  • hold
  • release
  • long_released
  • shake

That is all for now. Please ask your question in comments section. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *