Getting response_variable to work with shell command

I tried to get a shell commands return value in an automation based on the example in Shell Command - Home Assistant.

However when I try to execute the automation I get the following error:

Script does not support ‘response_variable’ for service ‘response_variable’ which does not support response data… Got {‘type’: ‘execute_script’, ‘sequence’: [{‘service’: ‘shell_command.test_return_code’, ‘response_variable’: ‘todo_response’}], ‘id’: 45}

Does anyone have a working configuration where they get the result of the shell command? If so please share it with the class.

test.sh:

#!/bin/bash
echo "Please work!"
echo 1 > /config/entities/shell/test.txt
exit 0

configuration.yaml:

shell_command: !include_dir_merge_named entities/shell/yamls

test.yaml:

test_return_code: "bash /config/entities/shell/test.sh"

automation:

alias: Shell Result Test
description: ""
trigger: []
condition: []
action:
  - service: shell_command.test_return_code
    response_variable: return_response
mode: single

When removing response_variable from the service call the service is executed because test.txt is created.

  - service: shell_command.test_return_code

I’m running Home Assistant 2023.7.2.

Work for me, same script:

- alias: Shell Result Test
  id: 7c29e2fc-b1c4-476f-8a3a-ef437756d911
  description: ""
  trigger: []
  condition: []
  action:
    - service: shell_command.test_return_code
      response_variable: return_response
    - if: "{{ return_response['returncode'] == 0 }}"
      then:
        - service: notify.smtp
          data:
            title: "Return"
            message: "{{ return_response['stdout'] }}"
      else:
        - service: notify.smtp
          data:
            title: "ToDo file error"
            message: "{{ return_response['stderr'] }}"
  mode: single

email:

2 Likes

What version of HA are you running? Also what type of installation? I’m running it in docker, wonder if that could be the issue.

Running docker as well. That was on 2023.8.1.

I updated to 2023.8.1 and it is indeed working now.
Thank you good sir.

Hello @koying , maybe you can help me with my problem too. I use this integration for SSH commands on my servers: GitHub - zhbjsh/homeassistant-ssh: Control and monitor devices in Home Assistant by executing commands via SSH. This is the only one I have found that allows me to set a timeout to 0 and thus get the remote terminal output.

I have tried to recreate it somehow using your example, but I fail to get “stdout”, “stderr” or “exit: 0” into a template.
I can’t get to grips with “extracting” the response variables. This is beyond my horizon.

“hifi_update” is the name of my response variable and this is the output:

results:
  - device_id: 883d93ecc43bcf23e2108bf8ce3ab991
    device_name: HiFi-System
    success: true
    command: sudo apt update && sudo apt upgrade
    stdout:
      - OK:1 http://packages.azlux.fr/debian bullseye InRelease
      - OK:2 http://raspbian.raspberrypi.org/raspbian bullseye InRelease
      - OK:3 http://archive.raspberrypi.org/debian bullseye InRelease
      - OK:4 https://dtcooper.github.io/raspotify raspotify InRelease
      - Paketlisten werden gelesen…
      - Abhängigkeitsbaum wird aufgebaut…
      - Statusinformationen werden eingelesen…
      - Alle Pakete sind aktuell.
      - Paketlisten werden gelesen…
      - Abhängigkeitsbaum wird aufgebaut…
      - Statusinformationen werden eingelesen…
      - Paketaktualisierung (Upgrade) wird berechnet…
      - >-
        0 aktualisiert, 0 neu installiert, 0 zu entfernen und 0 nicht
        aktualisiert.
    stderr:
      - ""
      - >-
        WARNING: apt does not have a stable CLI interface. Use with caution in
        scripts.
      - ""
      - ""
      - >-
        WARNING: apt does not have a stable CLI interface. Use with caution in
        scripts.
      - ""
    code: 0

All I want is to use “code: 0” for a template but I don’t know how. The “templating” is unfortunately the worst documentation available in Home Assistant. I’ve been working on it for months… how do you extract this data?