Cannot get Command Line sensor to work

I have a working command line sensor that I cannot get to work using the new command line format.

Previously working sensor

sensor:
  - platform: command_line
    name: AQI
    command: "aqi aqi.algos.epa pm25:{{states('sensor.pm25')}}"

This is not working. The sensor just shows “unknown” :

command_line:
  - sensor:
      name: AQI
      command: "aqi aqi.algos.epa pm25:{{states('sensor.pm25')}}"

The command I’m trying to execute works from the HA terminal. Would appreciate any suggestions on what I’m doing wrong with the new template. Thanks!!

The lines “name” and “command” should line uop under the first ‘s’ in ‘sensor’ - similar to your first picture.

When you save the configuration.yaml file There should be an indicator in the upper right-hand corner of the screen that’s either red or green if it’s red that means there’s an error message and you should highlight it and it’ll tell you where the problem is

Not according to the documentation:

https://www.home-assistant.io/integrations/command_line/

Or my own working command line sensors:

- sensor:
    name: CPU Temperature
    command: "cat /sys/class/thermal/thermal_zone2/temp"
    unit_of_measurement: "°C"
    value_template: '{{ value|multiply(0.001)|round(1) }}'

- sensor:
    name: UV Index
    command: "curl -k --silent 'https://uvdata.arpansa.gov.au/xml/uvvalues.xml'|sed -n '86p'|cut -c 12-14"
    unit_of_measurement: " "

@chanster did you restart home assistant after adding that config?

You can’t use reload until you have restarted once after adding a new integration.

@tom_l and @KruseLuds :

My identation is per the documentation. I tried aligning them all up as per @KruseLuds, that triggers an error.

Yes, I did a full restart instead of just a reload of configuration.yaml.

Some more information, I looked into the HA logs and see this:

Logger: homeassistant.components.command_line.utils
Command failed (with return code 127): aqi aqi.algos.epa pm25:5.0
Command failed (with return code 127): aqi aqi.algos.epa pm25:6.6
Command failed (with return code 127): aqi aqi.algos.epa pm25:6.0
Command failed (with return code 127): aqi aqi.algos.epa pm25:12.0
Command failed (with return code 127): aqi aqi.algos.epa pm25:10.0

The values at the end come from states(‘sensor.pm25’). When I copy paste one of the executed commands from the log above into HA terminal, it works:

aqi aqi.algos.epa pm25:10.0
42

42 is the result I want to pass to the AQI sensor.

Try this:

command_line:
  - sensor:
      name: AQI
      command: "aqi aqi.algos.epa pm25:{{ states('sensor.pm25')|float(0) }}"

I suspect what you are actually sending is:

aqi aqi.algos.epa pm25:"5.0"

Thanks for your suggestion @tom_l, I tried it and unfortunately still getting this error message in the logs:

Command failed (with return code 127): aqi aqi.algos.epa pm25:0
Command failed (with return code 127): aqi aqi.algos.epa pm25:11.0
Command failed (with return code 127): aqi aqi.algos.epa pm25:9.0

return code 127 means command not found.

What happens if you remove the template (just for testing purposes)?

command_line:
  - sensor:
      name: AQI
      command: "aqi aqi.algos.epa pm25:10.0"

Still getting the same error in log:

Command failed (with return code 127): aqi aqi.algos.epa pm25:10.0

The puzzling thing is that the same command when running directly in HA terminal works just fine:

$ aqi aqi.algos.epa pm25:10.0
42

42 is the expected result.

“aqi” is installed in /usr/bin and even when I add this to the template, it doesn’t work:

Command failed (with return code 127): /usr/bin/aqi aqi.algos.epa pm25:10.0

And /usr/bin is also in the path:

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

How did you install home assistant?

The terminal and command line integration execute in different containers.

Mine is a new HAOS install. So how do I figure out what’s the correct path using the home assistant container context?

This might help: SSH'ing from a command line sensor or shell command

Even if you do get the context right that is going to disappear next time you update the OS.

The only way you are supposed to install software in HAOS is through integrations or addons.

My apologies, you are correct

Thank you!! I got it to work based on the link you shared! The issue was that I didn’t know that the HA terminal is a different container to the main HA container. I was tested the code in the HA terminal (working) but it wasn’t working in the main HA container because I never installed the “aqi” utility in the main HA container.

I followed the instructions that you linked to and I was to access the HA container and after I installed “aqi” there, it worked! But yes, I will need to do this everytime there is an HA update.

I’m going to look into using AppDaemon as a way to get around this. THANK YOU again @tom_l !

2 Likes