Bash script button not executing

Hey,
i got a project were i have to use bash script.
I found the decumentation: Shell Command - Home Assistant
and did:

  - platform: command_line
    switches:

      turn_windows_vm:
        friendly_name: Windows VM
        command_on: shell_command.start_windows_vm
        command_off: shell_command.shutdown_windows_vm
        
shell_command:
  shutdown_windows_vm: bash /config/scripts/turn_off_windows.sh
  start_windows_vm: bash /config/scripts/turn_on_windows.sh

The script file is located at:
script

I can run the script via terminal ui with:

bash /config/scripts/turn_on_windows.sh

but when i try to execute it via the button or services nothing happens.

The .sh:

#! bin/bash/expect
sshpass -p "pw" ssh -o StrictHostKeyChecking=no [email protected] 'qm start 105'
- platform: command_line
    switches:

      turn_windows_vm:
        friendly_name: Windows VM
        command_on: "bash /config/scripts/turn_on_windows.sh"
        command_off: "bash /config/scripts/turn_off_windows.sh"

I tried it like this as well. The button is always hopping back to off and the script is not working.


Calling the switch service is not working as well.

Hold the bus. Why are you using expect to run the shell script?

PS you shouldn’t be using sshpass! Use public key auth!

The script is working fine. I can run it from the terminal with “bash /config/scripts/turn_on_windows.sh”. Its one of my first bash scripts so i dont really know what im doing but i saw that i should use
#! bin/bash
instead. But why can i run the script via terminal but not via “command_line - switch”.

If you call the service via ha’s service tab in dev-tools, does it work.

No, its not working. I deleted the /expect from the bash script. It works in the Terminal but not via service call.

service: switch.turn_on
data: {}
target:
  entity_id: switch.turn_windows_vm

I got it. The problem was, I had to add apk add sshpass to the script.
I got the config like:

switch: 
  - platform: command_line
    switches:
       vm_windows:
        command_on: "bash /config/scripts/turn_on_windows.sh"
        command_off: "bash /config/scripts/turn_off_windows.sh"


  - platform: command_line
    switches:
       vm_kde:
        command_on: "bash /config/scripts/turn_off_kde.sh"
        command_off: "bash /config/scripts/turn_on_kde.sh"

The bash script is now:

#! bin/bash
apk add sshpass
sshpass -p "password" ssh -o StrictHostKeyChecking=no [email protected] 'qm start 105'

Everything works as expected now.

Hi! … I have public key auth configured and executing the bash script from the HomeAssistant-RPi’s command line works, but it doesn’t work from the switch on the HA dashboard… it just hangs. Any thoughts? :thinking: