ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
xiaomi_miscale.cpp
Go to the documentation of this file.
1#include "xiaomi_miscale.h"
3#include "esphome/core/log.h"
4
5#ifdef USE_ESP32
6
8
9static const char *const TAG = "xiaomi_miscale";
10
12 ESP_LOGCONFIG(TAG, "Xiaomi Miscale");
13 LOG_SENSOR(" ", "Weight", this->weight_);
14 LOG_SENSOR(" ", "Impedance", this->impedance_);
15}
16
18 if (device.address_uint64() != this->address_) {
19 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
20 return false;
21 }
22 char addr_buf[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
23 const char *addr_str = device.address_str_to(addr_buf);
24 ESP_LOGVV(TAG, "parse_device(): MAC address %s found.", addr_str);
25
26 bool success = false;
27 for (auto &service_data : device.get_service_datas()) {
28 auto res = parse_header_(service_data);
29 if (!res.has_value())
30 continue;
31
32 if (!parse_message_(service_data.data, *res))
33 continue;
34
35 if (!report_results_(res, addr_str))
36 continue;
37
38 if (res->weight.has_value() && this->weight_ != nullptr)
39 this->weight_->publish_state(*res->weight);
40
41 if (this->impedance_ != nullptr) {
42 if (res->version == 1) {
43 ESP_LOGW(TAG, "Impedance is only supported on version 2. Your scale was identified as version 1.");
44 } else {
45 if (res->impedance.has_value()) {
46 this->impedance_->publish_state(*res->impedance);
47 } else {
49 this->impedance_->publish_state(NAN);
50 }
51 }
52 }
53 success = true;
54 }
55
56 return success;
57}
58
59optional<ParseResult> XiaomiMiscale::parse_header_(const esp32_ble_tracker::ServiceData &service_data) {
60 ParseResult result;
61 if (service_data.uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(0x181D) && service_data.data.size() == 10) {
62 result.version = 1;
63 } else if (service_data.uuid == esp32_ble_tracker::ESPBTUUID::from_uint16(0x181B) && service_data.data.size() == 13) {
64 result.version = 2;
65 } else {
66 char uuid_buf[esp32_ble::UUID_STR_LEN];
67 ESP_LOGVV(TAG,
68 "parse_header(): Couldn't identify scale version or data size was not correct. UUID: %s, data_size: %d",
69 service_data.uuid.to_str(uuid_buf), service_data.data.size());
70 return {};
71 }
72
73 return result;
74}
75
76bool XiaomiMiscale::parse_message_(const std::vector<uint8_t> &message, ParseResult &result) {
77 if (result.version == 1) {
78 return parse_message_v1_(message, result);
79 } else {
80 return parse_message_v2_(message, result);
81 }
82}
83
84bool XiaomiMiscale::parse_message_v1_(const std::vector<uint8_t> &message, ParseResult &result) {
85 // message size is checked in parse_header
86 // 1-2 Weight (MISCALE 181D)
87 // 3-4 Years (MISCALE 181D)
88 // 5 month (MISCALE 181D)
89 // 6 day (MISCALE 181D)
90 // 7 hour (MISCALE 181D)
91 // 8 minute (MISCALE 181D)
92 // 9 second (MISCALE 181D)
93
94 const uint8_t *data = message.data();
95
96 // weight, 2 bytes, 16-bit unsigned integer, 1 kg
97 const int16_t weight = uint16_t(data[1]) | (uint16_t(data[2]) << 8);
98 if (data[0] == 0x22 || data[0] == 0xa2) {
99 result.weight = weight * 0.01f / 2.0f; // unit 'kg'
100 } else if (data[0] == 0x12 || data[0] == 0xb2) {
101 result.weight = weight * 0.01f * 0.6f; // unit 'jin'
102 } else if (data[0] == 0x03 || data[0] == 0xb3) {
103 result.weight = weight * 0.01f * 0.453592f; // unit 'lbs'
104 }
105
106 return true;
107}
108
109bool XiaomiMiscale::parse_message_v2_(const std::vector<uint8_t> &message, ParseResult &result) {
110 // message size is checked in parse_header
111 // 2-3 Years (MISCALE 2 181B)
112 // 4 month (MISCALE 2 181B)
113 // 5 day (MISCALE 2 181B)
114 // 6 hour (MISCALE 2 181B)
115 // 7 minute (MISCALE 2 181B)
116 // 8 second (MISCALE 2 181B)
117 // 9-10 impedance (MISCALE 2 181B)
118 // 11-12 weight (MISCALE 2 181B)
119
120 const uint8_t *data = message.data();
121
122 bool has_impedance = ((data[1] & (1 << 1)) != 0);
123 bool is_stabilized = ((data[1] & (1 << 5)) != 0);
124 bool load_removed = ((data[1] & (1 << 7)) != 0);
125
126 if (!is_stabilized || load_removed) {
127 return false;
128 }
129
130 // weight, 2 bytes, 16-bit unsigned integer, 1 kg
131 const int16_t weight = uint16_t(data[11]) | (uint16_t(data[12]) << 8);
132 if (data[0] == 0x02) {
133 result.weight = weight * 0.01f / 2.0f; // unit 'kg'
134 } else if (data[0] == 0x03) {
135 result.weight = weight * 0.01f * 0.453592f; // unit 'lbs'
136 }
137
138 if (has_impedance) {
139 // impedance, 2 bytes, 16-bit
140 const int16_t impedance = uint16_t(data[9]) | (uint16_t(data[10]) << 8);
141 result.impedance = impedance;
142
143 if (impedance == 0 || impedance >= 3000) {
144 return false;
145 }
146 }
147
148 return true;
149}
150
151bool XiaomiMiscale::report_results_(const optional<ParseResult> &result, const char *address) {
152 if (!result.has_value()) {
153 ESP_LOGVV(TAG, "report_results(): no results available.");
154 return false;
155 }
156
157 ESP_LOGD(TAG, "Got Xiaomi Miscale v%d (%s):", result->version, address);
158
159 if (result->weight.has_value()) {
160 ESP_LOGD(TAG, " Weight: %.2fkg", *result->weight);
161 }
162 if (result->impedance.has_value()) {
163 ESP_LOGD(TAG, " Impedance: %.0fohm", *result->impedance);
164 }
165
166 return true;
167}
168
169} // namespace esphome::xiaomi_miscale
170
171#endif
uint8_t address
Definition bl0906.h:4
ESPDEPRECATED("Use to_str() instead. Removed in 2026.8.0", "2026.2.0") std const char * to_str(std::span< char, UUID_STR_LEN > output) const
Definition ble_uuid.cpp:146
const char * address_str_to(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf) const
Format MAC address into provided buffer, returns pointer to buffer for convenience.
const std::vector< ServiceData > & get_service_datas() const
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool parse_message_v1_(const std::vector< uint8_t > &message, ParseResult &result)
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
bool parse_message_(const std::vector< uint8_t > &message, ParseResult &result)
optional< ParseResult > parse_header_(const esp32_ble_tracker::ServiceData &service_data)
bool report_results_(const optional< ParseResult > &result, const char *address)
bool parse_message_v2_(const std::vector< uint8_t > &message, ParseResult &result)
const LogString * message
Definition component.cpp:35