Push notification html5 for HA

Found this guide on reddit

So for the past three days I’ve been struggling to set up push notifications since the guides miss some pretty crucial steps. I finally got it working and hoping to save someone headache in the future, I’m going to write a quick guide. I’ll likely use this myself if I need to set it up again.
Assumption
Up-to-Date Home Assistant
Installed on a Raspberry Pi
Used All-In-One Installer
DuckDNS
Home-assistant.io guide
Bruh-automation guide (with video)
Log into your router and go to NAT/Gaming or IP Forwarding or Whatever it might be called. IT is different for each router. You need to forward external port 8123 to internal port 8123 on the internal IP Address of your Raspberry pi.
Find your public IP Address and connect to it on port 8123. For example, if your IP address is 123.456.78.90, type 123.456.78.90:8123 into your browser. Home Assistant should load (If it doesn’t work, try disabling the wifi on your phone and connecting that way. You might only be able to connect properly if you are outside of the network).
Go to www.duckdns.org and login using your choice of methods at the top.
Enter a name for your subdomain. For this, I will use hass-example making my full domain hass-example.duckdns.org. Don’t worry about setting your IP addresses yet.
Click install at the top, for their guide. Under Operating Systems, select pi and then choose the domain you’re using for your DNS.
SSH into your pi.
Enter the following commands
mkdir duckdns This will make a directory for the duckdns script.
cd duckdns Change to the duckdns directory.
nano duck.shcreate the duck dns script. Note that BRUH and DuckDNS use vi. This is stupid and more complicated than necessary.
Copy the line of code from the install page and paste it into the empty file (If you are using putty, copy the text then right-click in putty to paste). It should look like this:
echo url=“https://www.duckdns.org/update?domains=hass-example&token=a836a8s-1337-892c-a495-ef817a92&ip=” | curl -k -o ~/duckdns/duck.log -K -
Press Ctrl-X, Y, then ENTER to save the file.
type chmod 700 duck.sh which lets the script run.
run crontab -e. Copy the following text and paste it into the bottom of the crontab file. */5 * * * * ~/duckdns/duck.sh >/dev/null 2>&1
Press Ctrl-X, Y, ENTER
Run the script by typing ./duck.sh
Check the script by typing cat.duck.log If it worked, You should see OK on the same line as pi@raspberrypi:~/duckdns $. If it says KO, something didn't work right! Check your work and try again. type cd to return to the root directory. Make sure everything is working by connecting your duckdns domain on port 8123, such as http://hass-example.duckdns.org:8123. Remember to try while outside of your local network. If everything worked, Home Assistant should load! Let's Encrypt! Home-assistant.io guide Bruh-automation guide (with video) Open your router configurations again. You need to forward Port 80 to Port 80 on your raspberry pi, as well as port 443 to port 443 on your pi. This is to verify the certificate. Connected to your pi through SSH, run the following commands mkdir certbot to make a directory for the SSL certification bot cd certbot to change to that directory wget https://dl.eff.org/certbot-auto to download the certbot chmod a+x certbot-auto to allow the certbot to run. Edit and paste in the following command to run the certbot. You need to put in your real email address and your duckdns domain. ./certbot-auto certonly --standalone --standalone-supported-challenges http-01 --email [email protected] -d YOURSUBDOMAIN.duckdns.org Answer the questions as it poses them to you. You should get a confirmation that everything went well and that the certificates are in the /etc/letsencrypt directory. Remove the port 443 and port 80 forwarding from your router. Type sudo chmod -R 777 /etc/letsencrypt to allow homeassistant to read the certificates. in configuration.yaml, you need to add thessl-certificate:andssl_key:lines underhttp: It should look like this
http:
api_password: YOUR_SECRET_PASSWORD
ssl_certificate: /etc/letsencrypt/live/hass-example.duckdns.org/fullchain.pem
ssl_key: /etc/letsencrypt/live/hass-example.duckdns.org/privkey.pem
Try to access https://YOURDOMAIN.duckdns.org:8123 from outside of your local network. (note that is httpS, not http). If it connects, you did good!
Push Notifications
Almost there!
Terrible Guide
Missing Files Information
Google Verification Information
While connected to Home assistant, under developer tools click the first icon, services
In domain, type homeassistant and in service, type stop
Click call service. This will shut down home assistant temporarily.
type source /srv/homeassistant/homeassistant_venv/bin/activate to activate the Home Assistant virtual environment.
type sudo apt-get install libpython-dev libffi-dev libssl-dev to download necessary packages
Type pip uninstall pywebpush to uninstall an old version, if it is even there.
Type pip install “https://github.com/web-push-libs/pywebpush/archive/e743dc92558fc62178d255c0018920d74fa778ed.zip#pywebpush==0.5.0” to update the package.
When that finishes, type sudo reboot to restart your pi. Log back in to SSH in a few minutes.
Meanwhile, go to https://console.cloud.google.com/home/dashboard and create a new project.
Go to https://console.cloud.google.com/apis/credentials/domainverification, add your domain as https://YOURDOMAIN.duckdns.org:8123 YOU MUST INCLUDE THE PORT.
It will have you verify ownership. Choose “Other” for your domain registrar. Choose Alternate methods, then HTML tag.
On your SSH connection, type sudo nano /srv/homeassistant/homeassistant_venv/lib/python3.4/site-packages/homeassistant/components/frontend/templates/index.html
Copy the meta tag from the domain verification website. It should look like
In your SSH Shell, move the cursor past the tag. press ENTER then right-click to paste the meta tag.
Press Ctrl-X, Y, ENTER to save
Go back to the verification website and click verify. This should verify your ownership of the domain! Go back to https://console.cloud.google.com/apis/credentials/domainverification and verify that the domain is added. If not, add it again. Since it is already verified, you won’t get forwarded to another site.
Go to https://console.firebase.google.com/ and import the project you created.
Click the cogwheel in the upper-left, and select Project Settings
Select the Cloud messaging tab. the server key is your gcm_sender_key and the sender ID is your gcm_sender_id.
Add the following to your configuration.yaml, with the proper information added from firebase added. note that both keys must be encapsulated in single quotes. The API key is long, while the sender ID is short in comparison.
notify:
platform: html5
gcm_api_key: ‘gcm-sender-key’
gcm_sender_id: ‘gcm-sender-id’
Restart homeassistant (either sudo reboot and wait, or do it from Developer services in the Home Assistant interface).
You should now see a notifications slider in the sidebar on the left side. Turn it to the on position
You should get a prompt to allow notifications. Click Allow.
Go to the developer services. for domain, enter notify. for service, choose html5. For service data, enter {“title”:“Test”, “message”:“This is a test”}
In a few seconds you should receive a notification from Home Assistant!
As you add devices, it is a good idea to name them open the html5_push_registrations.conf file in your configuration directory and rename any unnamed device to whatever you wish to name them. Don’t make any other changes to this file.
You’re good to go! Read the usage notes and good luck notifying!

4 Likes

This was already cross-posted to the forum.

3 Likes