Home Assistant - Web Service 'Ping' Status Checker Thingy Sensor

Home Assistant - Web Service 'Ping' Status Checker Thingy Sensor

Hello there! If you're familiar with Home Assistant you've most likely come across or even used the 'device tracker' element (more details here). This is done by 'pinging' a device over your wireless network or having certain applications provide the information to HA such as GPS location via a third-party service (OwnTracks for example).

As the name mentions, this feature allows you to 'track' your 'devices' in HA such as phones, TVs or any other WiFi enabled devices and ultimately bring back a result to say whether the device is 'Online' or 'Offline'. This is great however you may also want to track other things such as Web Applications or services hosted on a server or an internal device (like a Raspberry Pi).

In my case, I wanted to monitor web applications, which aren't techincally 'devices' and determine whether these services are up and running or broke and somehow fell over and died (offline).

For example, my Plex server. I currently host and run several web applications such as Sonarr, Radarr, Transmission and Plex which mostly rely on eachother to provide me with all my media needs. As these all run on the same 'device' (although on different ports), the current device tracker element would only be able to report and see the single entity which is the server (VM) and not be able to determine which service is up or down ... Until now!

Having a browse through the HA Community forum, I stumbled across some clever chaps (and/or chapettes) who managed to get around this using the 'command line' platform to do some clever magic with curl'ing and listen out for specific IP addresses, including ports, to determine whether a web service is either up or down.

In the example below, I've managed to create a sensor to listen out for the Transmission service running on my Plex box (hosted on port 9091) and bring back a response of either 'Online' or 'Offline' depending on the HTTP code repsonse.

platform: command_line
name: 'Transmission Online'
command: response=$(curl -LIk -m 3 -o /dev/null -w "%{http_code}\n" -s); test "$response" -ge 200 && echo "Online" || echo "Offline" 
scan_interval: 90
value_template: '{{ value }}' 

This command will 'curl' the IP and port to determine if the service is up or down and then spit out a value of 'Online' or 'Offline' (these values can be changed to whatever you desire). Once this is added into HA, I've assigned a custom icon (using 'customize') to the new sensor (sensor.transmission_check) and placed it in a lovely Glance card alongside the other services hosted on that server.