Curl switch to detect if service is up

I am looking at the https://www.home-assistant.io/integrations/switch.command_line/ via Hassio

This would give me what I need:

core-ssh:~# curl --url http://192.168.0.96:21080
curl: (7) Failed to connect to 192.168.0.96 port 21080: Connection refused

Basically I need the syntax that checks if the curl response says Connection refused then make the switch off, on otherwise.
No idea how to formulate the expression to give the values above however

Do you want to just show the state of the service or do you also have some commands to turn it on/off?

Is the service really down when it says “connection refused”? It looks more like a permission issue to me.

Yes a ping doesn’t work because it might ping ok but refuse the connection on that port.
No other commands needed, not sure how often it updates either, can I set the refresh value to something like every 5 minutes?
I used a port that I know to be not in use as an example above

So what you are looking for is a command line binary sensor and not a command line switch as you only want to know if the service is on or off.

In the binary sensor you can set a scan interval and it will then only update the value based on this.

You may try:

curl -o /dev/null -qsI -w "%{http_code}\n" https://your.url

and do whatever you want on specific HTTP response codes (https://www.restapitutorial.com/httpstatuscodes.html) .

If you need strings instead of status codes you may use:

curl -sI https://your.url |grep HTTP |cut -f3- -d\ 

Ok so when it is refused I get 000


core-ssh:~# curl -o /dev/null  -w "%{http_code}\n" http://192.168.0.96:21080
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (7) Failed to connect to 192.168.0.96 port 21080: Connection refused
000

And as a binary sensor something like this?

binary_sensor:
  - platform: command_line
    command: 'curl -o /dev/null  -w "%{http_code}\n" http://192.168.0.96:21081'
    name: 'is_online'
    device_class: connectivity
    payload_on: 1
    payload_off: 0

But I am not seeing how it knows when to set it to 1 or 0.

payload_on and payload_off needs to be the respond you receive from the command when the service is on resp. off.

What do you get as a response if the service is on?

Btw I would probably use another name than “is_online”, but that’s just my opinion.

Yes I will name that correctly once I finalize but I am getting this (a 200 message) when it does work

core-ssh:~# curl -o /dev/null  -w "%{http_code}\n" http://192.168.0.96:21081
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1902    0  1902    0     0   8025      0 --:--:-- --:--:-- --:--:--  8025
200

The 200 when it works and 000 when it doesn’t

Please add the -qsI options as well, so you can get rid of the progress part.

Please also note that you have a lot of HTTP codes, 200 is only one of them. So you’ll have to parse the returned value and set the payload value, something like:

curl -o /dev/null -qsI .... | awk '{if ($1 == 200) {print "1"} else {print "0"}}'
1 Like

If you always receive code 200 if it works and 000 if it doesn’t work then try this:

binary_sensor:
  - platform: command_line
    command: 'curl -o /dev/null  -w "%{http_code}\n" http://192.168.0.96:21081'
    name: 'is_online'
    device_class: connectivity
    payload_on: "200"
    payload_off: "000"

I’m not sure whether the response is a string or an int, from the response 000, I assume it’s a string, however if this doesn’t work you may need to remove the quotes around 200 and change the payload_off to 0.

I believe that did it, thanks :slight_smile:

if it may be useful:

binary_sensor:
  - platform: command_line
    command: 'ping -W 1 -c 1 192.168.xxx.xxx > /dev/null 2>&1 && echo On || echo Off'
    name: 'Your _name'
    scan_interval: 60
    payload_on: "On"
    payload_off: "Off"

I have an issue with payload_off value.

‘payload_on’ works fine when set to “200”. However, I cannot get proper ‘disconnected’ status, as ‘payload_off’ never works properly with both “000” and 0 (integer) as value - status is either Connected or Unknown (never ‘Disconnected’).

Can anyone help here? Looks like some stupid mistake from my side, but it does not work properly.

Here’s my code:

command_line:
  - binary_sensor:
        scan_interval: 20
        command: 'curl -s -o /dev/null --insecure --socks5-hostname 192.168.1.11:8111 -w "%{http_code}\n" https://172.1.1.1'
        name: 'vpn_online'
        device_class: connectivity
        payload_on: "200"
        payload_off: "000"

Fixed by clearing curl exit code. HASS is unable to properly handle non-zero output from scripts, and reporting this to the log file as an error (leaving actual sensor value unavailable). Also, curl timeout is good to have.

Working code is below:

command_line:
  - binary_sensor:
        scan_interval: 20
        command: out=`curl -s -o /dev/null --insecure --connect-timeout 2 --socks5-hostname 192.168.1.99:8443 -w "%{http_code}\n" https://172.1.1.1` && echo $out || echo 000'
        name: 'vpn_online'
        device_class: connectivity
        payload_on: "200"
        payload_off: "000"