[HowTo] Push notifications from OSMC using Pushover

Intro
This post will step through how to configure OSMC to send push notifications to your device (iOS, Android, Desktop) based on a range of triggers. There are many ways you could use this script, I’ve included a couple of examples.

All steps below should work on any system, but they have been tested on a Raspberry Pi 2 running OSMC 2015.07-1.

1. Register for Pushover

  • Register for a Pushover account at www.pushover.net. You get a 7 day trial for free, after that there is a one-off cost of $4.99 USD per receiving platform.
  • Install the Pushover app on your receiving platform of choice.
  • Create a new Application within the Pushover web interface. You can call the app whatever you like but “OSMC” makes sense. I use this icon for the app to keep it looking consistent:
  • Make a note of your User Key and your newly created application’s API Key – you will need
    these to send notifications.

2. Create the notification script

Create a file /home/osmc/push.sh with the following content:

#!/bin/bash

# Uses the Pushover API to send the first command line parameter as a push notification

curl -s \
  --form-string "token=YOUR_APPLICATION_API_KEY" \
  --form-string "user=YOUR_USER_API_KEY" \
  --form-string "message=$1" \
  https://api.pushover.net/1/messages.json > /dev/null
  • Insert your own User Key and Application API Key from above.
  • Make the script executable by running chmod +x /home/osmc/push.sh

It is possible to send more complicated notifications than a simple message. Read the Pushover API documentation for details.

3. Test the notification script
The script will take the first command line parameter passed to it and send it out as a
notification. To test the script, run

/home/osmc/push.sh “YOUR NOTIFICATION IN HERE”

Note the quotes around the notification text. You should receive the notification you typed on your device. Make sure this is working before you set up any other triggers.

4. Set up some triggers
Now you have a working push script, you can set up triggers to notify you on whatever events you choose. Two examples are below.

4.1 Push a notification when OSMC upgrades packages

This will notify when packages have been upgraded within the system using apt. (I realise that technically, this trigger will fire on other dpkg actions, but for the majority of users the only time dpkg is executed is when the system is upgrading packages).

Create the file /etc/apt/apt.conf.d/99-post-update with the following content:

Dpkg::Post-Invoke {"/home/osmc/push.sh 'Packages have been upgraded'";};

When the system upgrades packages you should get a notification.

4.2 Push a notification when a new video is added to your media directory
This will notify when a new file is added to a directory of your choosing (including sub-directories).

First, install the inotify package:

sudo apt-get install inotify-tools

Create a new file /home/osmc/folderwatch.sh with the following content:

#!/bin/bash
# Pushes a notification when a file is created in WATCHDIR
# The test for a file is that it has an extension ("something.anything")
# Neeeds inotify-tools package installed
# Uses separate script push.sh to interact with Pushover API
# Sleeps for 5 seconds after each notification to avoid flooding on multiple small file creation

WATCHDIR="/path/to/your/watch/directory"

echo "Starting folderwatch..."

while true

  do

  inotifywait -q -r -e create --format %f "$WATCHDIR" |

  while read FILE; do

    if [[ $FILE == *.* ]]; then

      MESSAGE="${FILE%.*} is ready to watch"
      /home/osmc/push.sh "$MESSAGE"
      echo "Push notification sent: $MESSAGE"
      sleep 5
    fi
  done
done

Make this script executable:

chmod +x /home/osmc/folderwatch.sh

Create a new file /etc/systemd/system/folderwatch.service with the following content:

[Unit]
Description=Watch a folder and notify when files are created

[Service]
Type=simple
ExecStart=/home/osmc/folderwatch.sh | systemd-cat -t folderwatch

[Install]
WantedBy=multi-user.target

Start the service by running:

sudo systemctl daemon-reload
sudo systemctl enable folderwatch    
sudo systemctl start folderwatch

Copy a video file into your watch directory and you should get a notification sent to your
device.

4 Likes

Thanks for your work.
Can we add more than one path in the dir watcher ?

I don’t know as I haven’t tried.

A quick Google suggests that you can, just add two folder paths as arguments when launching inotifywait instead of one.

Another way to do it would be to duplicate the process above so you have two services running, one for each directory you want to watch. But I suspect the first option would be more efficient. Let us know if you get it to work.

Having now tested it across two OSMC monthly updates, here’s another example:

4.3 Push notification when OSMC has a version upgrade

Create a new file /home/osmc/updatecheck.sh with the following content:

#!/bin/bash

# Check if OSMC has just been updated
OLDVER=$(cat /home/osmc/versionid.txt)
NEWVER=$(cat /etc/os-release | grep VERSION_ID | awk -F\" '{print $(NF-1)}')

echo "Checking if OSMC has been updated..."

if [ "$OLDVER" != "$NEWVER" ]; then
  echo "OSMC has been updated, pushing notification..."
  /home/osmc/push.sh "OSMC has been updated to $NEWVER"
  echo $NEWVER > /home/osmc/versionid.txt
  exit 0
fi

echo "No update detected, current version is $NEWVER"

Option 1 - Create a new service that fires on startup

Create a new file /etc/systemd/system/updatecheck.service with the following content:

[Unit]
Description=Run the updatecheck script once on startup after networking is up
After=network-online.target

[Service]
Type=oneshot
ExecStart=/home/osmc/updatecheck.sh | systemd-cat -t updatecheck

[Install]
WantedBy=multi-user.target

Start the service by running:

sudo systemctl daemon-reload
sudo systemctl enable updatecheck
sudo systemctl start updatecheck

Option 2 - Hook onto the mediacenter service

Create the file /etc/systemd/system/mediacenter.service.d/local.conf with the following contents:

[Service]
ExecStartPre = /home/osmc/updatecheck.sh | systemd-cat -t updatecheck

You should now get a push notification when the OSMC version is updated.

And don’t forget to :

sudo chmod +x  /home/osmc/updatecheck.sh

:smiley:

Yes, missed that :smile: