Read Govee Temperature Humidity BLE Advertisements with esp32 and esphome!

For anyone using a Govee 5075 sensor, the following script will get you up and running:

- platform: template
    name: "Kitchen Humidity"
    id: kitchen_humidity
    unit_of_measurement: '%'
    icon: "mdi:water-percent"
  - platform: template
    name: "Kitchen Temperature"
    id: kitchen_temperature
    unit_of_measurement: '°C'
    icon: "mdi:thermometer"
  - platform: template
    name: "Kitchen Battery"
    id: kitchen_battery
    unit_of_measurement: '%'
    icon: "mdi:battery"
- mac_address: A4:C1:38:AA:BB:CC #kitchen
      manufacturer_id: EC88
      then:
        - lambda: |-
            const int basenum = (int16_t(x[1]) << 16) + (int16_t(x[2]) << 8) + int16_t(x[3]);
            ESP_LOGD("ble_adv", "goveesensor basenum: (%d)", basenum);
            const float temperature = basenum / 10000.0f;
            const float humidity = (basenum % 1000) / 10.0f;
            const float battery_level = uint16_t(x[4]) / 1.0f;
            ESP_LOGD("ble_adv", "  Temperature: %.2f°C", temperature);
            ESP_LOGD("ble_adv", "  Humidity: %.2f", humidity);
            ESP_LOGD("ble_adv", "  Battery Level: %.0f percent", battery_level);
            id(kitchen_temperature).publish_state(temperature);
            id(kitchen_humidity).publish_state(humidity);
            id(kitchen_battery).publish_state(battery_level);

Complete noob questions, but does anyone know of a way to abstract this to a script that can take parameters? I have 4 of these sensors around the house and while the code isn’t that long, I’d like to have one function that can take the sensor data and a name and handle the decoding / publishing instead of having multiple copies…

4 Likes