Skip to content

Commit 6acef43

Browse files
AzonIncbdraco
andauthored
[api] Connected Condition - state_subscription_only flag (#5624)
Co-authored-by: J. Nick Koston <nick+github@koston.org>
1 parent f105481 commit 6acef43

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

content/components/api.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,25 +385,51 @@ api:
385385
- logger.log: "API client disconnected!"
386386
```
387387

388+
## Conditions
389+
388390
{{< anchor "api-connected_condition" >}}
389391

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
391397

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:**
395401

396402
```yaml
397403
on_...:
398404
if:
399405
condition:
400406
api.connected:
401407
then:
402-
- logger.log: API is connected!
408+
- logger.log: Client is connected to API!
403409
```
404410

405411
The lambda equivalent for this is `id(api_id).is_connected()`.
406412

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
432+
407433
{{< anchor "api-device-actions" >}}
408434

409435
## User-defined Actions

0 commit comments

Comments
 (0)