Ssh to remotely start/stop service

i have 2 raspberry pi devices and I enabled ssh access to the remote box using keys so I don’t have to enter in a password. On my HA, i create a shell script to start a service on the remote machine and another script to stop the service. When I call start script from HA, it correctly starts the service. However, when i call the script to stop the service, it does not stop the service on the remote box. When I run the same commands ssh pi@IPADDRESS sudo service lspi start and ssh pi@IPADDRESS sudo service lspi stop from putty they work.

start_lightshowpi.yaml

alias: Startup Lightshowpi
sequence:
- event: LOGBOOK_ENTRY
  event_data:
    name: EVENT
    message: "Starting Lightshowpi"
- service: notify.pushbullet
  data:
    message: "Starting Lightshowpi at {{ states('sensor.date__time') }}"
- alias: Start Lightshowpi
  service: shell_command.start_lightshowpi

stop_lightshowpi.yaml
> alias: Stop Lightshowpi
> sequence:
> - event: LOGBOOK_ENTRY
> event_data:
> name: EVENT
> message: “Stopping Lightshowpi”
> - service: notify.pushbullet
> data:
> message: “Stopping Lightshowpi at {{ states(‘sensor.date__time’) }}”
> - alias: Stop Lightshowpi
> service: shell_command.stop_lightshowpi

shell_commands.yaml

start_lightshowpi: ssh pi@IPADDRESS sudo service lspi start
stop_lightshowpi: ssh pi@IPADDRESS sudo service lspi stop

Ok I got rid of the shell_commands and just created a switch… it works with the switch, but i still don’t know why it doesn’t work with the shell_commands

here is the simple code:

- platform: command_line
  switches:
    lightshow_pi:
      command_on: "ssh pi@IPADDRESS sudo service lspi start"
      command_off: "ssh pi@IPADDRESS sudo service lspi stop"
      value_template: '{{ states.switch.lightshow_pi.state }}'
      friendly_name: Lightshow pi

When I try this it does not work.

Do you know what I could be doing wrong?

switch:

  • platform: command_line
    switches:
    magicmirror:
    friendly_name: Magic Mirror
    command_on: “ssh [email protected] sudo tvservice -p”
    command_off: “ssh [email protected] sudo tvservice -o”

Here is how I did it in HassOS

  • Install SSH & Web Terminal
  • Login into your HassOS via SSH
  • Once in login into your HomeAssistant container:
    # docker exec -it homeassistant /bin/bash
  • Generate a key pair:
    # ssh-keygen -t ed25519 -C "aname.com"
  • Do a first SSH into your remote system:
    # ssh pi@myremote
  • Copy your id_ed25519.pub into your .ssh/authorized_keys in your remote

Once you have done, you have to remember that in SSH you have to use the full path for it to work, so you configuration would be:

switch:
  - platform: command_line
    switches:
      magicmirror:
        friendly_name: Magic Mirror
        command_on: “ssh [email protected] 'sudo /usr/bin/tvservice -p'”
        command_off: “ssh [email protected] 'sudo /usr/bin/tvservice -o'”

Hey how do you do that?

This should help
https://help.ubuntu.com/community/SSH/OpenSSH/Keys

1 Like