Temperature monitoring

The first temperature measurements which I fed into Home Assistant used simple sensors like the DHT11/22 or AM2320. They were connected to a raspberry pi or a sonoff switch. Whilst these work very well, they do have drawbacks when implementing around a home. Firstly, they need a power source which usually takes up a mains socket. Secondly, they look ugly (even if inside a box). I wanted temperature and humidity sensors in each room of my house, but did not ruin the look. They had to work on batteries because correct placement in a room is essential for temperature sensors and I shouldn’t need to change batteries every few weeks. Oh, and I don’t want to spend a fortune as usual. After a few hours google searching I came across a sensor from Xiaomi named (LYWSDCGQ).

Xiaomi LYWSDCGQ

These sensors show the temperature and humidity and battery level on a clear display. They transmit the data via bluetooth. The batteries are standard batteries and last about a year (not yet proven, but looks like they will indeed). They also have a magnetic backplate to fix to a wall, so are easy to move around when you need to. You can get them on Amazon UK, Ebay UK or Aliexpress for around £12 each. If you buy as a bunde of 4, on AliExpress you can get the price down to about £8 each.

Bluetooth receiver

So having picked the approriate sensors, it’s clear we need a bluetooth receiver to receive the data and repackage for Home Assistant. These Xiaomi sensors are designed to work with a Xiaomi hub device, but I didn’t choose that path. Instead, I used an ESP32 system on a chip (SOC) to receive the bluetooth data and then send it via WiFi to Home Assistant. This isn’t as hard as you might think since some very clever people have already written most of the code. I used a TTGO T7 V1.3 MINI32. You can pick these up for £5 in Aliexpress. All you will need to power this SOC is a spare USB charger from an old gadget and a USB to micro usb cable. Chances are that you have both of these already. The charger can be less than 1 amp rating as the ESP32 SOC uses very little power.

TTGO T7 V1.3 MINI32 ESP32

ESP Home software

ESP Home is a great software stack for powering ESP32 devices. You upload the software to the ESP32 once, and after that you can manage the devices via over the air (OTA) updates. It now integrates really great with Home Assistant and allows you to use many many sensors including the temperature sensor above (Xiaomi LYWSDCGQ).

Installing ESP home

By far the easiest way to install esphome is to use the Home Assistant add on store and search for ESPHome. I’m assuming you are already familiar with this process.

The Home Assistant Add on store

Once you have installed ESPhome and possibly rebooted your Home Assistant server, you should have an ESPHome item in your Home Assistant menu. And if you click that you will see something similar to below.

The ESPHome set up screen

To set up the ESP32 SOC, we simply click the big pink + button on the right and answer some basic questions. Here is what to enter:

  • Name of Node : bluetooth_scanner
  • Device Type: {for the ESP32 SOC above choose ‘wemos_d1_mini32’}
  • WiFi SSID : {your network SSID}
  • WiFi password: {your SSID password}
  • Access password: {can put a password or leave blank}

That’s it. We now need to upload the ESPHome software to the ESP32 SOC. If your Home Assistant is running on a Raspberry Pi, the easiest thing to do connect your ESP32 SOC to a USB port on the raspberry Pi and then reboot the raspberry pi. Once rebooted, go back to the ESPHome page in Home Assistant and in the top right corner choose the port for the ESP32 SOC which should now be available in the drop down list. It will say something like “/dev/ttyACM0 (ttyACM0)”. Now click upload in the white box you just created and called ‘ bluetooth_scanner ‘. If all goes well, ESPHome will write its software stack to the ESP32 SOC. The next time you upload software to this ESP32 SOC you will be able to choose to upload either by USB port or over the Wifi Link. Disconnect the ESP32 SOC from the raspberry pi and power it up with the USB charger. It should now appear as a device on your Wifi network.

ESP HOME config

So we have put everything in place now to run our bluetooth scanner, but we have yet to tell it how to receive the data from the Xiaomi temperature sensors and report that information into Home Assistant. We are going to do this by editing the config file for the ESP32 SOC device in ESPHome. Previously, you clicked on the upload button in ESPHome. If you now click on ‘EDIT‘ button this will open a file editor which contains the configuration. To save you a lot of time looking through the ESP Home website on how to configure the Xiaomi LYWSDCGQ temperature sensor, I’ve included it below.

esphome:
  name: bluetooth_scanner
  platform: ESP32
  board: wemos_d1_mini32

wifi:
  networks:
  - ssid: !secret wifi_ssid_house
  - password: !secret wifi_pw
  - ssid: !secret wifi_ssid_office
  - password: !secret wifi_pw

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bluetooth Scanner"
    password: !secret password

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret password

ota:
  password: !secret password

# Enable Bluetooth scanning for this ESP32
esp32_ble_tracker:
  scan_parameters:
    interval: 3000ms
    window: 333ms
    
