CLI switch with keyfile won't work in Home Assistant (but will work when ran from command line)

Hi everybody,

I am currently working on an alarm via Home Assistant. I cannot trigger the command I need to on my remote raspberry pi zero w from Home Assistant; however, the command does work fine when I run it from my computer in the terminal.

What I did

  • create a ssh key without password authentication that will allow me to log into that raspberry pi without having to enter any credentials
  • prepared the pi for what it needs to do (irrelevant for now, see below)
  • run the command from my command line (works)
  • copied the pubkey to /config/ssh/mykey
  • created a switch to trigger the command
  • try switch in Home Assistant (docker on local pc)
  • try switch in Home Assistant (docker on home server)¹

¹ usually things work better on the home server than the local pc. For instance, I cannot connect Plex from my local computer with Home Assistant. When I tried the exact same Plex configuration on my home server, it was able to connect just fine. Might have to do with the IP range my local docker creates. I don’t know. But now that this won’t work on either machines I don’t know what else I could change.

The command itself is ssh -i /config/ssh/mykey pi@<ip> 'aplay -D bluealsa /home/pi/grieg_morgenstimmung.wav'. When I run this from my main computer, the pi will play that wav file on the connected bluetooth speaker.

So I assumed that this switch would work as well => /config/switch/pi_wecker.yaml

- platform: command_line
      command_on: ssh -i /config/ssh/mykey pi@ip aplay -D bluealsa /home/pi/grieg_morgenstimmung.wav
      command_off: ssh -i /config/ssh/mykey pi@ip pkill aplay

I have also tried putting the command in double quotes and putting the remote command in single quotes (looking like this command_on: "ssh -i /config/ssh/mykey pi@ip 'aplay -D bluealsa /home/pi/grieg_morgenstimmung.wav').

I would understand not being able to do this without the keyfile, because Home Assistant would not be able to connect to the pi. But the key is there, this should work. Would somebody please help me fix this?

Thanks in advance for your help :slight_smile: