Configure an automatic restart after SSL certificate change by Let's Encrypt

Today I configured my Let’s Encrypt add-on to restart every night to check my SSL expiration and renew it.

But once the SSL is renewed it doesn’t change the HA editor to the new SSL unless the HA core is restarted. So 5 minutes after asking to restart the plugin I launch this sh script, which checks if the certificate has been modified or not. In case we have a new certificate it restarts the HA core and warns me that the SSL certificate has been renewed. This way I avoid unnecessary restarts and only restart when there is a real SSL change. The new ssl is downloaded in the file /ssl/fullchain.pem and /ssl/privkey.pem.

So let’s put in a scipt that checks that newly downloaded file against a version from the previous day and if it’s different then reboot the machine. To do this we are going to put the script shell:

/homeassistant/shell/reboot_for_new_ssl.sh

#!/bin/bash

if cmp -s /ssl/fullchain.pem /config/personal/fullchain_old.pem
then
echo Certificado SSL sin cambios
exit 0
else
echo ¡Detectado nuevo certificado SSL!
cp -f /ssl/fullchain.pem /config/personal/fullchain_old.pem
exit 1
fi

and add in the
/homeassistant/configuration.yaml

########## Comando shell como servicio
shell_command:
reboot_for_new_ssl: bash /config/shell/reboot_for_new_ssl.sh

create an empty file in the path:
/homeassistant/personal/fullchain_old.pem

restart homeassistant and create the automatic tasks

We set every day at 4:00 to run this:

Imagen2

Very important: reboot_for_new_ssl_flag
and then create the if that chooses whether or not to restart the HA, send alerts, etc…

Imagen3

Today, there were just 30 days left before my certificate expired. I got it on February 20 and it was valid until May 20.

image

That is, he still has a month to go, because today April 21.

I got a message from HA saying that HA had restarted itself because I have new SSL. I have seen how it restarted itself and when I published the web again I had the new certificate:

image

So the script works perfectly!!! :slight_smile: