I want to check if specific webservers are available. And I of course want a negative result if there is no server listening.
It looks as if the “RESTful Binary Sensor” is not made for this purpose because it generates an error if there is no answer.
So which sensor is to be used to check the availability of a http server?
Ping and Command line don’t work for my setup and requirements but “TCP Binary sensor” shuold be it.
I am playing with it but I cant find out what the template should be like.
When I send
get /
to the server, it returns very much text that begins with
HTTP/1.1
Thats fine. But what does the template look like that matches this long text (or just the first few characters)?
All the templates I find handle JSON. This is no JSON.
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
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