Command line sensor - error at startup

I have a command line sensor that calls a python script that reads from an energy meter. It works great and has for over a year… however, every startup I get this error:

[homeassistant.components.sensor] Error while setting up command_line platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 361, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 66, in async_setup_platform
    name: str = sensor_config[CONF_NAME]
                ~~~~~~~~~~~~~^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

It happens immediately in the logs on startup with no other errors. Here’s the command line sensor in my config file:

command_line: 
  sensor:
    command: 'python3 /config/command_line_scripts/get_emeter_readings.py'
    unique_id: 451BFDC3-361B-4016-AFEA-F3E147B83ADE
    name: emeter_readings
    value_template: "OK"
    json_attributes:
      - house_total_power
      - house_current_power_consumption
      - ytd_solar_generation
      - solar_output_power
      - house_net_energy
      - voltage
      - timestamp

Do I have something misconfigured? I have been over the docs several times and I can’t figure out what is wrong. Like I said though - other than this error, the sensor works as expected and has for like a year - I am just trying to clean up my config as much as possible.

I am on 2023.12.1

Hey,

since the last HA Update to 2023.12.1 I am getting the same error at every startup.

I have two command line sensors that read the CPU Temperature of another 2 Raspberry Pi´s in my Home. (I´m new to HA, but they have been running fine for a week until the Update was installed)

But now all I`m getting is this Error Message:

ERROR (MainThread) [homeassistant.components.sensor] Error while setting up command_line platform for sensor
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 361, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/command_line/sensor.py", line 66, in async_setup_platform
    name: str = sensor_config[CONF_NAME]
                ~~~~~~~~~~~~~^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable

And that is one of my Sensors:

- platform: command_line
    name: "octoprint cpu temp"
    command: "ssh -F /etc/ssh/ssh_config -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts pi@<IP-Adress> 'vcgencmd measure_temp' 2> /config/command.log"
    unit_of_measurement: "°C"
    scan_interval: 60
    value_template: >-
        {% set temp_index = value.find('temp=') %}
        {% if temp_index >= 0 %}
          {{ value[temp_index+5:].split('\'')[0] | replace('\'', '') }}
        {% else %}
          {{ none }}
        {% endif %}

I have no Idea why it doesn´t work anymore.

2 Likes

Thanks, now it works again:

command_line:
  - sensor:
      name: Octo-Print CPU Temp
      command: "ssh -F /etc/ssh/ssh_config -i /config/.ssh/id_rsa -o UserKnownHostsFile=/config/.ssh/known_hosts pi@<IP-Adress> 'vcgencmd measure_temp' 2> /config/command.log"
      unit_of_measurement: "°C"
      scan_interval: 60
      value_template: >-
        {% set temp_index = value.find('temp=') %}
        {% if temp_index >= 0 %}
          {{ value[temp_index+5:].split('\'')[0] }}
        {% else %}
          {{ none }}
        {% endif %}

Thanks, but you’ll note that I am using the “integration” rather than the platform:

command_line: 
  sensor:
    command: 'python3 /config/command_line_scripts/get_emeter_readings.py'
    unique_id: 451BFDC3-361B-4016-AFEA-F3E147B83ADE
    name: emeter_readings
    value_template: "OK"
    json_attributes:
      - house_total_power
      - house_current_power_consumption
      - ytd_solar_generation
      - solar_output_power
      - house_net_energy
      - voltage
      - timestamp

Oh snap - my bad. I found another platform-based command_line sensor I had in a package file from years ago! That was it.