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
+31-5Lines changed: 31 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -385,25 +385,51 @@ api:
385
385
- logger.log: "API client disconnected!"
386
386
```
387
387
388
+
## Conditions
389
+
388
390
{{< anchor "api-connected_condition" >}}
389
391
390
-
## `api.connected` Condition
392
+
### `api.connected` Condition
393
+
394
+
This [Condition](/automations/actions#all-conditions) checks if at least one client is connected to the ESPHome native API.
395
+
396
+
#### Configuration variables
391
397
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.
398
+
- **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`.
399
+
400
+
**Check if any client is connected:**
395
401
396
402
```yaml
397
403
on_...:
398
404
if:
399
405
condition:
400
406
api.connected:
401
407
then:
402
-
- logger.log: API is connected!
408
+
- logger.log: Client is connected to API!
403
409
```
404
410
405
411
The lambda equivalent for this is `id(api_id).is_connected()`.
406
412
413
+
**Check if a client subscribed to entity states is connected (typically Home Assistant):**
414
+
415
+
```yaml
416
+
on_boot:
417
+
- wait_until:
418
+
condition:
419
+
api.connected:
420
+
state_subscription_only: true
421
+
- logger.log: Home Assistant is connected!
422
+
- homeassistant.event:
423
+
event: esphome.device_booted
424
+
```
425
+
426
+
The lambda equivalent for this is `id(api_id).is_connected(true)`.
427
+
428
+
**Use Cases:**
429
+
430
+
- Use `state_subscription_only: false` (default) to detect any API connection
431
+
- 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
0 commit comments