Here is a script which uses “playlist save” and “playlist resume” (rather than trying to add the alert song to the end of the playlist and then removing it once it stops playing). I did some testing on this script and it seems to work pretty good. Let me know if you see any issues.
NOTE: This script takes an additional argument “playlist_name” as the first argument. You will want this to be a different value for each player, so you could just pass in the player name. It also takes an optional parameter to not restore the playlist (restore_playlist=0 only works if music was previously stopped… see comments in script below for details).
#!/bin/bash
#squeezebox_alert.sh
#NOTE: Edit "user", "pass", ip and port below
JSONRPC="http://user:[email protected]:9000/jsonrpc.js"
#Example command arguments
#playlist_name="bedroom"
#mac="00:04:20:01:02:03"
#alert_song="//volume1//homeassistant//mp3//beep.mp3"
#alert_volume=60
#NOTE: restore_playlist is optional.(defaults to 1)
# restore_playlist=0 only works if music was previously stopped
#restore_playlist=0
playlist_name=$1
mac=$2
alert_song=$3
alert_volume=$4
restore_playlist=${5:-1}
echo "restore_playlist=$restore_playlist"
#get power state
power=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["power","?"]]}' \
$JSONRPC | jq '.result._power')
prev_power=0
if [[ $power =~ .*1.* ]] ; then
prev_power=1
fi
echo "prev_power=$prev_power"
#get play mode
mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mode","?"]]}' \
$JSONRPC | jq '.result._mode')
prev_playmode=0
if [[ $mode =~ .*play.* ]] ; then
prev_playmode=1
fi
echo "prev_playmode=$prev_playmode"
noplay=1
prev_time=0
if [ $prev_playmode -eq 1 ] ; then
noplay=0
# pause currently playing song
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["pause"]]}' $JSONRPC
echo "pause currently playing song"
# get paused time
prev_time=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["time","?"]]}' \
$JSONRPC | jq '.result._time')
echo "prev_time=$prev_time"
fi
# save current playlist
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","save","'"$playlist_name"'","silent:1"]]}' $JSONRPC
echo "save current playlist"
# GET SETTINGS TO RESTORE AFTER PLAYING ALERT SONG
#get current volume
prev_volume=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume","?"]]}' \
$JSONRPC | jq '.result._volume')
echo "prev_volume=$prev_volume"
#get current repeat setting
prev_repeat=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat","?"]]}' \
$JSONRPC | jq '.result._repeat')
echo "prev_repeat=$prev_repeat"
# SET SETTINGS FOR ALERT SONG
#set alert_volume to command argument value
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume",'$alert_volume']]}' $JSONRPC
echo "set alert_volume"
#set repeat setting to 0
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat",0]]}' $JSONRPC
echo "set repeat_setting to 0"
#play alert song (this will clear current playlist)
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","play","'"$alert_song"'"]]}' $JSONRPC
echo "play alert song"
# WAIT FOR ALERT SONG TO STOP PLAYING
echo "wait for alert song to stop playing"
cur_mode="play"
while [[ $cur_mode =~ .*play.* ]]; do
sleep 1
cur_mode=$(curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mode","?"]]}' \
$JSONRPC | jq '.result._mode')
done
echo "alert song stopped playing"
# RESTORE PREVIOUS SETTINGS
#restore prev_volume setting
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mixer","volume",'"$prev_volume"']]}' $JSONRPC
echo "restore prev_volume setting"
#restore prev_repeat setting
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","repeat",'"$prev_repeat"']]}' $JSONRPC
echo "restore prev_repeat setting"
# resume previous playlist (always resume if previously playing)
if [ $prev_playmode -eq 1 ] || [ $restore_playlist -eq 1 ] ; then
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","resume","'"$playlist_name"'","noplay:'$noplay'"]]}' $JSONRPC
echo "resume previous playlist"
fi
# RESUME PREVIOUSLY PLAYING MUSIC
if [ $prev_playmode -eq 1 ] ; then
#skip ahead in song to prev_time
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["time",'"$prev_time"']]}' $JSONRPC
echo "skip ahead in song to prev_time"
fi
#restore prev_power setting
if [ $prev_power -eq 0 ] ; then
echo "prev_power setting was off, power off"
curl -X GET -H "Content-Type: application/json" \
-d '{"id":1,"method":"slim.request","params":["'"$mac"'",["power",0]]}' $JSONRPC
fi