Cannot get a commandline sensor to work

I try to get a commandline sensor to work
This is the yaml:

- platform: command_line
  name: gateway_woonkamer_temperatuur
  command: '/usr/bin/python3 /home/homeassistant/python_scripts/test.py'
  scan_interval: 60
  unit_of_measurement: '°C'

The content of test.py:

x = 12.3
print(x)

In the logbook I get the message:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:41
Integration: command_line (documentation, issues)
First occurred: 23:46:03 (3 occurrences)
Last logged: 23:48:03

Command failed: /usr/bin/python3 /home/homeassistant/python_scripts/test.py

When I run test.py in a terminal i get 12.3 as output.

What am I doing wrong?

How are you running HA? HA OS, Supervised install, docker, venv etc? The correct path to your script will vary depending on the answer.
If running in a venv, the path is likely

/home/homeassistant/.homeassistant/python_scripts/test.py

For the other cases

/config/python_scripts/test.py

Also, the script should be inside the config folder reachable by HA, so place it in
/home/homeassistant/.homeassistant/python_scripts/test.py.

You missed the /.homeassistant/

Sorry, of course I should have mentioned that: HA is running in Docker with Docker Compose.
The config folder is “/home/homeassistant”. There are also the yaml-files and the db-file.

@tjntomas
You set me on the right track.
With docker the standard folders like /usr/bin/ are not accesible.
I give myself a shell in the homeassistant docker with:
docker exec -it homeassistant /bin/bash
I then figured out that this shell starts in de config-folder of homeassistant
So the comandline should be:
python3 python_scripts/test.py
And that worked fine!!

1 Like