Guide: How to run full blown linux programs on any computer using mqtt

Hello. For some time, I was trying to find a way how to run complex python3 program by using shell command integration, but I was not successful. Partly because of setting remote ssh access, which has been pain and after reading tens of messages I could not get it working. You have to have mqtt working, you can have local mosquitto server installed by Home Assistant supervisor, for example.

Instead, I use very neat way of sending mqtt topic that triggers linux script, and then capturing the topic by whatever pc that can suscribe to mqtt broker, even somewhere else on the internet!

Start by scripting process of publishing topic in your automation script:

service: mqtt.publish
  topic: shell
  payload: script_echo

Then you will capture the topic on target computer that runs linux. Install there mosquitto and mosquitto-clients and get it working. Then use EXPECT to capture the topic and run script. create and give it run authority with sudo chmod +x


spawn /usr/bin/mosquitto_sub -v -h <broker_IP> -t shell -u <username> -P <password>

while 1 {

          expect {

                    "script_echo" {
                                exec sh -c "echo This was called by HA."

I had some troubles that expect script was killed by error (I was calling some complex scripts and if these throw an error then expect script is halted), so I have put expect script into endles loop and I do not call ./, but ./ The line with telegram sends me message to mobile that was killed and restarted:

while true
  ./ | ts
  curl " quit, restarting."
  sleep 5
1 Like

Interesting proof of concept, but I’d strongly suggest to fix your SSH issues instead :wink:

Hello. By debugging I found that shell_command is ran in config folder with user root. To remote computer, I copied key with ssh-copy-id and I also set up sudo without password on remote computer. Everything works fine from HA terminal without user intervention, yet it does not run as shell command:

ssh -i .ssh/id_rsa -o 'UserKnownHostsFile=.ssh/known_hosts' -o 'StrictHostKeyChecking=no' <remote_user>@<remote_IP> -t "sudo pkill; /home/<user>/ 300"

Error code 255 is still the same:

2021-11-21 18:01:32 ERROR (MainThread) [homeassistant.components.shell_command] Error running command: `ssh -i .ssh/id_rsa -o 'UserKnownHostsFile=.ssh/known_hosts' -o 'StrictHostKeyChecking=no' [email protected] -t "sudo pkill; /home/janbenes/ 300"`, return code: 255
NoneType: None

Are you running a Core installation?
If not, the shell_command is ran inside the docker container, so, unless you actually copied the keys to /root/.ssh in the Home Assistant container, (the SSH addon is a different one) your key and host files should come from somewhere under “/config”

I run HA as virtual disk with no access to the OS inside it. I can only open terminal and it shows “some” home directory called core-ssh, which contains folders .ssh and config. I can access these but I cannot go any higher that core-ssh even with cd \. There is no \root either or is not accessible.

I don’t understand what you say, but whatever.

Move .ssh/id_rsa to /config/.ssh/id_rsa and try

ssh -i /config/.ssh/id_rsa -o 'StrictHostKeyChecking=no' <remote_user>@<remote_IP> -t "sudo pkill; /home/<user>/ 300"

It makes no sense to specify a known host file and StrictHostKeyChecking=no at the same time.