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
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.
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).
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…
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.
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 }}'