Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

MiTemperature2

> 编程语言
Open source

Read the values of the Xiaomi Mi Bluetooth Temperature sensor 2 including custom encrypted format.

760 stars0 likes0 views
WebsiteGitHub

About

Read the values of the Xiaomi Mi Bluetooth Temperature sensor 2 including custom encrypted format.

Read data from Xiaomi Mijia LYWSD03MMC Bluetooth 4.2 Temperature Humidity sensor

With this script you can read out the data of your LYWSD03MMC (and some other) sensors, e.g. with Raspberry Pi. Note Raspbery Pi 4 has a very limited bluetooth range. Pi Zero W gives much longer range.

Table of Contents

  • Read data from Xiaomi Mijia LYWSD03MMC Bluetooth 4.2 Temperature Humidity sensor
    • Table of Contents
    • Supported sensors
    • Prequisites / Requirements
      • Requirements for reading sensors in passive mode
    • Usage
      • Passive Mode Usage
    • Built in MQTT support
    • Tips
      • Minus degrees
      • High battery usage (not applicable for recommended passive mode)
    • Sample output
      • More info
      • Troubleshooting
    • Calibration
      • Offset calibration
      • Two point calibration
    • Docker usage
    • Callback for processing the data
    • Send metrics to Prometheus
    • Node-RED flows
    • Callback to Home-Assistant MQTT integration
      • Home-Assistant device details:

Important release information 6.0 and above

Passive mode is now standard, so commandline interface has changed. The new programm name is MiTemperature2.py Old file LYWSD03MMC.py is still present, but not supported anymore.

requests module has been replaced by python-builtin urllib. This makes startup twice as fast on Raspberry PI Zero There is a simple debugHTTPServer.py to see the HTTP-requests the httpcallback is sending.

Supported sensors

This script was originally made to support LYWSD03MMC devices running Xiaomi firmware but support for other hardware and firmware was added later.

Passive mode (recommended) device support: LYWSD03MMC, MHO-C401, CGG1-M, CGGDK2, LYWSDCGQ

Normal (active connection) device support has been dropped since Version 6. You need now the pvvs Firmware. Get it here https://github.com/pvvx/ATC_MiThermometer

BTHome V2 pakets are since v6.0 also supported. When sensor sends packets in BT Home v2 Standard, there are different kinds of packets. One type containts Temperature and Humidity, the other only battery value. Temperature and battery packets are send more often. The missing values (temperature and humidity at battery packets and battery and voltage at temperature/humidity packages) are cached. Only when scripts starts and the cache is empty, the missing values are transmitted as 0. To prevent these packets beeing sent, use -bot switch (see below).

Qingping format advertisements are supported so it's possible this script also supports advertisements sent by other types of Qingping CGG* devices but this is not tested. CGG1-M (Mijia version) devices can also run custom ATC firmware by pvvx. Then they behave exactly the same as LYWSD03MMCs running custom firmware. Qingping sensors only send Qingping format advertisements when running the original Qingping firmware.

LYWSDCGQ format (the old round one with an aaa battery) is supported. The sensor can send four types of advertisements: 1) both temperature and humidity, 2) only temperature, 3) only humidity and 4) only battery. All of these are supported. The missing values are 0 for the callback, so this needs to be handled in the callback script implementation. There are roughly about 20 advertisements with both temperature and humidity per minute and about two single information advertisements per minute for each information, temperature, humidity and battery.

Prequisites / Requirements

This is not needed if you are using the docker image. However this is often oudated See Docker usage

You need Python3 3.7 or above, which should be easily feasible nowadays.

Using venv (recommended)

…

ini [info] info1=now all available options are listet. If offset1, offset2, calpoint1 and calpoint2 are given 2Point calibration is used instead of humidityOffset. info2= Note options are case sensitive ;info4=Use semicolon to comment out lines sensorname=Specify an easy readable name humidityOffset=-5 offset1 = -10 offset2 = 10 calpoint1 = 33 calpoint2 = 75 topic=This sensor data will be published with this name when using integrated MQTT ;If no topic is given, then default topic from MQTT config is used decryption = For encrypted sensor give the key preceded by k. It is advised to use an individual key for every sensor. Please also set a PIN to prevent adversary from connecting and reading key. ;example for key ;decryption = k9088F9B4F7EC3BB52378F8F31CB74073

;[A4:C1:38:DD:EE:FF] ; sensorname=Bathroom ; humidityOffset=-30 ; offset1 = -10 ; offset2 = -10 ; calpoint1 = 33 ; calpoint2 = 75 ; topic = basement/Bathroom

[A4:C1:38:1F:4D:81] sensorname=Living Room offset1 = -2 offset2 = 2 calpoint1 = 33 calpoint2 = 75 topic=basement/Living_Room decryption = k9088F9B4F7EC3BB52378F8F31CB74073

…

json { "temperature": 12.5, "humidity": 58, "voltage": 2.882, "calibratedHumidity": 57, "battery": 75, "timestamp": 1619805618, "sensor": "Living Room", "rssi": -88, "receiver": "raspberrypi" }

…

ini ;For configuration options please see DEFAULT section below and https://github.com/JsBergbau/MiTemperature2/blob/master/README.md [MQTT] broker=127.0.0.1 port=1883 topic=ATCThermometer ;username= ;password= receivername= ;subtopics=temperature,humidity

;Enable TLS / MQTTS tls=0 ;CA file cacerts=isrgrootx1.pem ;Certificate file certificate= ;Certificate key file certificate_key= ;Enable TLS insecure mode insecure=0

