How do I check the availability of a webserver?

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 }}'
5 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.

3 Likes

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

Website down or up - #15 by Michel is the best I could find for this matter. I’d like to automatically handle notifications per-site instead of doing one automation per entity, but it’s good already.

Highly suggest: louislam/uptime-kuma: A fancy self-hosted monitoring tool (github.com)

Using this custom repo: https://github.com/meichthys/uptime_kuma

Looks like we might see this as an official integration in the future: Add Uptime Kuma Integration by meichthys · Pull Request #67985 · home-assistant/core (github.com)

I just wanted to check whether my OctoPrint camera was switched on or not - this worked a treat in my binary_sensor.yaml file, thank you all - I’d never have figured this out on my own.

# Check if OctoPrint camera is running
  - platform: command_line
    name: 'OctoPrint camera status'
    device_class: connectivity
    command: response=$(curl -LIk -m 5 http://octopi.local/webcam/ -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 503 && echo "OFF" || echo "ON"
    scan_interval: 60
    value_template: '{{ value }}'

Nice, but overkill when used for local network appliances.

I just created an integration (GitHub - mauro-midolo/homeassistant_webserver_status: Home Assistant Integration to check webserver status)

At the moment, the integration is not publish into HACS, you can import manually.

Using this integration you can have:

  • Connection status
  • http/https response time
  • http/https response code (i.e. 200, 301 or 400)

syntax has changed since, so now it should look like this, works like a charm!

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


Hi,
I just found this thread and am trying to do the same in HA 2024.07 but no binary_sensor is being created.

Does anybody know if something has changed? Or is this not supported in HA docker setups?

This was exactly what I was looking for. Sometimes I have docker containers go down, but ping checks won’t tell me anything because the host machine/network is still up. So this checks 90% of my domains I worry about

You are trying to check if one of the docker containers is down from HA?
Are you running HA OS or HA docker?

I think this should also be possible with command line if you ever wanted to stay inside HA with that check.