How to monitor the status of web servers in Home-Assistant



If you are reading this article you might have some services with built-in webservers running, Home-Assistant is an example of one. In fact, many services use a web-based interface.
These services are often useful to monitor, and most people do it with the ping integration. The problem with this method is that is is just monitoring if the host is online in the network. But in all cases, the server can be up even if the service itself is down or not working.
There is no way to guarantee 100% that a service is working, but by using the HTTP status codes from he webserver we can be pretty sure the service is up and working as it should. Then you can get sensors like this:

Find and test the correct address

Before making any sensor I recommend to test if the URL’s are resulting in he correct status code. 200 OK is what you want. This can be done with curl, if you are using Windows use the Windows version. Run it from CMD after moving to the cd curl\bin\ path.
Then Run curl -I http(s)://server/
It is also possible to run curl commands directly from the Home-Assistant server, I recommend the official Home-Assistant add-on Terminal & SSH for this.

In the example blow I get a HTTP/1.1 200 OK response from my mStream server.

C:\curl\bin>curl -I
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Accept-Ranges: bytes
Cache-Control: public, max-age=0
Last-Modified: Mon, 27 Jan 2020 02:01:56 GMT
ETag: W/"cb27-16fe4bbe220"
Content-Type: text/html; charset=UTF-8
Content-Length: 52007
Date: Fri, 21 May 2021 13:37:00 GMT
Connection: keep-alive

if you get anything but 200 OK, try to correct the command according to the sever and status code.

If the message curl: (60) SSL certificate problem: unable to get local issuer certificate appear you can ignore the certificate by adding --insecure as a parameter.
Example for my local ESXi server:

C:\curl\bin>curl -I --insecure
HTTP/1.1 200 OK
Date: Fri, 21 May 2021 13:37:00 GMT
Connection: Keep-Alive
Content-Security-Policy: block-all-mixed-content
Content-Type: text/html
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1
Content-Length: 4201

Home-Assistant Sensors

Now to the sensors, I have found some code to get this implemented in Home-Assistant.
Replace with your own url.

If you wan he status code itself use this sensor:

  - platform: command_line
    name: mStream HTTP Response Code
    command: curl -I 2>/dev/null | head -n 1 | cut -d$' ' -f2
    scan_interval: 60

If you just want to know if it’s okay or not use this binary sensor, it will return Problem or OK.

  - platform: command_line
    name: mStream HTTP Response Status
    command: response=$(curl -LIk -m 3 -o /dev/null -w "%{http_code}\n" -s); test "$response" -eq 200 && echo "OFF" || echo "ON"
    scan_interval: 60
    value_template: '{{ value }}'
    device_class: 'problem'

In both examples above replace with your service URLs.
Here is a list of services that works in my setup (this URL’s might not be valid in all setups)

pfSense: http://<ip>/
VMWare ESXi 7: https://<ip>/ui/#/login --insecure (This website):
ZWave TO MQTT: http://<ip>:8091/
Phoscon – http://<ip>/
UnRAID: http://<ip>/Main
CrashPlan (UnRAID/Docker): http://<ip>:5800/
Plex (UnRAID/Docker): http://<ip>:32400/web/index.html
Deemix (UnRAID/Docker): http://<ip>:6595/
AirSonic (UnRAID/Docker): http://<ip>:4040/login
UnRAID API (UnRAID/Docker): http://<ip>:3005/
mStream (UnRAID/Docker): http://<ip>:3000/

Lovelace Examples

Lovelace example of a entity card, just he status:

Another cool view is the history graph, here in panel mode:

Looks like my UnRAID API docker is a bit unstable…