shell_command response documentation (#28226)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
RoboMagus 2023-07-23 14:33:04 +02:00 committed by GitHub
parent 77e84f5e6e
commit 3f7a6c306b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -54,6 +54,10 @@ If you are using [Home Assistant Operating System](https://github.com/home-assis
A `0` exit code means the commands completed successfully without error. In case a command results in a non `0` exit code or is terminated after a timeout of 60 seconds, the result is logged to Home Assistant log. A `0` exit code means the commands completed successfully without error. In case a command results in a non `0` exit code or is terminated after a timeout of 60 seconds, the result is logged to Home Assistant log.
## Response
Shell commands provide a service response in a dictionary containing `stdout`, `stderr`, and `returncode`. These can be used in automations to act upon the command results using [`response_variable`](/docs/scripts/service-calls#use-templates-to-handle-response-data).
## Examples ## Examples
### Defining multiple shell commands ### Defining multiple shell commands
@ -99,3 +103,37 @@ shell_command:
``` ```
{% endraw %} {% endraw %}
The following example shows how the shell command response may be used in automations.
{% raw %}
```yaml
# Create a ToDo notification based on file contents
automation:
- alias: "run_set_ac"
trigger:
- ...
action:
- service: shell_command.get_file_contents
data:
filename: "todo.txt"
response_variable: todo_response
- if: "{{ todo_response['returncode'] == 0 }}'
then:
- service: notify.mobile_app_iphone
data:
title: "ToDo"
message: "{{ todo_response['stdout'] }}"
else:
- service: notify.mobile_app_iphone
data:
title: "ToDo file error"
message: "{{ todo_response['stderr'] }}"
shell_command:
get_file_contents: "cat {{ filename }}"
```
{% endraw %}