How to SSH into Mycroft from Home Assistant

I would like to control my Mycroft (www.mycroft.ai) smart speaker from Home Assistant. For instance I would like to have a switch in Home Assistant to turn on and off the radio.

I tried to do so using a shell command from Home Assistant, but somewhere I make an error with the SSH authorization. This is what I did:

  • I have two Raspberry Pi’s: one with Home Assistant running on Hassio, the other one with Picroft.
  • I generated a SSH private and public key using shell commands in Home Assistant. After this I have two new files in my config folder, namely ‘my.key.pub’ and ‘my.key’*.
  • I copied the content of ‘my.key.pub’ from Home Assistant into Mycroft, assuming that I would be able to SSH from Home Assistant into Mycroft with shell commands.
  • I created this shell command into Home Assistant: ‘ssh xx@192… /home/pi/mycroft-core/bin/mycroft-say-to stop’ to stop the current task in Mycroft.
  • If I try to use this command I get the following error: Error running command: ssh xx@192... /home/pi/mycroft-core/bin/mycroft-say-to stop, return code: 255’, so I guess there is an authentication error or so.

If I do the same from my Linux-computer everything works. What do I do wrong? Should I perhaps define my keys in the configuration.yaml file or so? If so how? Thanks a lot in advance.

*I used this topic as inspiration: https://www.reddit.com/r/homeassistant/comments/6yldgt/how_to_add_ssh_key_for_hassio/

My Setup: Raspbian Buster and I have my home assistant running in it’s own environment. I am sorry if I use any terms incorrectly, I am new to Linux and don’t understand everything. Just wanted to share what I did to get mine to work.

Step one: Setting up SSH Key (I did this both on the homeassistant pi and in the environment)

ssh-keygen -t rsa

Then I left the RSA ID blank and pressed enter, left password blank and pressed enter, left confirm password blank and pressed enter.

I then copied the key to the machine that I watned to run the command on. Pi below is the username of the other machine.

ssh-copy-id [email protected]

Verify that the ssh connection works without prompt for password. You should now have an ssh connection to the other pc

ssh [email protected]

type exit to exit the ssh connection.

Now I do the same step above but under the home assistant user / environement

sudo -u homeassistant -H -s
ssh-keygen -t rsa

Enter, Enter, Enter past the ID name, Password, confirm password

Now copy the key over to the other machine you want to issue commands on. I also copied the key over to the PI where my home assistant is running. Not sure if I needed to but since it is running in an evironment I did any ways. The 10.0.0.128 is where homeassistant is installed and running.

ssh-copy-id [email protected]
ssh-copy-id [email protected]
exit

Now you should be signed in as the pi user and not the home assistant user

SSH Key’s are finished.

Add line to Configuration.yaml /home/homeassistant/.homeassistant

cd /home/homeassistant/.homeassistant
sudo nano configuration.yaml

Then add in the following line. I added it in at the bottom.

shell_command: !include shell_command.yaml

Save the Config

Now we need to create the shell_command.yaml file

sudo nano shell_command.yaml

I added the follow in the shell_command file

reboot_test: sudo -u homeassistant -H -s ssh [email protected] sudo reboot

Now we need to create the HA script that issues the command

sudo nano script.yaml

I added in the following

reboot_test:
 alias: Reboot Test
 sequence:
  - Service: shell_command.reboot_test

Tested and it now works.

I think the key is to run the ssh command as the homeassistant user or in the homeassistant environment. Not sure what term is correct.

1 Like