Skip to content

Commit da18bc5

Browse files
jesserockzswoboda1337
authored andcommitted
[api] Docs for action responses (#5435)
1 parent c7bca5c commit da18bc5

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

content/components/api.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ api:
115115
Before using any of the actions below, you'll need to tell Home Assistant to allow your device to
116116
perform actions.
117117

118+
> [!NOTE]
119+
> Starting with ESPHome 2025.10.0, you can configure actions to receive and process responses from
120+
> Home Assistant using `capture_response`, `on_success`, and `on_error`. See [Action Response Handling](#action-response-handling) for details.
121+
118122
Open the ESPHome integration page on your Home Assistant instance:
119123

120124
[![Open your Home Assistant instance and show the ESPHome integration.](https://my.home-assistant.io/badges/integration.svg)](https://my.home-assistant.io/redirect/integration/?domain=esphome)
@@ -202,6 +206,20 @@ on_...:
202206
- **variables** (*Optional*, mapping): Optional variables that can be used in the `data_template`.
203207
Values are [lambdas](#config-lambda) and will be evaluated before sending the request.
204208

209+
- **capture_response** (*Optional*, boolean): Enable capturing the response from the Home Assistant action call.
210+
When enabled, `on_success` must be configured. Defaults to `false`.
211+
212+
- **response_template** (*Optional*, [templatable](#config-templatable), string): Optional Jinja template to process
213+
the action response data. This template is evaluated on the Home Assistant side with Home Assistant's templating engine.
214+
Requires `capture_response: true`.
215+
216+
- **on_success** (*Optional*, [Automation](#automation)): Optional automation to execute when the Home Assistant action
217+
call succeeds. When `capture_response: true`, the response data is available as a `response` variable of type `JsonObjectConst`.
218+
See [Action Response Handling](#action-response-handling).
219+
220+
- **on_error** (*Optional*, [Automation](#automation)): Optional automation to execute when the Home Assistant action
221+
call fails. See [Action Response Handling](#action-response-handling).
222+
205223
Data structures are not possible, but you can create a script in Home Assistant and call with all
206224
the parameters in plain format.
207225

@@ -235,6 +253,74 @@ on_...:
235253
blue: '71'
236254
```
237255

256+
#### Action Response Handling
257+
258+
> [!NOTE]
259+
> Action response handling is available in ESPHome 2025.10.0 and later.
260+
261+
You can configure actions to receive and process responses from Home Assistant. This enables bidirectional
262+
communication where ESPHome can not only call Home Assistant actions but also handle their responses.
263+
264+
##### Basic Success/Error Handling
265+
266+
Use `on_success` and `on_error` to respond to action completion:
267+
268+
```yaml
269+
on_...:
270+
- homeassistant.action:
271+
action: light.toggle
272+
data:
273+
entity_id: light.demo_light
274+
on_success:
275+
- logger.log: "Toggled demo light"
276+
on_error:
277+
- logger.log: "Failed to toggle demo light"
278+
```
279+
280+
##### Capturing Response Data
281+
282+
To capture and process response data from actions, set `capture_response: true`. When enabled, `on_success` must be configured
283+
and the response data is available as a [`JsonObjectConst`](https://arduinojson.org/v7/api/jsonobjectconst/) variable named `response`.
284+
285+
```yaml
286+
# Example: Get weather forecast and parse JSON response
287+
on_...:
288+
- homeassistant.action:
289+
action: weather.get_forecasts
290+
data:
291+
entity_id: weather.forecast_home
292+
type: hourly
293+
capture_response: true
294+
on_success:
295+
- lambda: |-
296+
JsonObjectConst next_hour = response["response"]["weather.forecast_home"]["forecast"][0];
297+
float next_temperature = next_hour["temperature"].as<float>();
298+
ESP_LOGI("weather", "Temperature next hour: %.1f", next_temperature);
299+
```
300+
301+
##### Using Response Templates
302+
303+
Use `response_template` to extract and format data from complex responses using Home Assistant's Jinja templating engine.
304+
This requires `capture_response: true`.
305+
306+
```yaml
307+
# Example: Extract temperature using a template
308+
on_...:
309+
- homeassistant.action:
310+
action: weather.get_forecasts
311+
data:
312+
entity_id: weather.forecast_home
313+
type: hourly
314+
capture_response: true
315+
response_template: "{{ response['weather.forecast_home']['forecast'][0]['temperature'] }}"
316+
on_success:
317+
- lambda: |-
318+
float temperature = response["response"].as<float>();
319+
ESP_LOGI("weather", "Temperature next hour: %.1f", temperature);
320+
```
321+
322+
When `response_template` is used, the processed result is available in `response["response"]`.
323+
238324
{{< anchor "api-homeassistant_tag_scanned_action" >}}
239325

240326
### `homeassistant.tag_scanned` Action

0 commit comments

Comments
 (0)