diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 86651bbcf5..6005cdbf22 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -1714,12 +1714,12 @@ void APIConnection::on_home_assistant_state_response(const HomeAssistantStateRes // Create null-terminated state for callback (parse_number needs null-termination) // HA state max length is 255, so 256 byte buffer covers all cases char state_buf[256]; - size_t copy_len = msg.state.size(); + size_t copy_len = msg.state_len; if (copy_len >= sizeof(state_buf)) { copy_len = sizeof(state_buf) - 1; // Truncate to leave space for null terminator } if (copy_len > 0) { - memcpy(state_buf, msg.state.data(), copy_len); + memcpy(state_buf, msg.state, copy_len); } state_buf[copy_len] = '\0'; it.callback(StringRef(state_buf, copy_len));