Can't get shell command to run in automation

I can’t get the shell_command service to fire in my automations. What am I doing wrong?

I’m following this YouTube video on how to automate backing up my config files to a private github repo, but the video was made before HA removed the ability of the hassio.addon_stdio to run ssh scripts. No biggie; shell_command should ostensibly be able to do this.

Here’s what I’ve done so far:

  1. I’ve created a shell script to back up my stuff to a github repo. The script works perfectly when running it manually from the command line (via the Terminal add-on)
  2. In my configuration.yaml file I’ve added:
# expose shell commands:
shell_command:
  ha_gitpush: "/config/ha_gitpush.sh"
  1. In my automation yaml file:
alias: HA Push to Github
description: >-
  Push all configuration files (as specified in /config/.gitignore) to the
  Github private repo.
trigger:
  - platform: time
    at: "04:00:00"
  - platform: homeassistant
    event: start
condition: []
action:
  - service: shell_command.ha_gitpush
    data: {}
mode: single

Note that HA insists on adding the data: {} bit. I didn’t add this to the yaml file when I was editing it, but HA adds it automatically upon save.

Finally, when I run the automation, the trace comes back with running_script: false

params:
  domain: shell_command
  service: ha_gitpush
  service_data: {}
  target: {}
running_script: false

I’ve reloaded my config files after making changes; I’ve even completely rebooted HA. No dice.

What if you changed it to a command line entity?

command_line:
  - switch:        
      name: ha_gitpush
      command_on: "ssh -i /config/ha_gitpush.sh"

Try changing your config to this:

shell_command:
  ha_gitpush: /bin/bash /config/ha_gitpush.sh

That’s what’s in my config file (including the lack of quotes), and it works like a charm. I run my scripts the same way you do with the service.

Hmm, I’ve tried this suggestion with both quotes and no quotes, and still no dice.

Any errors in the log?

Are you sure the git utilities are installed in the HA docker image? The terminal is a separate docker image and may have different things installed.

Ssh into the box, then use docker ps to find the HA container, docker exec -it into the Ha container then try running the command.

1 Like

Could you make it work? I’m getting a similar result, a shell command runs correctly when I do it from the developer tools, but not when it’s on an automation.

Me too. It says the path is incorrect, but I’ve tried all the variations I can think of. Calling the script itself works fine, but the Shell Command service from automations results in an error that the file doesn’t exist.

Fun fact: I’m trying to do the same today to backup my esphome. :smiley:
I added everything like in the tutorials and it wasn’t working. After restart HA it works to execute the script, but username can’t be found.
configuration.yaml:

shell_command:
  ha_gitpush: /bin/bash /config/ha_gitpush.sh

ha_gitpush.sh:

cd /config/esphome
MESSAGE_TEXT="Code on "
COMMIT_CURRENT_DATE=$(date +'%d-%m-%Y %H:%M:%S')
COMMIT_MESSAGE="[$MESSAGE_TEXT]: $COMMIT_CURRENT_DATE"
echo "$COMMIT_MESSAGE"
git add .
git commit -m "$COMMIT_MESSAGE"
git push -u origin master

automations.yaml:

alias: push ESPHOME configuration to GitHub repo
description: Backup ESPHome configuration folder
trigger:
  - platform: time
    at: "02:00:00"
condition: []
action:
  - service: shell_command.ha_gitpush
    data: {}
    response_variable: backup_response
  - if:
      - condition: template
        value_template: "{{ backup_response['returncode'] == 0 }}"
    then:
      - service: notify.notify
        data:
          title: ✅ Backup successful
          message: "{{ backup_response['stdout'] }}"
    else:
      - service: notify.notify
        data:
          title: ❌ Backup failed
          message: "{{ backup_response['stderr'] }}"
mode: single

When calling script in dev tools service: "shell_command.ha_gitpush"

  [Code on ]: 25-02-2024 15:32:08
  On branch master
  Your branch is ahead of 'origin/master' by 1 commit.
    (use "git push" to publish your local commits)

  nothing to commit, working tree clean
stderr: >-
  fatal: could not read Username for 'https://github.com': No such device or
  address
returncode: 128

I followed the instructions on Daily backup Home Assistant configuration into a git repository · GitHub , besides I didn’t init my git on /homeassistant/ but /homeassistant/esphome/. Also when running script in Advanced SSH & Web Terminal it worked. So I had to do 2 more things:

docker exec -it homeassistant bash
cd esphome
./ha_gitpush.sh
# --> Enter username and tokken for github
# --> Wait till commit finished and verify on github

Then:

git config credential.helper store

After that, change something in your HA folder and run
service: "shell_command.ha_gitpush"
again. This resolved in:

stdout: |-
  [Code on ]: 25-02-2024 15:44:00
  [master e5fdcfb] [Code on ]: 25-02-2024 15:44:00
   1 file changed, 1 insertion(+), 1 deletion(-)
  branch 'master' set up to track 'origin/master'.
stderr: |-
  To https://github.com/***************/home-assistent.git
     ec739e7..e5fdcfb  master -> master
returncode: 0

Just an addition, which is pretty nice I think:
I changed the ha_gitpush.sh to:

# Change this to your folder if it is different and you don't want to backup just esphome configs.
cd /config/esphome

# Add all files to the repository with respect to .gitignore rules
git add .

# Commit changes with message prepared in file:
git commit --file .commit_message.txt

# Push changes towards GitHub
git push -u origin master

in same folder (for me /config/esphome) I have a file .commit_message.txt and if I change something in my config, I just write down what I’ve changed and why. So I don’ have every commit with “files at dd-mm-yyyy”, I have the message

changed i2c Pin mapping in file esphome_base.yaml