Repository: esphome/esphome (2026-04-17T13:25:59Z)

Issue:
2026.4.0 Compilation error when using delay action in http_request response
### The problem

To reproduce the issue, try compiling the attached YAML. Tested on ESP32 and ESP32-C3, I think it may occur on any device type. Checked both EspHome - in the add-on and in a command line installation. 

As a workaround for the error, I placed the delay action inside script. This compiles without errors:

```
...
        on_response:
           then:
              - script.execute: delay_script

script:
 - id: delay_script
      then:
       - delay: 5s 
...
```

### Which version of ESPHome has the issue?

ESPHome version 2026.4.0

### What type of installation are you using?

Home Assistant Add-on

### What platform are you using?

ESP32

### Component causing the issue

http_request

### YAML Config

```yaml
esphome:
  name: test-esp32
  friendly_name: test esp32

esp32:
  board: esp32dev
  framework:
    type: esp-idf

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

http_request:

button:
  - platform: template
    id: test
    on_press: 
      then:
        - http_request.get:
            url: http://127.0.0.1
            request_headers:
              Content-Type: application/json
            capture_response: true
            on_response:
              then:
                - delay: 5s
```

### Anything in the logs that might be useful for us?

```txt
In file included from src/esphome.h:26,
                 from src/main.cpp:3:
src/esphome\core\base_automation.h: In instantiation of 'void esphome::DelayAction<Ts>::play_complex(const Ts& ...) [with Ts = {std::shared_ptr<esphome::http_request::HttpContainer>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]':
src/esphome\core\base_automation.h:187:8:   required from here
  187 |   void play_complex(const Ts &...x) override {
      |        ^~~~~~~~~~~~
src/esphome\core\base_automation.h:208:50: error: binding reference of type 'std::__cxx11::basic_string<char>&' to 'const std::__cxx11::basic_string<char>' discards qualifiers
  208 |       auto f = [this, x...]() { this->play_next_(x...); };
      |                                                  ^
In file included from src/esphome\components\button\automation.h:4,
                 from src/esphome.h:3:
src/esphome/core/automation.h:527:29: note:   initializing argument 2 of 'void esphome::Action<Ts>::play_next_(const Ts& ...) [with Ts = {std::shared_ptr<esphome::http_request::HttpContainer>, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&}]'
  527 |   void play_next_(const Ts &...x) {
      |                   ~~~~~~~~~~^~~~
Compiling .pioenvs\test-esp32\esp-tls\esp_tls_mbedtls.c.o
Compiling .pioenvs\test-esp32\esp_app_format\esp_app_desc.c.o
*** [.pioenvs\test-esp32\src\main.cpp.o] Error 1
```

### Additional information

_No response_


Discussion:

Patch:
diff --git a/esphome/core/base_automation.h b/esphome/core/base_automation.h
index 11133d39739e..17f937d10d90 100644
--- a/esphome/core/base_automation.h
+++ b/esphome/core/base_automation.h
@@ -205,7 +205,9 @@ template<typename... Ts> class DelayAction : public Action<Ts...>, public Compon
     } else {
       // For delays with arguments, capture by value to preserve argument values
       // Arguments must be copied because original references may be invalid after delay
-      auto f = [this, x...]() { this->play_next_(x...); };
+      // `mutable` is required so captured copies of non-const reference args (e.g. std::string&)
+      // are passed as non-const lvalues to play_next_(const Ts&...) where Ts may be `T&`
+      auto f = [this, x...]() mutable { this->play_next_(x...); };
       App.scheduler.set_timer_common_(this, Scheduler::SchedulerItem::TIMEOUT, Scheduler::NameType::NUMERIC_ID_INTERNAL,
                                       nullptr, static_cast<uint32_t>(InternalSchedulerID::DELAY_ACTION),
                                       this->delay_.value(x...), std::move(f),


Test patch:
diff --git a/tests/components/http_request/http_request.yaml b/tests/components/http_request/http_request.yaml
index 13ca5ceba034..ef67671c91d1 100644
--- a/tests/components/http_request/http_request.yaml
+++ b/tests/components/http_request/http_request.yaml
@@ -45,6 +45,11 @@ esphome:
                   args:
                     - response->status_code
                     - body.c_str()
+              - delay: 1s
+              - logger.log:
+                  format: "After delay, body still: %s"
+                  args:
+                    - body.c_str()
 
 http_request:
   useragent: esphome/tagreader