mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-23 09:17:06 +00:00
shell_command response documentation (#28226)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
77e84f5e6e
commit
3f7a6c306b
@ -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.
|
||||
|
||||
## 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
|
||||
|
||||
### Defining multiple shell commands
|
||||
@ -99,3 +103,37 @@ shell_command:
|
||||
```
|
||||
|
||||
{% 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 %}
|
||||
|
Loading…
x
Reference in New Issue
Block a user