BEWARE! THE CONFIGURATION BELOW MIGHT EXPOSE YOUR HASS INSTANCE TO CONTROL FROM AN UNAUTHORIZED USER! USE AT YOUR OWN RISK
After the big bold let me explain - my hass instance is accessible only from my internal network! It is not accessible from the Internet in any ways, hence my security is somewhat lax! If you’ve exposed your HASS host to the Internet I advise you NOT TO use the configuration below, unless you understand the configuration and its implications.
First install monit:
$ sudo apt-get install monit
Then edit /etc/monit/monitrc
and make changes to the following lines so that you can send e-mails (you need a working smtp server to send e-mails!):
# set mailserver mail.bar.baz, # primary mailserver
# backup.bar.baz port 10025, # backup mailserver on port 10025
# localhost # fallback relay
Enable the built-in localhost by uncommenting and editing the following lines:
set httpd port 2812 and
use address localhost # only accept connection from localhost
allow localhost # allow localhost to connect to the server and
# allow admin:monit # require user 'admin' with password 'monit'
# allow @monit # allow users of group 'monit' to connect (rw)
# allow @users readonly # allow users of group 'users' to connect readonly
The line above enables the http server and allows ANYONE to connect to it from localhost (i.e. the server where it is running). I use ssh local forwarding to access the monit instance from my own computer by adding the following parameters to ssh: -L 2812:localhost:2812
, this forwards port 2812 on my laptop/desktop to the Raspberry’s port 2812, so I type http://localhost:2812
in my browser and I’m able to view monit’s web UI.
Find the line below in /etc/monit/monitrc
and modify accordingly:
# set alert [email protected] # receive all alerts
Then in /etc/monit/conf.d/home-assistant I have the following:
check process home-assistant pidfile /home/hass/.homeassistant/home-assistant.pid
group automation
group home-assistant
start program = "/bin/systemctl start home-assistant.service"
stop program = "/bin/systemctl stop home-assistant.service"
if failed host localhost port 8123 proto http for 5 cycles then restart
if 5 restarts with 5 cycles then timeout
if cpu usage > 20% for 10 cycles then restart
Keep in mind that the above location of the PID file (/home/hass/.homeassistant/home-assistant.pid
) might be different for you! I have to admit setting up monit is not really that straightforward, and also it means with the above configuration, anyone, who has access to localhost can stop/start/restart your HASS instance! Use at your own risk!