Skip to content

Instantly share code, notes, and snippets.

@jklap
Last active January 22, 2024 22:24
Show Gist options
  • Save jklap/1e80ea3a5bb7ccc5021d276f1201450f to your computer and use it in GitHub Desktop.
Save jklap/1e80ea3a5bb7ccc5021d276f1201450f to your computer and use it in GitHub Desktop.
Wave2 Battery & esphome
# https://esphome.io/guides/automations.html#interval-component
interval:
# trigger the update to fetch battery
- interval: ${temp_update_interval}
then:
- ble_client.ble_write:
id: "airthings_${wave_serial}"
service_uuid: "B42E4A8E-ADE7-11E4-89D3-123B93F75CBA"
characteristic_uuid: "B42E50D8-ADE7-11E4-89D3-123B93F75CBA" # 0x10
value: [ 0x6d ]
sensor:
- platform: template
name: "Airthings ${wave_serial} Battery Voltage"
id: "airthings_${wave_serial}_battery_v"
unit_of_measurement: "V"
accuracy_decimals: 2
update_interval: never
- platform: copy
name: "Airthings ${wave_serial} Battery Level"
id: "airthings_${wave_serial}_battery_level"
source_id: "airthings_${wave_serial}_battery_v"
unit_of_measurement: "%"
device_class: battery
accuracy_decimals: 0
filters:
- calibrate_linear:
- 0.0 -> 0
- 2.0 -> 0
- 3.0 -> 100
- platform: ble_client
id: "airthings_${wave_serial}_config"
type: characteristic
ble_client_id: "airthings_${wave_serial}"
service_uuid: "B42E4A8E-ADE7-11E4-89D3-123B93F75CBA"
characteristic_uuid: "B42E50D8-ADE7-11E4-89D3-123B93F75CBA" # 0x10
internal: true
notify: true
lambda: |-
uint16_t battery = x[26] + (x[27] << 8);
float battery_f = battery / 1000.0f;
id(airthings_${wave_serial}_battery_v).publish_state(battery_f);
// following might be a better calculation vs the linear calibrate.. need to verify with the app
float battery_pct = 100 * (battery_f - 2.0 ) / (3.0 - 2.0);
//id(airthings_${wave_serial}_battery_level).publish_state(battery_pct);
ESP_LOGI("custom", "calc'ed battery pct %f", battery_pct);
return 0.0;
button:
# flash the ring light
- platform: template
name: "${wave_serial} Ring Light"
on_press:
- ble_client.ble_write:
id: "airthings_${wave_serial}"
service_uuid: "B42E4A8E-ADE7-11E4-89D3-123B93F75CBA"
characteristic_uuid: "B42E50D8-ADE7-11E4-89D3-123B93F75CBA" # 0x10
value: [ 0x67, 0x08 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment