Staying safe – Sending notifications and alarms to your smartphone with Pushover

Published by Oliver on

If anything goes wrong in your smart home (or with your server) it would generally be great to get notified as soon as possible. Sending notifications with the versatile Pushover service is a simple way to do this. Here is how you can also make your smart home text you.

Knowing when something goes wrong

The idea of a smart home is that it quietly takes care of everything in the background. Once everything is set up properly like I did with my smart home server and Home Assistant installation there is usually no need for any more interaction with the actual server.

There are some emergencies though where quick reactions from my side are needed. A alarm caused by my water leak sensor is not that useful if I do not see it because I am not at home. Noticing a failed backup job weeks later while manually checking can result in a loss of data and me not noticing this blog going down or my Nextcloud failing also warrant quick action from my side.

Fortunately all of these cases can be solved by (near) instant messages to a smartphone. It is the one piece of tech you will take with you pretty much all the time. Fortunately there is a simple solution for implementing such messages.

Sending messages to your phone

There are a lot of services that resolve around sending notifications to your phone or other devices. WhatsApp & co are on most phones already but usually do not offer an API for you to send messages. Slack, Teams & co do but they are targeted more at professionals and organisations and the setup can be tricky.

Home Assistant itself offers support for notifications but I have not exposed my Home Assistant setup to the Internet so this will not work outside. It is also a limited solution that can not be used directly from a server for example.

Instead I started using a service that is built for this: Pushover. It supports iOS and Android as well as Desktop devices and has a bunch of really nice integrations with other services (including Home Assistant and OpenHab). You can test it for free for some time, then you can buy it for a reasonable price of 5$ per device type. That’s a one time purchase, no subscription!

How to use Pushover to send notifications

To start using Pushover just create an account and download either one of the Apps or the desktop integration. Then you should be ready to receive messages on your device. You can check if it works by using the test feature from their website.

Now you are ready to set up your integrations to send messages automatically. I use this in a couple of different ways.

Pushover and Home Assistant

Home Assistant supports Pushover with an integration. The setup process is simple but needs to be done via the configuration.yaml file. I would suggest adding your API key and client token to your secrets.yaml file first. You can get the application key by creating a new application in Pushover here.

pushover_ha_api_key: abc123
pushover_user_key: def456

Then you can add a configuration like this to your main configuration.yaml file to connect Home Assistant to Pushover.

# pushover
notify:
  - name: Pushover
    platform: pushover
    api_key: !secret pushover_ha_api_key
    user_key: !secret pushover_user_key

That’s it! Now you can start sending messages from any automations in Home Assistant. I use it mainly for an alert in case of a water leak. If a leak is detected I send an alarm to Home Assistant but also directly to my phone via Pushover.

notifications from my smart home for a water leak alarm - done in home assistant
Water leak alarm

The alarm is sent with the highest priority so that my phone will instantly show it even during quiet hours. Message and title are in German here but of course can be customized. You could also use a custom sound if you want to.

The whole automation is pretty simple and based on a Aqara water sensor and ZHA. Here is the full code.

alias: Wasser Alarm
 description: ''
 trigger:
 type: moist
 platform: device
 device_id: acc398b497e0667b2189ef8f431a5d97
 entity_id: binary_sensor.lumi_lumi_sensor_wleak_aq1_ias_zone
 domain: binary_sensor
 condition: []
 action:
 service: notify.pushover
 data:
   message: Der Wassersensor in der Küche hat angeschlagen!!
   title: Wasser in der Küche!
   data:
     priority: 1
 service: notify.persistent_notification
 data:
   message: Der Wassersensor in der Küche hat angeschlagen!!
   title: Wasser in der Küche!
 mode: single 

Watchtower and Pushover

On my smart home server I use docker-compose to manage all the running software, including Home Assistant. To keep everything up to date automatically everything but a few select services are monitored by a software called Watchtower. I described that part in-depth here.

Basically watchtower scans all running containers, searches for updates and automatically applies them. Then it restarts the container. Automatic updates are great and save me a lot of time but I still want to know when something changes. That is when notifications come in handy again.

Watchtower supports notifications out of the box. Via their shoutrrr library support for Pushover is also included. In my guide I used email support and send an email to a generated email adress from Pushover, allowing them to forward it to my phone as a notification (another nice feature). Then I discovered the direct support and switched to that.

The configuration for Pushover support in Watchtower is pretty simple. Start by getting a (new) application token and your userkey. I added those to my .env file.

#pushover
PO_USERKEY=abc123
PO_TOKEN=def456

Then you simply have to add some environment variables to your Watchtower container like this:

watchtower:
     container_name: watchtower
     image: containrrr/watchtower
     volumes:
       - /var/run/docker.sock:/var/run/docker.sock
     environment:
       - TZ=${TZ}
       - WATCHTOWER_CLEANUP=true # optional - deletes old images to safe some space
       - WATCHTOWER_NOTIFICATIONS=shoutrrr
       - WATCHTOWER_NOTIFICATION_URL=pushover://shoutrrr:${PO_TOKEN}@${PO_USERKEY}

This will send all messages from watchtower to my phone, keeping me notified of any changes.

Watchtower notification on my phone

Message from the command line – checking updates

I operate my own server which provides some services like a Nextcloud instance to my family and me. As it handles some important data there are of course several layers of backups. The server uses the ZFS file system and regularly creates snapshots. These snapshots are regularly cleanup up and synced to an external drive (I will describe this in more details in a future post) by a script started by a cron job.

Recently while working on something else on the server I noticed in the logs that these backups had stopped some time ago most likely due to someone moving the USB cable too far. Of course a missing backup can have grave consequences. To avoid this in the future I started to work on a new script that regularly tests the updates and sends and alarm if any are missing.

#!/bin/bash

# Backup-Pools to check the status of
BACKUPPOOLS=("backupPool" "backupPool2" "backupPool3" "backupPool4")

# needed paths
LOGFILE="/var/log/backupCheck.log"
ZFS="/sbin/zfs"

# pushover data
PO_TOKEN=abc123
PO_UK=def456

# -------------- program, don't change ---------------
hasRecentBackups=false

for BACKUPPOOL in ${BACKUPPOOLS[@]}
do
	isOnline=$(/sbin/zpool status $BACKUPPOOL | grep -i 'state: ONLINE' | wc -l)

	if [ $isOnline -ge 1 ]
	then
		echo "$(date) - Found online pool $BACKUPPOOL" >> $LOGFILE
		# find and compare latest snapshot to today
		lastestSnapshotDate=$($ZFS list -t snapshot -o name -s creation -r $BACKUPPOOL | tail -1 | egrep -o '[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}-[[:digit:]]{4}')
		timeAtStartOfTheDay=$(date +"%Y-%m-%d"-0000)
		echo "$(date) - Lastest date from the pool $BACKUPPOOL are from $lastestSnapshotDate and today started at $timeAtStartOfTheDay" >> $LOGFILE

		if [[ "$lastestSnapshotDate" > "$timeAtStartOfTheDay" ]] ;
		then
		    echo "Backups for $BACKUPPOOL are up to date" >> $LOGFILE
		    hasRecentBackups=true
		else
			echo "Backups for $BACKUPPOOL are not up to date. Last one is from $lastestSnapshotDate" >> $LOGFILE
		fi
	fi
done

if [ "$hasRecentBackups" = true ]; then
  	echo "$(date) - Found recent backups. Run finished" >> $LOGFILE
else
	echo "$(date) - Alarm - no recent backups found!!" >> $LOGFILE
	curl -s -F "token=$PO_TOKEN" \
    -F "user=$PO_UK" \
    -F "title=No recent backups!" \
    -F "message=Unable to find recent backups on the server. Last one is from $lastestSnapshotDate" https://api.pushover.net/1/messages.json
fi

The contents of the script is not really that important. Most of it is simply getting the date from the name of the latest ZFS snapshot and checking if it is from today. You need to adapt this to your use case. The important part is the curl command at the end of the file that is used to send the alarm notification.

As you can see a simple post to the https://api.pushover.net/1/message.json endpoint is enough to send a message. Make sure to provide your own user key and application token similar to the other alarms above.

These are just a couple of cases where notifications can be really really useful. Using these examples you can apply them to whatever you are trying to do. You are also not limited to one receiving device, multiple devices are totally possible.