Shell_command not working from automation but from ssh

Hi everyone,

I have a shell command, which runs fine from the ssh inside Home Assistant OS, but fails when started from an automation.

I guess there’s a problem with authentication.

There is no mention of mandatory authentication at Shell Command - Home Assistant

I want to shutdown my Windows PC or during testing just to create a directory.

My HA OS instance is a VM inside Proxmox.

This is in my configuration.yaml:

wake_on_lan:

switch:
  - platform: wake_on_lan
    name: mywindows
    host: 192.168.1.30
    mac: 12:34:56:78:90:ab
    turn_off:
      service: shell_command.hibernate_mywindows

shell_command:
  hibernate_mywindows: '/usr/bin/ssh "My Name"@192.168.1.30 shutdown /h'

I guess only the last two lines are important for my issue. WoL works perfectly and I’m not relying on turn_off.

This is my automation I’m trying to get to work:

alias: "Hibernate Windows PC"
description: ""
trigger: []
condition: []
action:
  - service: shell_command.hibernate_mywindows
    data: {}
mode: single

I replaced /usr/bin/ssh with ssh and I also replaced shutdown /h with mkdir test but they don’t work from the automation. Altough both work when I run them from the HA shell:

(mywindows PC) C:\> ssh homeassistant
[...]
➜  ~ ssh "My Name"@192.168.1.30 mkdir test

The folder test is successfully created (yes, mkdir works in PowerShell). I’m not prompted for a password. Also the PC goes into Hibernate with the other command.

Running the command verbousely tells me the key used is /root/.ssh/id_rsa.

Running the automation manually results with these Step Details:

Result:
params:
  domain: shell_command
  service: hibernate_mywindows
  service_data: {}
  target: {}
running_script: false

The last line is misleading. When I just create an automation to send a notification to my phone it works, though with the same line running_script: false.

When I create a notification that

  • sends a notification to my phone
  • runs the shell command

only the notification will be sent, no matter the order.

I read about ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i identityfile in another thread but my issue isn’t affected by reboots.

Can anybody help? I don’t know where else to look.

A shell_command is executed inside the HA container.
Read here how to open a console in the HA Container:

TL;DR:
I managed to successfully run an ssh command from inside the homeassistant container but the automation still doesn’t work.

… contrary to my way of running it in SSH? I see.

So back to SSH I run docker and I get the message how to disable protection mode. One restart later the command docker container ls reveals 12 running containers from which homeassistant seems to be the HA instance.

After successfully entering the container via docker container exec -it homeassistant I copy the id_rsa file from the OS host into the container. One chmod correction later the connection is established and the test directory created on the host.

Well, it doesn’t work from the automation yet. Of course, I restarted with every change, but it’s hard to tell if a quick restart would suffice, so I did a restart of Home Assistant as well.

Now I notice the quotes in my shell_command are unnecessary. I tried
hibernate_mywindows: ssh "My Name"@192.168.1.30 mkdir test123 and
hibernate_mywindows: mkdir test123

but nothing works from an automation. Notice the last command is executed inside the container, however I can’t find / -iname 'test123' in the container.

Is the syntax even correct?

I’m wondering why /var/log/ is empty. There must be a way of troubleshooting HA myself, right?

Any help will be highly appreciated.

This doesn’t directly answer your question about the shell script, but - if all you’re after is hibernate/reboot/shutdown, checkout the companion app for your pc. I use it, and it works great!

This how i restart a service on a remote rpi

shell_command:
  restart_mcp2ha_gw: "ssh [email protected] '/root/hagw/mcp2mqtt/init.sh restart'"