From cc6876d77676f8de067e594ad06260239fcb35c0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 21 Aug 2026 17:02:18 -0500 Subject: [PATCH] Count client states fresh when choosing the scan window --- esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp index f0f7393ad2..21053abe85 100644 --- a/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp +++ b/esphome/components/esp32_ble_tracker/esp32_ble_tracker.cpp @@ -258,7 +258,9 @@ void ESP32BLETracker::start_scan_(bool first) { this->scan_params_.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL; this->scan_params_.scan_interval = this->scan_interval_; uint32_t window = this->scan_window_; - this->scan_window_reduced_ = this->connection_scan_window_ != 0 && this->client_state_counts_.active > 0; + // Count fresh rather than reading the loop() cache: an automation can start + // a scan before loop() has refreshed the counts for a new connection. + this->scan_window_reduced_ = this->connection_scan_window_ != 0 && this->count_client_states_().active > 0; if (this->scan_window_reduced_) { // While a GATT connection is active, fall back to the connection scan // window so the connection events get guaranteed airtime instead of