[SOLVED] Command line Binary Sensor always off?

Hello everyone, I’m new here!

Short preamble:
I have chickens, there is a load cell (measures weight) in their roost, I want to have a binary sensor that tells me if they are on the roost or not.

I can query influxDB and see the average weight over the last 10 minutes, I use this to decide if they are on the roost or not using jq and if statements.

My configuration looks like this:

binary_sensor:
  - platform: command_line
    name: "Chickens Roosting"
    device_class: presence
    command: if (( $(curl "http://server.address.here:8086/query?pretty=false&db=chickens&q=select%20MEAN(*)%20from%20detector%20where%20time%20%3E=%20now()%20-%2010m" -s | jq ".results[0].series[0].values[0][1]" | jq floor) > 600)); then echo "on"; else echo "off"; fi
    payload_on: "on"
    payload_off: "off"

When I run this manually, I get nice clean on or off results, however, the sensor is always in the off state.

I have tried various combinations of specifying (or not) the payload_on and payload_off parameters, I have tried using all variations of on ON true TRUE True etc. but not had the sensor change to the on state.

Any help would be appreciated!

(( )) is a feature specific to the bash shell (and others), your sh might not support it.
Try this:

binary_sensor:
  - platform: command_line
    name: "Chickens Roosting"
    device_class: presence
    command: bash -c 'if (( $(curl "http://server.address.here:8086/query?pretty=false&db=chickens&q=select%20MEAN(*)%20from%20detector%20where%20time%20%3E=%20now()%20-%2010m" -s | jq ".results[0].series[0].values[0][1]" | jq floor) > 600)); then echo "on"; else echo "off"; fi'
    payload_on: "on"
    payload_off: "off"

That being said, you’re better off using a rest switch for this

Good idea, but it doesn’t seem to have helped.
I’m running this on a dedicated Pi using the Home Assistant OS.

You are right, but I gave up on anything that needed templates in the past because I couldn’t get them to work.

I’ve tried again with “REFTful Binary Sensor” and the following configuration, with success!

  - platform: rest
    name: "Chickens RESTing"
    device_class: presence
    resource: http://server.name:8086/query?pretty=false&db=chickens&q=select%20MEAN(*)%20from%20detector%20where%20time%20%3E=%20now()%20-%2010m
    value_template: '{{ value_json.results[0].series[0]["values"][0][1] > 600}}'

I think my issues before were that the json has a section called values and that is a reserved name - using ["values"] works - and probably will for all the other sensors I couldn’t get to work!

The template editor in developer tools was what helped me work this out when it said
built-in method values of dict object

Thanks for the pointers and suggesting I have another look at using RESTful.