[DEFAULT] ;Do not change here, leave it as it is broker=127.0.0.1 username= password= port=1883 topic=ATCThermometer ;lastwill message lastwill= ;lastwill topic lwt= clientid= ;If not specified, hostname is used as receivername receivername= ;enable subtopics like temperature and humidity, combine with comma without spaces so "temperature,humidity" ;to disable JSON output, add "nojson" ;subtopics=nojson,temperature,humidity will send temperature and humidity subtopics=

…

ATCThermometer/temperature ATCThermometer/voltage ATCThermometer/humidity

or when you have specified a topic for your particular sensor like `basement/livingroom/` then it would like

basement/livingroom/temperature basement/livingroom/voltage basement/livingroom/humidity

…

./MiTemperature2.py --mqttconfigfile mqtt.conf

MiTemperature2 / ATC Thermometer version 6.0

Please read README.md in this folder. Latest version is available at https://github.com/JsBergbau/MiTemperature2#readme This file explains very detailed about the usage and covers everything you need to know as user.

Script started

All devices within reach are read out, unless a devicelistfile and --onlydevicelist is specified. In this mode debouncing is not available. Rounding option will round humidity and temperature to one decimal place. Passive mode usually requires root rights. If you want to use it with normal user rights, please execute "sudo setcap cap_net_raw,cap_net_admin+eip $(eval readlink -f which python3)" You have to redo this step if you upgrade your python version.

Power ON bluetooth device 0 Bluetooth device 0 is already enabled Enable LE scan scan params: interval=1280.000ms window=1280.000ms own_bdaddr=public whitelist=no socket filter set to ptype=HCI_EVENT_PKT event=LE_META_EVENT Listening ... BLE packet - ATC1441: AA:BB:CC:A1 00 1110161a18aabbcca100ea402f0a46a7 -58 Temperature: 23.4 Humidity: 64 Battery voltage: 2.63 V RSSI: -58 dBm Battery: 47 %

BLE packet - ATC1441: AA:BB:CC:A2 00 1110161a18aabbcca200e141450b09fe -48 Temperature: 22.5 Humidity: 65 Battery voltage: 2.825 V RSSI: -48 dBm Battery: 69 %

MQTT connected with result code 0 MQTT published, Client: Userdata: None mid: 1 MQTT published, Client: Userdata: None mid: 2 BLE packet - Custom: AA:BB:CC:A3 00 1602010612161a18a3ccbbaa520965180a0b594c04 -65 Temperature: 23.86 Humidity: 62.45 Battery voltage: 2.826 V RSSI: -65 dBm Battery: 89 %

MQTT published, Client: Userdata: None mid: 3 BLE packet - Custom: AA:BB:CC:A4 00 1312161a18a4ccbbaa4509b2188a0b537704 -70 Temperature: 23.73 Humidity: 63.22 Battery voltage: 2.954 V RSSI: -70 dBm Battery: 83 %

MQTT published, Client: Userdata: None mid: 4 BLE packet - ATC1441: AA:BB:CC:A5 00 1110161a18aabbcca5010c351e09a6dd -46 Temperature: 26.8 Humidity: 53 Battery voltage: 2.47 V RSSI: -46 dBm Battery: 30 %

BLE packet - ATC1441: AA:BB:CC:A6 00 1110161a18aabbcca600eb3e2a0a18f9 -45 Temperature: 23.5 Humidity: 62 Battery voltage: 2.584 V RSSI: -45 dBm Battery: 42 %

MQTT published, Client: Userdata: None mid: 5 MQTT published, Client: Userdata: None mid: 6 BLE packet - BTHome : AA:BB:CC:A7 00 120201060e16d2fc4000cf015b020109031419 -62 Packet type: Data Temperature: 23.05 Humidity: 64.2 Battery voltage: 0 V RSSI: -62 dBm Battery: 0 %


### More info

This section remains for historical reasons. It was used in connect mode.

If you like gatttool you can use it, too. However it didn't notice when BT connection was lost, while this Python-Script automatically reestablishes the connection.

gatttool -I connect AA:BB:CC:DD:EE:FF #enable notifications char-write-req 0x0038 0100 #enable lower power mode, please don't do when you want to interact with the device, because often then connection gets lost char-write-req 0x0046 f40100 #Read battery-Level, just for reference, since it is always 99 consider: note value is in Hex format char-read-hnd 0x001b

…

offset1 = -6 offset2 = 5 calpoint1 = 33 calpoint2 = 75


## Docker usage
There is a docker image available on dockerhub you can use. See https://docs.docker.com/engine/install/debian/ for instructions on how to install docker on raspberry pi.   
Once this is installed, you can create a sensors.ini and mqtt.conf file on the host machine and use this to run the container using the these files

docker run --net=host --privileged -it -v $(pwd)/sensors.ini:/app/sensors.ini -v $(pwd)/mqtt.conf:/app/mqtt.conf antxxxx/mitemperature2 -a --devicelistfile /app/sensors.ini --mqttconfigfile /app/mqtt.conf


## Callback for processing the data

Via the --callback option a script can be passed to sent the data to.
Example
`./MiTemperature2.py --callback sendToFile.sh`
If you don't give the sensor a name, the MAC-Address is used. The callback script must be within the same folder as this script.
The values outputted depends on the options like calibration or battery. So the format is printed in the first argument.
Example callback

#!/bin/bash

This is quite useful for testing

echo $@ >> data.txt exit 0

…

./iniToJSON.py -h usage: iniToJSON.py [-h] --readfile filename [--writefile filename]

optional arguments: -h, --help show this help message and exit --readfile filename, -rf filename Specify filename of ini-File to convert --writefile filename, -wf filename Specify filename of json-File to convert

Use these to convert between ini and JSON for usage in Node-RED.

```./jsonToIni.py -h
usage: jsonToIni.py [-h] --readfile filename --writefile filename

optional ar

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言