ESPHome 2026.5.1
Loading...
Searching...
No Matches
inkbird_ibsth1_mini.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
4#ifdef USE_ESP32
5
7
8static const char *const TAG = "inkbird_ibsth1_mini";
9
11 ESP_LOGCONFIG(TAG, "Inkbird IBS TH1 MINI");
12 LOG_SENSOR(" ", "Temperature", this->temperature_);
13 LOG_SENSOR(" ", "External Temperature", this->external_temperature_);
14 LOG_SENSOR(" ", "Humidity", this->humidity_);
15 LOG_SENSOR(" ", "Battery Level", this->battery_level_);
16}
17
19 // The below is based on my research and reverse engineering of a single device
20 // It is entirely possible that some of that may be inaccurate or incomplete
21
22 // for Inkbird IBS-TH1 Mini device we expect
23 // 1) expected mac address
24 // 2) device address type == PUBLIC
25 // 3) no service data
26 // 4) one manufacturer data
27 // 5) the manufacturer data should contain a 16-bit uuid amd a 7-byte data vector
28 // 6) the 7-byte data component should have data[2] == 0 and data[6] == 8
29
30 // the address should match the address we declared
31 if (device.address_uint64() != this->address_) {
32 ESP_LOGVV(TAG, "parse_device(): unknown MAC address.");
33 return false;
34 }
35 if (device.get_address_type() != BLE_ADDR_TYPE_PUBLIC) {
36 ESP_LOGVV(TAG, "parse_device(): address is not public");
37 return false;
38 }
39 if (!device.get_service_datas().empty()) {
40 ESP_LOGVV(TAG, "parse_device(): service_data is expected to be empty");
41 return false;
42 }
43 const auto &mnf_datas = device.get_manufacturer_datas();
44 if (mnf_datas.size() != 1) {
45 ESP_LOGVV(TAG, "parse_device(): manufacturer_datas is expected to have a single element");
46 return false;
47 }
48 const auto &mnf_data = mnf_datas[0];
49 if (mnf_data.uuid.get_uuid().len != ESP_UUID_LEN_16) {
50 ESP_LOGVV(TAG, "parse_device(): manufacturer data element is expected to have uuid of length 16");
51 return false;
52 }
53 if (mnf_data.data.size() != 7) {
54 ESP_LOGVV(TAG, "parse_device(): manufacturer data element length is expected to be of length 7");
55 return false;
56 }
57 if ((mnf_data.data[6] != 8) && (mnf_data.data[6] != 6)) {
58 ESP_LOGVV(TAG, "parse_device(): unexpected data");
59 return false;
60 }
61
62 // sensor output encoding
63 // data[5] is a battery level
64 // data[0] and data[1] is humidity * 100 (in pct)
65 // uuid is a temperature * 100 (in Celsius)
66 // when data[2] == 0 temperature is from internal sensor (IBS-TH1 or IBS-TH1 Mini)
67 // when data[2] == 1 temperature is from external sensor (IBS-TH1 only)
68
69 // Create empty variables to pass automatic checks
70 auto temperature = NAN;
71 auto external_temperature = NAN;
72
73 // Read bluetooth data into variable
74 auto measured_temperature = ((int16_t) mnf_data.uuid.get_uuid().uuid.uuid16) / 100.0f;
75
76 // Set temperature or external_temperature based on which sensor is in use
77 if (mnf_data.data[2] == 0) {
78 temperature = measured_temperature;
79 } else if (mnf_data.data[2] == 1) {
80 external_temperature = measured_temperature;
81 } else {
82 ESP_LOGVV(TAG, "parse_device(): unknown sensor type");
83 return false;
84 }
85
86 auto battery_level = mnf_data.data[5];
87 auto humidity = ((mnf_data.data[1] << 8) + mnf_data.data[0]) / 100.0f;
88
89 // Send temperature only if the value is set
90 if (!std::isnan(temperature) && this->temperature_ != nullptr) {
92 }
93 if (!std::isnan(external_temperature) && this->external_temperature_ != nullptr) {
94 this->external_temperature_->publish_state(external_temperature);
95 }
96 if (this->humidity_ != nullptr) {
97 this->humidity_->publish_state(humidity);
98 }
99 if (this->battery_level_ != nullptr) {
100 this->battery_level_->publish_state(battery_level);
101 }
102
103 return true;
104}
105
106} // namespace esphome::inkbird_ibsth1_mini
107
108#endif
esp_ble_addr_type_t get_address_type() const
const std::vector< ServiceData > & get_service_datas() const
const std::vector< ServiceData > & get_manufacturer_datas() const
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
uint16_t temperature
Definition sun_gtil2.cpp:12