Binary_sensor with command_line platform not updating properly

Hi all,

I am running nuts on this… but step by step:
I am running a Logitechmediaserver to serve music to all rooms of my house. However, the LMS somtimes hangs and with an automation I want LMS to be restarted without user interaction.

Therefore I created a PING sensor, but if the service for LMS stopps running, the PING is still ok. So I tried to grab the HTTP code (200 is OK everything else should trigger a service restart).

I grap the http status code with curl:

curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/

This returns “200” when the service is running and “000” when the service is not running.

Now I created the following binary_sensor (in my binary_sensor.yaml):

- platform: command_line
  command: "curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/"
  payload_on: "200"
  payload_off: "000"
  name: LMSstatus
  unique_id: "LMS Status"
  scan_interval: 5

The sensor is always "on" also if I shut of the LMS service manually it will not switch to "off".I also tried to replace payload_off: "000" with payload_off: "" but there is no change detected.

Does anyone know what I am doing wrong?

I also just used a sensor with the following setup (in my sensor.yaml):

- platform: command_line
  name: "LMSStatus"
  unique_id: "LMS Status"
  command: "curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/"
  scan_interval: 10
  value_template: "{{ value }}"

But the sensor value didn’t update. It always stayed at “200”.

I am more than happy if anyone can’t point me to the right direction…

Thanks in advance for any help!

Regards, goeste

Long shot: what you’re getting is 200 sent to stderr, because you’re redirected the actual document to /dev/null; but you need the code sent to stdout. Try:

command: "curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/ 2>&1"

Also, you don’t need the value_template line.

a good tip @Troon, but unfortunately after an reboot of HA the entity still doesn’t update…

Anything in the logs? It’s a different curl binary in the HA “shell” than the one you’re running.

You don’t need to reboot:

1 Like

The log says now:

* Command failed (with return code 1): curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/ 2>%1
* Command failed (with return code 7): curl -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/ 2>&1

What is code 1 and 7 ?

OK, I get the typo in the first line (% instead of &). But “7”?

https://everything.curl.dev/usingcurl/returns

  1. Failed to connect to host. curl managed to get an IP address to the machine and it tried to setup a TCP connection to the host but failed. This can be because you have specified the wrong port number, entered the wrong host name, the wrong protocol or perhaps because there is a firewall or another network equipment in between that blocks the traffic from getting through.
1 Like

ok…

I get that I can sucessfully retrieve a 200 and an error if the service is down (because no http code is actually delivered or can’t be read or whatever, correct?). But why is the entity not updating, at leat to not available or sinmilar?!

Got this solved!
I placed the actual command in an executable file and call that instead of the command itself from the sensor:

- platform: command_line
  command: "/config/lmsstatus.sh"
  payload_on: "200"
  payload_off: "000"
  name: LMSstatus
  unique_id: "LMS Status"
  scan_interval: 5

The executable look like the following:

#!/bin/bash
if curl --fail -s -o /dev/null -w '%{http_code}' http://192.168.2.240:9000/material/ 2>&1; then
  echo
else
  echo
fi;
1 Like