Issue with command_line sensor (json payload)

Hi,

I am having a stupid issue with a command_line sensor, but I don’t seem to be able to resolve it.

system info:

Home Assistant 2023.10.3
Supervisor 2023.10.0
Operating System 11.0
Frontend 20231005.0 - latest

The issue

I am trying to read info from my unifi APs through a python script (several scripts are available through the HA forums). Running that script trhough a command line sensor, this should give a nice sensor with some json values mapped to attributes.

Command line sensor (I work with packages):

# unifi sensors (using external scripts)

command_line:
  - sensor:
      command: "python3 /config/www/pyscripts/unifiapbeneden.py"
      name: unifi_apbeneden
      value_template: "{{ value_json.Clients }}"
      # unit_of_measurement: Clients
      scan_interval: 60
      json_attributes:
          - Guests
          - Clients_wifi0
          - Clients_wifi1
          - Score  
          - CPU
          - RAM
          - Uptime
          - Score_wifi0
          - Score_wifi1
          - Activity
          - Update

In the home assistant logs the sensor is defined as “unknown”

image

In the logs I find the following error:

image

I don’t seem to find what is causing this as running this through the home assistant terminal gives the normal json output (hence the script is working):

Anyone knows what could be causing this and how I could resolve this?

Many thanks for your input and kind regards,

Ben

Seems like you’re facing a bit of trouble with your command_line sensor for accessing your Unifi APs. Let’s try to troubleshoot this.

The error you’re encountering, “ValueError: No JSON object could be decoded,” typically suggests that the script isn’t producing valid JSON output. However, since you mentioned that the script is working fine when run via the Home Assistant terminal, it’s possible that the context in which the script is executed by the sensor is causing the issue.

Here are a few steps you could take to resolve this:

  1. Check script dependencies: Ensure that all necessary dependencies for your script are available in the Home Assistant environment.
  2. Adjust the script for compatibility: Double-check if there are any environment-specific configurations within the script that might cause differences when run by the Home Assistant command_line sensor.
  3. Confirm user permissions: Make sure that the user under which Home Assistant is running has the necessary permissions to execute the script and access its resources.
  4. Troubleshoot the script output: You might consider modifying your script to print the output or log it to a file, enabling you to see the exact format of the output and verify that it matches the expected JSON format.
  5. Check for any runtime environment differences: Compare the runtime environment of the script when run through the Home Assistant terminal with the runtime environment when executed by the command_line sensor. Look out for any differences in environment variables, permissions, or configurations that might impact the script’s behavior.

Hi there,

Can you please give me some more explanation how I would tackle all of your remarks? I am by no means a programmer. it seems to me both the terminal and the command line are run on the same machine with the same dependencies, compatibility and user permissions.

As for the script output, that’s already in my post above, and this seems like this should work with the sensor.

Please forgive me my bluntness, but to me it seems you ran my question through something like chatGPT. It would be anyhow great if you could point me in the right direction to troubleshoot any of the suggestion you did above, as I have no idea on how to start on those.

Thank you!

kind regards,

Ben

Hi all,

Solved this myself by generating of a log by appending

2> /config/command.log

to my command line in the command_line sensor, which showed the command_line sensor did not find the py module (which was installed by terminal) then by looking at this post

and circumventing the issue of the py module not being available for the command sensor (not sure why this error occurs as on the terminal (which is run under rooot) the module was installed) but in any case, the sensor works.

thanks

Ben