Add documentation for scripts and service calls with response data (#27863)

Co-authored-by: c0ffeeca7 <38767475+c0ffeeca7@users.noreply.github.com>
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Allen Porter 2023-06-27 04:12:56 -07:00 committed by GitHub
parent 432b5bdd28
commit 7ebf94ad8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -125,6 +125,57 @@ data: >
{% endraw %}
### Use templates to handle response data
Some services may respond with data that can be used in automation. This data is called _service response data_. Service response data
is typically used for data that is dynamic or large and which may not be suited for use in entity state.
Examples of service response data are upcoming calendar events for the next week or detailed driving directions.
Templates can also be used for handling response data. The service call can specify
a `response_variable`. This is the [variable](/docs/scripts/#variables)
that contains the response data. You can define any name for your `response_variable`. This example calls a service and stores the response in
the variable called `agenda`.
{% raw %}
```yaml
service: calendar.list_events
target:
entity_id: calendar.school
data:
duration:
hours: 24
response_variable: agenda
```
{% endraw %}
You may then use the response data in the variable `agenda` in another action
in the same script. The example below sends a notification using the response
data.
<div class='note'>
Which data fields can be used in a service call depends on the type of notification service that is used.
</div>
{% raw %}
```yaml
service: notify.gmail_com
data:
target: "gduser1@workspacesamples.dev"
title: "Daily agenda for {{ now().date() }}"
message: >-
Your agenda for today:
<p>
{% for event in agenda.events %}
{{ event.start}}: {{ event.summary }}<br>
{% endfor %}
</p>
```
{% endraw %}
### `homeassistant` services
There are four `homeassistant` services that aren't tied to any single domain, these are: