Home Automation with “Awesome” HASS.IO — Part II

In the previous post, we have seen how HASS can be configured to toggle outdoor lights based on sunrise and sunset. In this post, we will continue the journey and see how Xiaomi BLE Temperature and Humidity sensors can be integrated to the same system.

Xiaomi BLE (Bluetooth Low Energy) sensors are cheap with an advertised battery life of 1 year. Besides providing temperature and relative humidity values over BLE, the device sports a tiny display that shows the current temperature and relative humidity values (another reason why I prefer this device). Here is a map showing the sensor locations on Top Floor and Basement:

In case you are wondering about the “Balcony” box, we discussed that in an earlier post (I just borrowed the same picture here!). HASS comes with in built support for Xiaomi’s BLE sensors (also known as mitemp_bt integration). BLE was incorporated into Bluetooth version 4.0. This means if the HASS host machine has a Bluetooth adapter supporting version 4.0, rest assured it also supports BLE. In order to get this working, find the MAC address of the BLE sensor and configure this MAC address in HASS. Detailed instructions can be found here. For example in my configuration YAML file (/config/configuration.yaml), it looks like this:

Restart HASS to discover new device based on the updated configuration. Once HASS is online, check for logs: HASS --> Developer Tools --> Logs where it will show some information about the interaction between mitemp_bt module of HASS and the Xiaomi sensor, which can now be configured as “Cards” on the HASS Dashboard. This way we can observe both the historical values and instantaneous values as seen below:

Entity Cards in HASS

BLE operating range for this Xiaomi sensor is quite limited (5–7 meters). This further degrades with building material between the sensor and the HASS server. For the Basement sensor, this was not an issue, because the HASS server was placed in close proximity. This ensured that the Bluetooth adapter on the HASS server itself was capable of detecting and handling the temperature and humidity values from the sensor in the Basement as seen below:

However this solution does not cover the sensor on the “Top Floor” since the sensor is quite distant from the HASS server:

Sensor Placement from HASS

We now have to find a way to collect data from sensor on the Top Floor and pipe it down to the HASS server sitting in Basement. For this, we employ another technology called MQTT. It follows a PUB-SUB pattern where one participating entity subscribes for a “topic” and another participating entity publishes information on that same “topic”. Connecting these entities is a “broker” who is responsible to track which entities publish and which entities subscribe for the specific topics. It may be easier to understand this if we assign appropriate roles to our devices.

The sensor in the Top Floor is a client who publishes information on a topic, e.g., “Sensor Values”. HASS server in Basement is another client who subscribes for the same topic. Next we need to identify a broker who can manage the subscriptions. Since the sensor can speak only BLE and the HASS can be reached easily over WiFi, the MQTT Broker should ideally be a device that acts a BLE-WiFi Gateway. This broker should receive BLE packets from the sensor and publish them to HASS over WiFi. Any laptop with a Bluetooth adapter and a WiFi adapter can serve this purpose. In my case I chose to use a RaspberryPi 3 B+:

Device Map with MQTT Broker

RaspberryPi 3 B+ has all the necessary hardware for our purpose and for the software we need to install two components.

  1. An MQTT broker such as “Mosquitto”. Installation instructions can be found here.
  2. Python script that can talk BLE to the sensor to get the temperature and humidity values. And publish these values as an MQTT topic to the MQTT broker. There is one such script that does exactly this and is available freely on github.

In step-1, do note that if “Mosquitto” is installed with default options, it starts using port 1883. This is needed when configuring the broker component of the python script in step-2.

For step-2, github pages provide all necessary instructions on configuring the broker and the BLE client. This is how the .ini files look in my configuration:

Next step is to configure the MQTT client end of the HASS so it can receive payload (containing temperature and humidity values) from the RaspberryPi. Edit the configuration YAML file on HASS (/config/configuration.yaml)and update the following:

The tags are self-explanatory. Do note that the expression in value_template retrieves temperature and humidity values from the MQTT message payload and displays them on the dashboard. Save the configuration and restart HASS. Once the dashboard is up, Top Floor (or First Floor) sensor shows up similar to the Basement sensor:

Sensor display in HASS

That’s it! Now we have the Xiaomi BLE Temperature and Humidity sensors configured into HASS. In the next post, we will extend this further to integrate Google Assistant into HASS, so we can ask Google for the temperature and humidity readings from the Xiaomi sensors.

Technology Enthusiast!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store