How do I check the availability of a webserver?

I don’t know. From a quick search, it looks like they should work fine.

they did in my previous setup.

It works now with the following config

binary_sensor:
  - platform:      command_line
    name:          'webtools_via_http'
    command:       curl -m 3 -s webtools.internal:80/ > /dev/null && echo ON || echo OFF
    scan_interval: 33

Thanks!

6 Likes

Thanks a bunch for this thread… Not sure if the new version changed something on us but payload_on/off seems to be required. Here’s the config working as of 2018-10-20

binary_sensor:
  - platform: command_line
    name: 'Kibana - Status'
    command: curl 192.168.0.251:5601 > /dev/null && echo on || echo off
    scan_interval: 180
    payload_on: "on"
    payload_off: "off"

I don’t have access to my setup just now so can’t test - what does this do?

My example above shows 2 things… one is how to get a Linux command to run and you being able to recognize between success or failure. In my case, it’s testing for a website being up and running. If the website is accessible then binary sensor is on otherwise off.

It’s useful to couple this with an automation that says if a site has not been on for X amount of time, notify me.

Thanks for the hints in this thread. Based on this i created a self-check for the webserver which seems to die from time to time while HA is alive (Pi3 + Hassio)… I understand this is a bit crude but so far it seems to do what I intended. I also tried hassio.restart which does not work in this context.

Some explanations: I used duckdns and from inside the network the resolution does not work properly thus the cert evaluation will come back with an error, which in the case of curl is return code 51. If I do not check for the curl return code the binary sensor would always be off…
Hope that is useful.

binary sensor: (in binary_sensor.yaml)

  - platform: command_line
    name: 'HA alive'
    command: curl -X GET https://someaddress:someport > /dev/null 2>&1; test $? -eq 51 && echo on || echo off
    scan_interval: 60
    payload_on: "on"
    payload_off: "off"

automation: (in server_health.yaml)

alias: Home assistant health check
id: 'home assistant health check'
initial_state: 'on'
trigger:
 - platform: state
   entity_id: binary_sensor.ha_alive
   to: 'off'
   for:
     minutes: 4
action:
 - service: notify.something
   data_template:
     message: "Webserver is not responding, restarting system in 1 minute {{now()}}."
 - delay:
     minutes: 1
 - service: homeassistant.restart

Payload isnt allowed anymore in the platform: command_line.

payload_on: "on"
payload_off: "off"

Curious what kind of solutions you all have been taken?

I used to do the same kind of sensor:

- platform: command_line
  name: 'Unifi alive'
  command: response=$(curl -LIk https://172.16.3.205:8443 -o /dev/null -w '%{http_code}\n' -s); test "$response" -ge 200 && echo "on" || echo "off"
  scan_interval: 600
  payload_on: "on"
  payload_off: "off"

You guys figure this out? I tried

- platform: command_line
  name: 'HA is alive'
  command: response=$(curl -LIk http://172.16.3.2:8123 -o /dev/null -w "%{http_code}\n" -s); test "$response" -ge 200 && echo "on" || echo "off" 
  scan_interval: 90
  value_template: '{{ value }}'

But it keeps returning as “off” even though if I run the command in ssh it returns “on” i am pretty sure it has something to do with quotes

Thanks Sejnub !!! Just used your code and working fine ! Thanks

I finally got it to work after looking at the source code here. It expected capitals


- platform: command_line
  name: 'HA is alive'
  command: response=$(curl -LIk -m 3 http://172.16.3.2:8123 -o /dev/null -w "%{http_code}\n" -s); test "$response" -ge 200 && echo "ON" || echo "OFF" 
  scan_interval: 90
  value_template: '{{ value }}'

I also added -m 3 which is --max-time basically a timeout.

This worked for me!

This looks like a very good solution, however is not working for me :frowning:
I get the following error log message under developer tools:

Command failed: response=$(curl -LIk -m 3 SERVER -o /dev/null -w "%{http_code}\n" -s); test "$response" -ge 200 && echo "ON" || echo "OFF"

10:52:28 PM – command_line (ERROR) - message first occurred at 10:28:12 PM and shows up 25 times

any ideas?

Maybe https:// , example above used http://

Error in choosing verification method.

-qe
needs to be replaced with
-eq

Working line:

response=$(curl -LIk -m 3 https://******:8123 -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 405 && echo "ON" || echo "OFF"

1 Like

I’ve been using this for a while:

binary_sensor:
  - platform: command_line
    name: example.com
    device_class: connectivity
    command: response=$(curl -LIk -m 3 https://example.com -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 200 && echo "ON" || echo "OFF"
    scan_interval: 300
    value_template: '{{ value }}'
3 Likes

This does exactly what I need it to do. It returns Connected or Disconnected if my self-hosted sites are running, not just if there’s something on the other end. For instance, my crappy 502 bad gateway Nginx fail returned “disconnected”. Perfect, thank you.

1 Like

Hi there (my first HA post).

This works great for me for checking WAN and for LAN services (e.g. NAS, router, hoobs).

However, it doesn’t work for my Home Assistant instance (internal or external) or plex instance. It also fails for NAS services that use browser authentication (e.g. raidarr and sonarr).

Any idea for how to fix this?

Many thanks - needed to create a monitor to alert me when Honeywell Total Comfort Connect goes down, which is pretty frequent…

binary_sensor:
  - platform: command_line
    name: 'Honeywell TCC'
    device_class: connectivity
    command: response=$(curl -LIk -m 3 https://mytotalconnectcomfort.com -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 200 && echo "ON" || echo "OFF"
    scan_interval: 300
    value_template: '{{ value }}'

I use uptimerobot.com. it is free and HA has a component to integrate it as one sensor for each server.

To get a 200 respons you ned the exact correct end url, if it redirects it will fail.
Home-Assistant will redirect different if you er logge in or not, as for what I see it redirects to /auth/authorize…

Or you can use another respons code (test curl to see what) List of HTTP status codes - Wikipedia