• 13
    Apr - 2019

    Home Automation, Raspberry Pi
    4 min | 3153

    #Raspberry Pi: Xiaomi Sensors & Home Assistant (Hass.io)

    Home Automation, Raspberry Pi | 4 min | 3153


    docker
    embedded
    hass.io
    home assistant
    home automation
    raspberry pi
    sensors
    xiaomi
    zigbee

    Last week, I published an article about using Xiaomi sensors without a Xiaomi Gateway. The gateway/hub can be replaced with a Raspberry Pi and a USB-ZigBee dongle. You find the instructions here: #ZigBee: Xiaomi Sensors using Raspberry Pi (without Gateway!).

    As you can read in that article, two repositories are available to connect and process the data from the Xiaomi sensors. Additionally, Zigbee2mqtt can be connected to Hass.io. Well, I didn't want to use HassOS (the OS on which hass.io runs), but I wanted to use Hass.io running on Raspbian, otherwise with Home Assistant running as an application, the add-ons were not available as in Hass.io (Docker based) and I didn't get the possibility to run the hassio-zigbee2mqtt official container.

    The following command doesn't help:

    docker run -d --name="home-assistant" -v /home/pi/homeassistant:/config -v /etc/localtime:/etc/localtime:ro --net=host homeassistant/raspberrypi3-homeassistant

    because it deploys a container in which the Home Assistant application runs, but the Hass.io functionabilities are not available.

    Then, I looked for the Hass.io installation scripts and I found a way to run Hass.io on Raspbian (if you are a Home Assistant expert and you know the right name for "this type of running" of Home Assistant, please leave me a comment).

    This is what I will try to accomplish in this tutorial:

    1. Install Docker on Raspberry PI
    2. Install/Deploy Hass.io on Raspbian
    3. Install the Mosquito broker and the zigbee2mqtt add-ons
    4. Pair all the Xiaomi devices

    Installing Docker

    First, if you've not install Docker, you need to type and run the following:

    curl -sSL https://get.docker.com | sh
    sudo usermod -aG docker pi

    After executing the usermod line, reboot the Raspberry to get the pi user added to the docker group. Otherwise, you need to always use sudo every time that you use the docker command.

    More information about Docker on embedded systems is available here.

    Home Assistant dockerized (Hass.io on Raspbian)

    The following deploys Home Assistant dockerized. Actually, it creates a service that deploys the hassio-supervisor container, which then pulls and runs the raspberrypi3-homeassistant container.

    sudo apt-get update
    sudo apt-get install -y apparmor-utils apt-transport-https avahi-daemon ca-certificates curl dbus jq network-manager socat software-properties-common
    
    wget "https://raw.githubusercontent.com/home-assistant/hassio-installer/master/hassio_install.sh"
    chmod +x hassio_install.sh
    sudo ./hassio_install.sh -m raspberrypi3

    The first container launched is, as I mentioned, armhf-hassio-supervisor. This pulls and deploys the raspberrypi3-homeassistant container (because of the option -m raspberrypi3).

    You can also deploy Portainer.io to see how the containers run:

    docker volume create portainer_data
    docker run -d -p 9000:9000 --restart unless-stopped -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer

    If you need to update the Hass.io version, just type the following on a terminal/console:

    sudo docker pull homeassistant/raspberrypi3-homeassistant:latest
    systemctl start hassio-supervisor.service
    sudo reboot

    If it does not work, you need to kill the running homeassistant container. To do that, list the running containers using docker ps and look for the CONTAINER ID of the container with IMAGE name "homeassistant/raspberrypi3-homeassistant", then type docker kill <<the container id/hash>>. Then, reboot the system. If you've installed Portainer.io, just select the container and click on the Kill button.

    Installing Zigbee2mqtt

    After running the hassio_install.sh file, you can access Home Assistant pointing to http://<ip-address>:8123. Home Assistant will ask you to set up a username and password as usual.

    Then, follow these steps to pair the Xiaomi sensors:

    1. Go to hass.io and add the https://github.com/danielwelch/hassio-zigbee2mqtt as repository (see Fig. 1).
    2. Look for the Mosquitto broker add-on and install it.
    3. Set the configuration to the following (I added login credentials to the MQTT broker. Alternative, you can set anonymous to true) and start the add-on:
      {
      "logins": [
      {
        "username": "yourusername",
        "password": "yourpassword"
      }
      ],
      "anonymous": false,
      "customize": {
      "active": false,
      "folder": "mosquitto"
      },
      "certfile": "fullchain.pem",
      "keyfile": "privkey.pem"
      }
    4. Then, look for the zigbee2mqtt add-on and install it (see Fig. 2).
    5. Set the configuration (add the login credentials - more options are available here) to the following and start the add-on:
      {
      "data_path": "/share/zigbee2mqtt",
      "homeassistant": true,
      "permit_join": true,
      "mqtt_base_topic": "zigbee2mqtt",
      "mqtt_server": "mqtt://homeassistant",
      "serial_port": "/dev/ttyACM0",
      "devices": [],
      "network_key": [],
      "mqtt_user": "yourusername",
      "mqtt_pass": "yourpassword"
      }

      You need to have the USB-ZigBee dongle connected before you start the add-on. The permit_join option should be set to true to pair the ZigBee sensors. Once you joined all devices, set permit_join to false for security reasons. Every time you change a setting, you need to restart the add-on.

    6. Go to Configuration->Integrations, and configure the MQTT add-on selecting the Enable discovery option (see Fig. 3).
    7. Press the link/reset button on the Xiaomi sensors to pair and discover them.
    8. Once a device is discovered, you can change the device_id with a friendly name using the web interface: Go to Configuration->Entity Registry to do that (see Fig. 4).
    9. The sensors should be available on the Overview page as in Fig. 5.
    10. Once you joined all devices, do not forget to set the permit_join variable to false! and restart the add-on.

    If you want to stop everything, you need to type the following:

    sudo systemctl stop hassio-supervisor.service

    This stops the armhf-hassio-supervisor container and then you can stop all the other running containers. Otherwise, the containers will start automatically after you stop them.

    To start the service again, type the following:

    sudo systemctl start hassio-supervisor.service
    hass_io-min.JPG
    Fig. 1: Hass.io running on Raspbian


    zigbee2mqtt-min.JPG
    Fig. 2: zigbee2mqtt: installation dialog


    mqtt_configuration-min.JPG
    Fig. 3: MQTT configuration: Enable discovery mode option


    rename_sensor-min.JPG
    Fig. 4: Entity Registry: Changing the Entity ID with a friendly name


    main_door_status-min.JPG
    Fig. 5: Overview page: Checking sensor status from the web interface

    Additional information and credits

    Conclusions

    This is an extension to the #ZigBee: Xiaomi Sensors using Raspberry Pi (without Gateway!) article. In this article, I integrate the Xiaomi sensors to Home Assistant without any Xiaomi gateway, cloud or smartphone application. There is also no need to install HassOS, and it was possible to run Hass.io on Raspbian. If you are curious and you want to see how many containers are running, check Fig. 6!

    running_containers-min.JPG
    Fig. 6: Portainer: Overview of the running Docker containers


    Comments

    Empty