diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index bcdb5c6242..897aa2aa78 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1687,6 +1687,24 @@ void APIConnection::process_batch_() { return; } + // Fast path for single message + if (this->deferred_batch_.items.size() == 1) { + const auto &item = this->deferred_batch_.items[0]; + auto message = item.creator(item.entity); + if (message) { + if (this->send_message(*message)) { + this->deferred_batch_.clear(); + } else { + // Message couldn't be sent, keep it for retry + // Batch is still scheduled, will retry later + } + } else { + this->deferred_batch_.clear(); // Creator failed, remove item + } + return; + } + + // Multiple messages - use batching // Single pass: create messages, calculate sizes and check what fits struct MessageData { std::unique_ptr message;