sensor:
  - platform: xiaomi_lywsdcgq
    mac_address: 58:2D:34:36:A9:20
    temperature:
      name: "Xiaomi_Bathroom Temperature"
    humidity:
      name: "Xiaomi_Bathroom Humidity"
    battery_level:
      name: "Xiaomi_Bathroom Battery Level"
      
  - platform: xiaomi_lywsdcgq
    mac_address: 58:2D:34:36:AA:27
    temperature:
      name: "Xiaomi_Livingroom Temperature"
    humidity:
      name: "Xiaomi_Livingroom Humidity"
    battery_level:
      name: "Xiaomi_Livingroom Battery Level"

  - platform: xiaomi_lywsdcgq
    mac_address: 58:2D:34:36:B4:43
    temperature:
      name: "Xiaomi_Bedroom Temperature"
    humidity:
      name: "Xiaomi_Bedroom Humidity"
    battery_level:
      name: "Xiaomi_Bedroom Battery Level"
      
  - platform: xiaomi_lywsdcgq
    mac_address: 58:2D:34:36:A1:1B
    temperature:
      name: "Xiaomi_Harrysroom Temperature"
    humidity:
      name: "Xiaomi_Harrysroom Humidity"
    battery_level:
      name: "Xiaomi_Harrysroom Battery Level"
      
time:
  - platform: homeassistant
    on_time:
      - seconds: 0
        minutes: 0
        hours: 4
        days_of_week: MON-SUN
        then:
           - switch.toggle: restart_switch

switch:
  - platform: restart
    name: "test_temperature Restart"
    id: restart_switch

Copy this config file into your own config and be sure to make the changes specific to your environment. When you have done this, you can save and click to validate the file. Assuming there are no errors, you can now click to upload the file, but this time you will choose OTA in the top right corner to update the device over Wifi.

It should be fairly self evident what this configuration above does, and there is documentation on the ESPHome website, but I will pick out a few details:-

wifi:
This section contains the wifi connection information for your network. Note I have specified two networks since I have two networks in my house. The ESPHme software will try to connect to one and then the other if it can’t find the first one. This is a great feature.

captive_portal:
This setting allows the ESP32 device to fall back to operating as a Wifi accepoint if ever it cannot find any wifi networks. This would be useful if you move the device and can no longer access through the wifi network(s) you configured. After a coupl of minuter it will defualt to this access point. You will then be able to http into the device after you have connected to its unique SSID.

api:
This tells ESPhome software to connect to Home Assistant.

ota:
Allows over the air updates and contains a password if you specified one when you created this device in ESPHome.

esp32_ble_tracker:
This sets up bluetooth scanning on the device. It has a scanning interval (time between scans) and a scanning window (duration of scan). I have found through trial & error, these numbers in my config file are optimal for the xiaomi_lywsdcgq temperature sensors. Since the sensors themselves are continuously broadcasting thier data, the scanner just listens for these broadcasts.

sensor:
Each xiaomi (lywsdcgq) temperature sensor has a mac address which you need to note down. To do this I downloaded a bluetooth tracker app to my phone and noted down the MAC address when the signal from each temperature sensor was picked up. They showed up on the tracker with Names like ‘MJ_HT_V1’. You can obviously change the names in each sensor based on the location of your own sensors. These are the names that will be reported in the Home Assistant event log.

time:
This sets up the ESP32 SOC to reboot every 24hrs. I do this just in case there are any reliability issues caused by a long uptime of the device.

switch:
This sets up a switch in Home Assistant, so I can also restart the ESP32 SOC from Home Assistant if I want to.

Finally

After all that, and a reboot of the ESP32 SOC and Home Assistant server, you should start receiving the xiaomi sensor data into Home Assistant and can display it how you want to. I use a couple of graphs as below.

These graphs were created in the Lovelace UI using the mini graph card. The Yaml config is below.

  - id: 1  # Automatically created id
    title: Rooms
    icon: mdi:home
    cards:
      - id: 84937a944ee54141bc9be3124c2aecfe  # Automatically created id
        type: vertical-stack
        cards:
          - type: horizontal-stack
            cards:
                - type: custom:mini-graph-card
                  entities:
                    - entity: sensor.fibaro_system_fgms001_motion_sensor_temperature
                      name: Dining Room
                    - entity:  sensor.loft_room_temperature
                      name: Rosalyn's Room
                    - entity:  sensor.laundry_room_temperature
                      name: Laundry Room
                    - entity:  sensor.xiaomi_bathroom_temperature
                      name: Bath Room
                    - entity:  sensor.xiaomi_bedroom_temperature
                      name: Bed Room
                    - entity:  sensor.xiaomi_harrysroom_temperature
                      name: Harry's Room
                    - entity:  sensor.xiaomi_livingroom_temperature
                      name: Living Room                      
                  name: Temperatures
                  hours_to_show: 12
                  points_per_hour: 4
                  
          - type: horizontal-stack
            cards:
                - type: custom:mini-graph-card
                  entities:
                    - entity:  sensor.loft_room_humidity
                      name: Rosalyn's Room
                    - entity:  sensor.laundry_room_humidity
                      name: Laundry Room
                    - entity:  sensor.xiaomi_bathroom_humidity
                      name: Bath Room
                    - entity:  sensor.xiaomi_bedroom_humidity
                      name: Bed Room
                    - entity:  sensor.xiaomi_harrysroom_humidity
                      name: Harry's Room
                    - entity:  sensor.xiaomi_livingroom_humidity
                      name: Living Room   
                  name: Humidity
                  hours_to_show: 12
                  points_per_hour: 4

I hope you find this project enjoyable. You will see from the ESPHome website, there are many other sensors which can be fitted. Initially I had to find the right location in my home to place the ESP32 SOC so that it received a good signal from every sensor. I settled on the middle floor of the house. I have had this running for approx. 6 months now and absolutely no reliability problems.