Command Line Sensor Issue

This one is driving me batty. Hopefully someone can help. I’m trying to setup a command line sensor to count the number of JSON objects return in an API request.

here’s my configuration:

sensor:
- platform: command_line
  name: "qBittorrent Active"
  command: curl -s http://192.168.1.85/api/v2/torrents/info?filter=active | python3 -c "import sys, json; print(len(json.load(sys.stdin)))"
  scan_interval: 60
  unit_of_measurement: ""
  unique_id: 86efaaa4-4fb9-4dd4-a3ad-43bdb9047730

When I restart, despite refreshing cache (I even tried on a different device), ZERO returns for the sensor come back. I’ve tried a one-word name like “qBittorrentActive”, and same result. The curl is fine, because when I run the same from the command line, it works as expected:

brandon@Elitebook-Mint:~$ curl -s http://192.168.1.85/api/v2/torrents/info?filter=active | python3 -c "import sys, json; print(len(json.load(sys.stdin)))"
2

That “2” return immediately above is expected and accurate.

Thoughts? Thanks in advance!

Check out the note at the very start of the command line documentation.

command: curl -s http://192.168.1.85/api/v2/torrents/info?filter=active | python3 -c "import sys, json; print(len(json.load(sys.stdin)))"

As @mekaneck point out … maybe:

command: 'curl -s http://192.168.1.85/api/v2/torrents/info?filter=active | python3 -c "import sys, json; print(len(json.load(sys.stdin)))"'

Although unclear to me why python is involved unless you are just converting a raw string returned into JSON or something.

As kbrown01 mention, avoid using python just to manipulate json.
The whole resulting payload will be available in value_json in the value_template

The command line is now a top-level platform. Always check the official documentation rather than relying on old posts or tutorials.

So without addressing any other potential problems, you should have:

command_line:
  - sensor:
      name: "qBittorrent Active"
      command: curl -s http://192.168.1.85/api/v2/torrents/info?filter=active | python3 -c "import sys, json; print(len(json.load(sys.stdin)))"
      scan_interval: 60
      unit_of_measurement: ""
      unique_id: 86efaaa4-4fb9-4dd4-a3ad-43bdb9047730