SSH works from the terminal but not via Home Assistant button/script

Hello everyone,

I’m having an issue running a Python script on a remote Raspberry Pi via SSH from Home Assistant.

Context

  • I have a Lovelace “SOS” button that should trigger a Python script (sos.py) on a Raspberry Pi via SSH.
  • SSH connection works perfectly when executed from the Home Assistant terminal.
  • But when I click the button, the script does not run and no log appears on the Raspberry Pi.

My configuration

configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

frontend:
  themes: !include_dir_merge_named themes

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

shell_command:
  sos_script: '/usr/bin/ssh -i /config/.ssh/id_ed25519 [email protected] "python /home/admin/script/sos.py"'

scripts.yaml:

run_sos:
  alias: "Run SOS Script"
  sequence:
    - service: shell_command.sos_script

Lovelace button:

type: button
tap_action:
  action: perform-action
  perform_action: script.turn_on
  target:
    entity_id: script.run_sos
show_name: true
show_icon: true
name: SOS
icon: mdi:alert

What I observe

  • From the Home Assistant Terminal, the command works:
ssh -i /config/.ssh/id_ed25519 [email protected] "python /home/admin/script/sos.py"
  • But from the button, the script does not start, and no output appears in sos.log on the Raspberry Pi.
  • SSH logs on the Raspberry (sudo journalctl -u ssh.service -f):
jan 24 18:57:09 raspberrypi sshd-session[1775]: Connection closed by 192.168.1.240 port 46946 [preauth]
jan 24 18:57:39 raspberrypi sshd-session[1788]: Connection closed by 192.168.1.240 port 36780 [preauth]
  • There is no /var/log/auth.log on this Raspberry.

Thank you very much for your help!

Welcome to the forums.

It’s generally a good idea to search the forums before creating a new thread, since odds are your question has been asked (and answered) before.

Have a read through this thread in the Community Guides section of the forums. If the first post doesn’t address how to fix your issue, there’s a good chance one of the replies below it will.