Hass.io Python script issues

Hi guys,

I’m trying to run a simple Python script via Hass.io - I can see there’s tons of topics on this already, and I’ve gone through probably all of them to try and get this to work, but I can’t.

The common solution seems to be setting up a command line switch and having the script as either the command_on or command_off - this part is fine, and when I turn on the switch it seems to pass the right message across, but then I get a ‘command failed’ error. This is super frustrating because it’s an incredibly unhelpful error, and the same script works perfectly if I SSH into the instance (using the SSH add-on) and run it.

I’m guessing it might be a user issue since when I SSH into the instance I’m root, but I’m not sure what Hass.io runs as when these scripts are being called - how would I address this and make sure Hass.io has enough permissions to run the script? Is it even a permissions issue?

This is the error I get:

Command failed: python3 /config/tv.py

The script itself is:

import samsungctl
import time
import subprocess

config = {
“name”: “samsungctl”,
“description”: “PC”,
“id”: “25”,
“host”: “192.168.1.140”,
“port”: 55000,
“method”: “legacy”,
“timeout”: 0,
}

subprocess.check_output(‘docker exec -t homeassistant /bin/bash -c “echo “as” | cec-client -s”’, shell=True)

time.sleep(2.0)

with samsungctl.Remote(config) as remote:
remote.control(“KEY_HDMI”)
time.sleep(2.0)
remote.control(“KEY_HDMI”)

I’d really appreciate any help on this as I’ve been tearing my hair out for the last couple of days - this is something that really should be pretty simple to do and I’m sure there’s a way to do it so please help!

Thanks,
Aazim.

It looks like you’re trying to call the docker command from within the hassio container, which won’t work, because docker isn’t in the container, it’s on the host OS.

Just remove the docker exec -t and call the homeassistant command directly.

Additionally you should check it runs in a console in the container… just because it runs in the ssh addon doesn’t mean it will run in the HA container.

Thanks both for your help - I’m completely new to Docker (and containerization in general) so I wouldn’t be surprised if I was doing something stupid here.

I tried removing the

‘docker exec -t’

comment and now get the following error:

subprocess.CalledProcessError: Command ‘homeassistant /bin/bash -c “echo “as” |
cec-client -s”’ returned non-zero exit status 127.

@DavidFW1960 - you say there’s a difference between running things in the SSH addon vs. in the container. How would I access the container without using the SSH addon?

I realize my questions might be basic but like I said I’m still new to this - really appreciate your help!

You can use Portainer and open a console in the HA container and then see if your script runs with no errors. If it runs there then it will run from Home Assistant as well. Just know that it running in the ssh container is no guarantee that it will run from Home assistant in a command line.

1 Like