You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/components/api.md
+180-7Lines changed: 180 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -58,7 +58,12 @@ api:
58
58
> The defaults are set to balance memory usage with allowing multiple simultaneous connections.
59
59
60
60
- **max_send_queue** (*Optional*, int): The maximum number of messages that can be queued for sending per connection before the connection is dropped. Must be between 1 and 64.
61
-
Defaults to `5` for ESP8266/RP2040, `8` for ESP32/BK72xx/RTL87xx/LN882x, `16` for host platform. This prevents memory exhaustion when a client is slow or network-stalled.
61
+
Defaults to:
62
+
- `5`for ESP8266/RP2040,
63
+
- `8`for ESP32/BK72xx/LN882x/nRF52/RTL87xx,
64
+
- `16`for host platform.
65
+
66
+
This prevents memory exhaustion when a client is slow or network-stalled.
62
67
Each queued message uses approximately 8-12 bytes of overhead plus the message size.
63
68
64
69
> [!NOTE]
@@ -104,7 +109,7 @@ api:
104
109
105
110
- **id** (*Optional*, [ID](/guides/configuration-types#id)): Manually specify the ID used for code generation.
106
111
- **password** (*Optional*, **Deprecated**, string): The password to protect the API Server with. Defaults
107
-
to no password. It is recommended to use the `encryption` -> `key` above instead of the the `password`.
112
+
to no password. It is recommended to use the `encryption` -> `key` above instead of the `password`.
108
113
109
114
- **on_client_connected** (*Optional*, [Action](/automations/actions#all-actions)): An automation to perform when a client
110
115
connects to the API. See [`on_client_connected` Trigger](#api-on_client_connected_trigger).
@@ -385,25 +390,51 @@ api:
385
390
- logger.log: "API client disconnected!"
386
391
```
387
392
393
+
## Conditions
394
+
388
395
{{< anchor "api-connected_condition" >}}
389
396
390
-
## `api.connected` Condition
397
+
### `api.connected` Condition
398
+
399
+
This [Condition](/automations/actions#all-conditions) checks if at least one client is connected to the ESPHome native API.
400
+
401
+
#### Configuration variables
402
+
403
+
- **state_subscription_only** (*Optional*, boolean): If enabled, only counts clients that have subscribed to entity state updates. This filters out logger-only connections (such as `esphome logs` command), which can cause false positives when waiting for Home Assistant. Defaults to `false`.
391
404
392
-
This [Condition](/automations/actions#all-conditions) checks if at least one client is connected to the ESPHome
393
-
native API. Please note client not only includes Home Assistant, but also ESPHome's OTA log output
394
-
if logs are shown remotely.
405
+
**Check if any client is connected:**
395
406
396
407
```yaml
397
408
on_...:
398
409
if:
399
410
condition:
400
411
api.connected:
401
412
then:
402
-
- logger.log: API is connected!
413
+
- logger.log: Client is connected to API!
403
414
```
404
415
405
416
The lambda equivalent for this is `id(api_id).is_connected()`.
406
417
418
+
**Check if a client subscribed to entity states is connected (typically Home Assistant):**
419
+
420
+
```yaml
421
+
on_boot:
422
+
- wait_until:
423
+
condition:
424
+
api.connected:
425
+
state_subscription_only: true
426
+
- logger.log: Home Assistant is connected!
427
+
- homeassistant.event:
428
+
event: esphome.device_booted
429
+
```
430
+
431
+
The lambda equivalent for this is `id(api_id).is_connected(true)`.
432
+
433
+
**Use Cases:**
434
+
435
+
- Use `state_subscription_only: false` (default) to detect any API connection
436
+
- Use `state_subscription_only: true` when you need to ensure Home Assistant (or other connections that subscribe to states) is connected before sending events or calling services, preventing errors from logger-only connections
437
+
407
438
{{< anchor "api-device-actions" >}}
408
439
409
440
## User-defined Actions
@@ -412,6 +443,10 @@ It is also possible to get data from Home Assistant to ESPHome with user-defined
412
443
When you declare actions in your ESPHome YAML file, they will automatically show up in
413
444
Home Assistant and you can call them directly.
414
445
446
+
> [!NOTE]
447
+
> User-defined actions can also send responses back to the calling client using the `api.respond` action.
448
+
> See [Action Responses](#action-responses) for details.
449
+
415
450
```yaml
416
451
# Example configuration entry
417
452
api:
@@ -471,6 +506,144 @@ Each of these also exist in array form:
471
506
- bool[]: An array of boolean values. C++ type: `std::vector<bool>`
472
507
- ... - Same for other types.
473
508
509
+
### Action Responses
510
+
511
+
User-defined actions can send responses back to the calling client (such as Home Assistant). This enables
512
+
bidirectional communication where actions can report success/error status or return structured JSON data.
513
+
514
+
#### Response Modes
515
+
516
+
The response behavior is controlled by the `supports_response` option, which can be set explicitly or
517
+
auto-detected based on your action configuration:
518
+
519
+
- **none** (default): No response is sent. The action is "fire and forget".
520
+
- **status**: Reports success/error status without data. Auto-detected when `api.respond` is used without `data:`.
521
+
- **optional**: Returns JSON data when the client requests it. Auto-detected when `api.respond` is used with `data:`.
522
+
- **only**: Always returns JSON data. Must be set explicitly. Use this for query-type actions.
523
+
524
+
#### Configuration variables
525
+
526
+
- **supports_response** (*Optional*, string): The response mode for this action. One of `none`, `status`,
527
+
`optional`, or `only`. If not specified, the mode is auto-detected based on `api.respond` usage in the action.
528
+
529
+
#### Trigger variables
530
+
531
+
When `supports_response` is not `none`, the following variables are available in the action:
532
+
533
+
- **call_id** (`uint32_t`): A unique identifier for this action call. Used internally by `api.respond`.
534
+
- **return_response** (`bool`): Only available in `optional` mode. Indicates whether the client requested
535
+
a response. You don't typically need to check this - `api.respond` handles it automatically.
536
+
537
+
### `api.respond` Action
538
+
539
+
This action sends a response back to the client that called the user-defined action. It can report
540
+
success/error status and optionally include JSON data.
541
+
542
+
#### Configuration variables
543
+
544
+
- **success** (*Optional*, boolean, [templatable](/automations/templates)): Whether the action succeeded.
545
+
Defaults to `true`.
546
+
- **error_message** (*Optional*, string, [templatable](/automations/templates)): An error message to include
547
+
when `success` is `false`. Defaults to an empty string.
548
+
- **data** (*Optional*, [lambda](/automations/templates#config-lambda)): A lambda that populates a JSON object
549
+
with response data. The lambda receives a `root` variable of type [`JsonObject`](https://arduinojson.org/v7/api/jsonobject/)
0 commit comments