Unable to call shell script in automation

I have a light that I can query the on/off state and also send commands via TCP. What I’d like to do is when a button is pressed, that command is sent and the light state changes.

I’ve tried a few approaches all of which have failed. I’ve tried to setup a service using the TCP setup in configuration.yaml but this only exposes the service as a state. I then tried to setup a ‘switch’ using TCP, but still the switch wasn’t shown in the action category of the automation.

Then I tried to create a shell_command alias and use that in the automation action, but get an error that ‘shell_command’ isn’t allowed.

Does anyone have any tips on the best way I can setup Home Assistant so that it can call a shell script whenever a button is pressed? Links to code or demo code would be super helpful.

This is what I’ve tried:

Command Line Switch: Command Line - Home Assistant
TCP Sensor: TCP - Home Assistant

Added this to configuration.yaml:

switch:
  - platform: command_line
    switches:
      office_lamp_light:
        command_on: 'xxx'
        command_off: 'xxx'

shell_command:
  office_light_lamp_on: 'xxx'

The error is: " The automation “Light on in Office” (automation.light_on_in_office) has an action that calls an unknown service: shell_command.office_light_lamp_on."

None of the above as exposed in the automation UI

automation yaml:

alias: Light on in Office
description: test
trigger:
  - device_id: xxx
    domain: hue
    platform: device
    type: initial_press
    subtype: 1
    unique_id: xxx
condition: []
action:
  - service: shell_command.office_light_lamp_on
mode: single

Any tips would be much appreciated!

Did you restart home assistant after creating your shell command?

1 Like

I’m far from an expert on this topic, but I do have several working shell scripts in my setup, and from what I know, that doesn’t appear to be the correct use of shell_command (unless the part you’ve omitted with ‘xxx’ actually correctly identifies your script - which makes helping you more challenging by not providing all the info… ?)

From my configuration.yaml:

shell_command:
    yamaha_tuner: /bin/bash /config/scripts/yamahatuner.sh tune {{ ip }} {{ station }}

Then the code attached to a button:

tap_action:
                  action: call-service
                  service: script.change_station_master

The change_station_master script in HA:

service: shell_command.yamaha_tuner
data:
  ip: x.x.x.x
  station: "{{ states('input_text.masterradio') }}"

And, not that it’s particularly relevant in this example, but the shell script, just in case anyone is interested:

#!/bin/sh
curl_cmd="curl --silent --output /dev/null"

tune() {
    foo=${2}
    ent="${foo//./}"
    stat=$((${ent} * 100))
    ${curl_cmd} http://${1}/YamahaExtendedControl/v1/tuner/setFreq?band=fm\&tuning=direct\&num=${stat}

}

"$@"

This script takes the value from a text-input-row and passes it to that HA script to change the station on a Yamaha AV receiver.

There might be a more efficient way of accomplishing this, but it’s what I was able to come up with to make it work.

HTH,
exx

For me I had to restart HomeAssistant. After that the commands also show up in the Automations UI

Is there way to do this without a restart?

Edit: Apparently no, since this change does touch services: Configuration.yaml - Home Assistant

Sorry for not replying earlier, I didn’t get any notifications, I ended up using a plugin instead, but these comments are helpful