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:
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 mqttexec.sh and give it run authority with sudo chmod +x mqttexec.sh:
#!/usr/bin/expect
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 ./mqttexec.sh, but ./mqtt-control.sh. The line with telegram sends me message to mobile that mqttexec.sh was killed and restarted:
#!/bin/bash
while true
do
./mqttexec.sh | ts
curl "https://api.telegram.org/bot_key/sendMessage?chat_id=my_chat_id&text=MQTTEXEC quit, restarting."
sleep 5
done
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:
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.