Python script works in terminal, but fails in command line sensor

I’m on HassOS and I followed this guide, I’m having an issue with a command line sensor:

- platform: command_line
  command: "python3 /config/custom_components/pfSense/pfSense_get_system_stats.py <host> <apikey> <apisecret>"

<host> is the ip of my pfSense firewall, <apikey> is the apikey etc.

When I run the command from ssh/terminal in Home Assistant it works fine, but the same command run from the sensor fails:

Logger: homeassistant.components.command_line
Source: components/command_line/__init__.py:41 
Integration: command_line (documentation, issues) 
First occurred: 22:49:15 (4 occurrences) 
Last logged: 23:04:17

Command failed: python3 /config/custom_components/pfSense/pfSense_get_system_stats.py <host> <apikey> <apisecret>

Anyone know why? Or what I should be doing differently?

what does your config look like for the sensor?

Hi! Thanks for coming back to me, the config of the sensor is:

- platform: command_line
  command: "python3 /config/custom_components/pfSense/pfSense_get_system_stats.py <host> <apikey> <apisecret>"

I followed the instructions in @bthoven’s comment as they were for hassio/HassOS

This is mine. I store my .py files in the python_scripts directory, but I don’t think that matters. I wonder if it has something to do with you not doing anything with the data after calling the command. I use the command_line sensor to grab the CPU temp, then the script also creates a file for the other file sensors.

#pfSense Stats Capture#
  - platform: command_line
    command: "python3 /config/python_scripts/pffa_get_system_stats.py <host> <api_key> <api_secret>"
    name: pfSense_CPU_temp
    value_template: '{{ value_json["data"]["stats"]["temp"] }}'
    unit_of_measurement : 'C'
 
  - platform: file
    file_path: /config/python_scripts/pfSense_stats.json
    name: pfSense_uptime
    value_template: '{{ value_json["data"]["stats"]["uptime"] }}'
 
  - platform: file
    file_path: /config/python_scripts/pfSense_stats.json
    name: pfSense_mem
    value_template: '{{ value_json["data"]["stats"]["mem"] }}'
    unit_of_measurement : '%'
 
  - platform: file
    file_path: /config/python_scripts/pfSense_stats.json
    name: pfSense_cpu
    # value_template: '{{ ( ( ((value_json["data"]["stats"]["cpu"].split("|")[0] | float) / (value_json["data"]["stats"]["cpu"].split("|")[1] | float)) - 1.0 ) * 100.0 ) | round(1) }}'
    value_template: '{{ ((value_json["data"]["stats"]["load_average"][0] | float) * 100.0 / 4.0 ) | round(0) }}'
    unit_of_measurement : '%'
 
  - platform: file
    file_path: /config/python_scripts/pfSense_stats.json
    name: pfSense_mbufpercent
    value_template: '{{ value_json["data"]["stats"]["mbufpercent"] }}'
    unit_of_measurement : '%'

That was it, as soon as I added the code to grab the CPU temperature after the command it worked. I now feel like an idiot! But I’m glad that it’s working!

Thanks for your suggestion :+1:

1 Like

Glad it’s working :+1: Don’t worry we all feel like idiots at times :wink: