Some Xiaomi temperature sensors advertise sensor data over Bluetooth Low Energy (BLE) packets. The data is encoded but we can decode it and make this data available in Home Assistant.
A quick search revealed a library that can decode the service data for these devices. I played with this for a bit but ended up finding a much easier solution. As it turns out, the different values for temperature and humidty have been converted to hex format and concatenated, forming the servicedata
string in the BLE message.
I can see these messages on the MQTT server via a Bluetooth to MQTT gateway. I linked to a previous post below for how to set up OpenMQTTGateway and the advantages of RF and BLE devices over Wifi.
Adding an RF gateway to your Home Assistant setup makes your setup incredibly versatile because it opens up a multitude of low-cost automation opportunities. This project enables you to interface your Home Assistant setup with RF devices, allowing you to add cheap RF sensors to your home automation setup!
MQTT Payload received by OpenMQTTGateway
{"id":"58:2D:34:11:D5:E1","name":"Qingping Temp & RH Lite","rssi":-78,"distance":7.85288,"servicedata":"30586f0626e1d511342d5808"}
Home Assistant Template Sensors
Given all this information– and some help from the community–we are able to create some Jinja templates to extract the correct values and convert them to temperature and humidity values.
- platform: mqtt
name: "Bedroom Mi Temp"
device_class: temperature
state_topic: "home/gw/BTtoMQTT/582D3411D5E1"
value_template: "{{ ((value_json.servicedata[10:12] | int(value_json.servicedata[10:12], 16)))/10 }}"
unit_of_measurement: "°C"
- platform: mqtt
name: "Bedroom Mi Hum"
device_class: humidity
state_topic: "home/gw/BTtoMQTT/582D3411D5E1"
value_template: "{{ ((value_json.servicedata[12:14] | int(value_json.servicedata[12:14], 16)))/10 }}"
unit_of_measurement: "Hum %"