Error running python script as command_line

Hi everyone,

I’m struggling to run the following python script on Home Assistant. I’m quite new to this, so I’m a bit out of my depth.

The script calls an API and then prints an integer:

import requests
import json 
import math 

#############################################################
#                          LOGIN                            #
#############################################################

email = "[email protected]"
password = "password"

url_login = "https://openapi.emtmadrid.es/v1/mobilitylabs/user/login/"

headers  = {
	"email":email,
	"password":password
	}

response = requests.get(url_login, headers = headers).json()

token = response['data'][0]['accessToken']

#############################################################
#                          ARRIVAL                          #
#############################################################


BusStop = "29"

url_arrival = "https://openapi.emtmadrid.es/v1/transport/busemtmad/stops/" + BusStop + "/arrives/"

headers  = {"accessToken": token}

data  = {
	"stopId" : BusStop,
	"Text_EstimationsRequired_YN" : "Y"
	}
response = requests.post(url_arrival, data = json.dumps(data), headers = headers).json()


next_bus_line = response['data'][0]['Arrive'][0]['line']
next_bus_time = math.trunc(response['data'][0]['Arrive'][0]['estimateArrive']/60)

print(next_bus_time)

It works perfectly on my W10 PC. I copied the .py file to \hassio\config\python_scripts\bus-sensor.py and included the following in the configuration.yaml:

sensor:
  - platform: command_line
    name: Next bus
    command: python /home/pi/.homeassistant/config/python_scripts/bus-sensor.py

After restarting HA, I get the following error in the Log:
Command failed: python /home/pi/.homeassistant/config/python_scripts/bus-sensor.py

I have tried changing “python” with “python3” and the path to “/config/python_scripts/bus-sensor.py” but nothing works.

Any idea on how to proceed? Thanks! :slight_smile:

Hello … Welcome to Home Assistant community !
Strange that .homeassistant directory is a sub directory of “pi” …
If I have to do this in my Home Assistant installation (in a virtual environment), the command should be:
python3 /home/homeassistant/.homeassistant/config/python_scripts/bus-sensor.py The py file should be executable as well (chmod x) I think. command is a string so maybe add quotes as well… Here is an example from my installation…

- platform: command_line
  name: smappee_solar_c
  command: "cat /home/homeassistant/.homeassistant/smappee_solar_c.txt"
  value_template: '{{ value }}'
  scan_interval: 10
  unit_of_measurement: 'W'

Thanks for your answer! I tried as you said, but, unfortunatelly, still the same error. This is the config now:

sensor:
  - platform: command_line
    name: Next bus
    command: "python3 /home/homeassistant/.homeassistant/config/python_scripts/bus-sensor.py"

When I try to navigate to that path through ssh I get:

core-ssh:~# cd /home/homeassistant/.homeassistant/config/python_scripts/
-bash: cd: /home/homeassistant/.homeassistant/config/python_scripts/: No such file or directory

You have probably copied the file to the wrong directory or you are searching for the file in the wrong directory…
If you look at \hassio\config\python_scripts\bus-sensor.py … does it exist ?
if yes, why not: “python \hassio\config\python_scripts\bus-sensor.py”

For some reason the double backslash is not appearing in the original post. But that path is where I placed the .py file from my Windows PC using Samba.

When I try to run python from ssh I get this:

core-ssh:/home# python3 foo
-bash: python3: command not found

Is “python” command working (instead of “python3”) ?
This is a link that could maybe help you to fix this if you installed Python3 on your environment… python - Install numpy for Python3 command not found error - Stack Overflow