Command line sensor: Bizarre problem

A command line sensor I’ve had for some time suddenly started misbehaving. To boot, misbehaving in a very odd way. The sensor is contained within its own command_line.yaml file.

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
    - last_updated
    - key_line_alerts
    - standard_reports
    - priority_reports
    - standard_report_count
    - priority_report_count

The sensor works. I have a live HA sensor entity… it just isn’t getting the data entirely correctly.

“Last_updated” is generated by the python script itself; everything else is pulled from a webpage via the script/BeautifulSoup.

Here’s the odd bit – the script DOES run, because HA is being fed the “last_updated” data; everything else however, is blank. I can verify this both by looking at the sensor entity, as well as by looking at a text file the python script outputs (raw json). However, if I run the script manually, the output is completely correct, including all the data being pulled from the webpage!

Has anyone seen anything like this, or have any advice as to how to troubleshoot?

-J

UPDATE:

Thanks everyone who helped – at least we all learned something about indentation!

Nothing was wrong with HA or the script persay. The URL I was scraping had changed :confused:… and in a fairly subtle way so I didn’t notice it when checking the code. :roll_eyes:

There is no indentation for the values

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
      - last_updated
      - key_line_alerts
      - standard_reports
      - priority_reports
      - standard_report_count
      - priority_report_count

Can’t believe I missed that – and I SO wanted it to be just that simple.

Sadly no dice.

Corrected the indentation error as you noted:

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    command_timeout: 60
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
      - last_updated
      - key_line_alerts
      - standard_reports
      - priority_reports
      - standard_report_count
      - priority_report_count

… and same issue. When HA runs the script all it gets is “last_updated” and fails to retrieve anything from the webpage. When I run the script locally… it produces the JSON as designed.

-J

Not needed, the list will be processed properly.

A common misconception. As long as they are all indented the same amount it does not matter how far they are indented.

valid:

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
    - last_updated
    - key_line_alerts
    - standard_reports

valid:

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
          - last_updated
          - key_line_alerts
          - standard_reports

invalid:

- sensor:
    name: "tpg_traffic"
    command: "python gettpgdisruptions.py"
    scan_interval: 120
    value_template: "Sensor Ok"
    json_attributes:
     - last_updated
      - key_line_alerts
    - standard_reports
1 Like

But they should have at least same indent as the parent key, right?

Are you running it in the Home Assistant container?

That’s where the sensor executes it.

Can you share the json you get?

Only for style. It is not required. Each block must be consistent.

e.g. also valid:

key_1:
  - list_item_1
  - list_item_2
  - list_item_3
key_2:
         - list_item_1
         - list_item_2
         - list_item_3
1 Like

Also, I think that py file should be in a folder which is accessible for HA in docker. Like inside “/config/my_scripts” where “my_scripts” is declared in “allowlist_dir” (or how it is called) option.

I meant that this is not valid, right?

    key_1:
  - list_item_1
  - list_item_2
  - list_item_3

I.e. the indentation should be at least same as the parent line.

Yeah, it has to be at least the same indentation as the parent.