Squeezebox Audio Alert Script

I wanted to share a squeezebox script I wrote that allows a locally stored mp3 file to be played on a squeezebox music player via a home assistant automation. (See post 20 below for an updated script that uses the home assistant TTS component to play the audio, rather than using a local mp3 file). It allows setting an “alert volume” and also supports resuming previously playing music after the alert is done playing. It also restores the previous volume, repeat setting and on/off state of the player.

Note, in the example below, I used an online tool to merge two mp3 files together… the first part of the mp3 file is a beep sound and I merged that mp3 with audio generated from an online TTS website (So the audio will beep and then say “The basement door is open”). This was just to simplify needing to handle multiple mp3 files in the script.

This script was written before the TTS component was added, which offers similar functionality. However, I don’t believe the current squeezebox support via the media_player component allows resuming of previously playing music. Note, it isn’t necessary to add a squeezebox platform under the media_player component for my script to work.

Note, you will need to edit the “user”, “pass”, ip and port of your squeezebox music server on the following line in the squeezebox_alert.sh file (remove “user:pass@” if you don’t use LMS password protection):
JSONRPC=“http://user:[email protected]:9000/jsonrpc.js

Here is an example of using the script in home assistant. The script takes 3 parameters. The MAC of your squeezebox player, the full path to the mp3 file you want to play, and the alert volume:

automation:
  - alias: "Play kitchen alert on gate opened"
    trigger:
      - platform: state
        entity_id: sensor.gate
        to: 'open'
    action:
      - service: shell_command.play_gate_opened_alert_in_kitchen
      
  - alias: "Play bedroom alert on gate opened"
    trigger:
      - platform: state
        entity_id: sensor.gate
        to: 'open'
    action:
      - service: shell_command.play_gate_opened_alert_in_bedroom
	  
  - alias: "Play kitchen alert on basement door opened"
    trigger:
      - platform: state
        entity_id: sensor.basement_door
        to: 'open'
    action:
      - service: shell_command.play_basement_door_opened_alert_in_kitchen
      
  - alias: "Play bedroom alert on basement door opened"
    trigger:
      - platform: state
        entity_id: sensor.basement_door
        to: 'open'
    action:
      - service: shell_command.play_basement_door_opened_alert_in_bedroom

shell_command:
  play_gate_opened_alert_in_kitchen:          bash /volume1/homeassistant/squeezebox_alert.sh "00:04:20:01:02:03" "//volume1//homeassistant//mp3//beep_the_gate_is_open.mp3" 65

  play_gate_opened_alert_in_bedroom:          bash /volume1/homeassistant/squeezebox_alert.sh "00:04:20:04:05:06" "//volume1//homeassistant//mp3//beep_the_gate_is_open.mp3" 65

  play_basement_door_opened_alert_in_kitchen: bash /volume1/homeassistant/squeezebox_alert.sh "00:04:20:01:02:03" "//volume1//homeassistant//mp3//beep_the_basement_door_is_open.mp3" 65

  play_basement_door_opened_alert_in_bedroom: bash /volume1/homeassistant/squeezebox_alert.sh "00:04:20:04:05:06" "//volume1//homeassistant//mp3//beep_the_basement_door_is_open.mp3" 65

squeezebox_alert.sh:

#!/bin/bash

#NOTE: Edit "user", "pass", ip and port below
JSONRPC="http://user:[email protected]:9000/jsonrpc.js"

#Example command arguments
#mac="00:04:20:01:02:03"
#alert_song="//volume1//homeassistant//beep.mp3"
#alert_volume=60

mac=$1
alert_song=$2
alert_volume=$3

#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
prev_mode=$(curl -X GET -H "Content-Type: application/json" \
                 -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["mode","?"]]}' \
                 $JSONRPC | jq '.result._mode')
echo "prev_mode=$prev_mode"

if [[ $prev_mode =~ .*play.* ]] ; then
  # pause any currently playing song
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["pause"]]}' $JSONRPC
  echo "pause"

  #May want to pause here before playing alert song
  #sleep 1
fi

# 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"

#get number of tracks
num_tracks=$(curl -X GET -H "Content-Type: application/json" \
                  -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","tracks","?"]]}' \
                  $JSONRPC | jq '.result._tracks')
echo "num_tracks=$num_tracks"

prev_time=0
prev_index=0

# IF MUSIC IS PLAYING, SAVE OFF INFO TO RESUME LATER
if [[ $prev_mode =~ .*play.* ]] ; then
  echo "music is playing"

  # 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"

  #get current song index
  prev_index=$(curl -X GET -H "Content-Type: application/json" \
                   -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","index","?"]]}' \
                   $JSONRPC | jq '.result._index')
  echo "prev_index=$prev_index"

  #add alert song to end
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","add","'"$alert_song"'"]]}' $JSONRPC
  echo "add alert song to end"

  #jump to and play alert song
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","index",'"$num_tracks"']]}' $JSONRPC
  echo "jump to and play alert song"

else
  #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"
fi

# 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"

#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

# RESUME PREVIOUSLY PLAYING MUSIC
elif [[ $prev_mode =~ .*play.* ]] ; then
  # delete alert song from playlist
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","delete",'"$num_tracks"']]}' $JSONRPC
  echo "delete alert song from playlist"

  #jump to and play prev_index
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$mac"'",["playlist","index",'"$prev_index"',1]]}' $JSONRPC
  echo "jump to and play prev_index"

  #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
8 Likes

Hey nice work! Can you format this so that it shows the code properly and then I will move it to “Share Your Projects”.

Select your code blocks in the post editor and then press the preformatted text button as indicated in the image below.

Thanks!

1 Like

This seems really nice, I will try this for sure.
Can maybe replace the TTS functionality I was after since I can´t get that to work with SB

I have TTS working with SB. Here are my relevant configuration.yaml entries. Note, I am running home assistant and LMS on a synology NAS.

tts:
  - platform: google
  
media_player:
  - platform: squeezebox
    host: 192.168.1.100
    #port: 9000
    #username: user
    #password: !secret squeezebox_password

automation:
  - alias: "TTS Test"
    trigger:
      - platform: state
        entity_id: switch.lamp
        to: 'on'
    action:
      - service: tts.google_say
        entity_id: media_player.kitchen
        data:
           message: 'Testing. Testing. 1, 2, 3.'

Hi
Do you use SSL?

The problem seems to be SSL + TTS + SB

Oh OK, yeah I don’t use SSL.

@smazman:
What version of LMS are you using?

I have 7.9 and get a lot of

squeezebox_alert.sh: line 18: jq: command not found

when running the shell script

I am using LMS 7.9 as well. You need to install jq (“sudo apt-get install jq” or something similar). I think it was already installed on my Synology.

Found this page:
https://stedolan.github.io/jq/download/

Hi
I installed jq and now it works pretty well.

It clears the current playlist and play the alert sound.
My only problem is that it is not restoring the playlist after the alert is done.

@frelev, I’m glad to hear it is working. So are you seeing that it does not restore the playlist only if a song was NOT playing previously on the squeezebox? (NOTE: It should restore the playlist if a song was playing previously) If so, this was my intended behavior. I added album art to my mp3 so it shows an image representative of the audio alert on the squeezebox radio (such as an image of an open door)… on squeezeboxes without a color display, it will at least show the song title of the last alert to again provide an easy way to see the last event… like in the case where you heard an alert, but didn’t hear the specifics, you can look at your squeezebox. If I restore the playlist immediately, it will delete the alert song from the playlist and you won’t get this visual indication. I was thinking of having it wait a few seconds before removing the alert song from the playlist, but I thought this might hurt performance of the script, so decided I didn’t care if it cleared out my current playlist.

See below for an updated version of the script that will keep your current playlist (and will immediately remove the alert song from the playlist after it is done playing). Note, I haven’t tested this… you may need to move the “#restore prev_power setting” if block to the end of the script… not sure. Let me know if it works.

[NOTE: I removed this script… use the script in post 15 below if you want to preserve current playlist]

1 Like

Wouldn’t it be possible to make one script that reads the playlist and a second one that “writes” it? So it could use it with the build in tts features (eg. variable text,…)

Yeah, I think the audio files from the TTS component are stored in a cache folder, so if you knew the path for the file you could pass it in to this script.

OK! Then I understand better. It´s by design.

I tried your new script and it is not fully working for me.
Hard to explain but here is a try:

New script as-is:
It inserts the track after current track but start to play last track in playlist instead.
If current track is the last it seems to work but it restores the playlist in reverse?

If I move the part to the end I get an error:
“squeezebox_alert.sh: line 148: [: “1”: integer expression expected” but the script run.
It only plays the alert track if the last music track in the playlist is current track.
It doesn’t remove the track however

If a track in the middle of the list is current track it inserts the alert after but doesn´t play it. It plays the last track in playlist instead.

@smazman: I use variable Texts (containing temperatur etc). So the name changes and i’m not so fit in programming to mod the existing tts engine. But if i try to split your bash-skript in two parts i think it wouldn’t work because the playlist-info is lost, isn’t it?

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
3 Likes

@smazman, this is great!

Seems to work fine now

I need some help: I would like to know how you integrate this in your ha. Do you all only use predefined alarms? I would like to use it like I do it with the “normal” tts with daily changing text (with temperature, humidity, etc) but I don’t understand how to do this with this script.

@mrMuppet, I’m not sure how to do this either. I am currently just using pre-defined audio alerts. I was hoping the TTS component saved the mp3 file in the cache directory with a file name that is equal to the text string (for example, “the_temperature_is_70_degrees.mp3”)… but this doesn’t seem to be the case. There has to be some way to get the last file name from the TTS component. Worst case you could pre-save a hundred or so mp3 files locally with every possible degree and humidity value. :slight_smile: The ideal solution would probably be for the media player component to handle alerts (saving and resuming the current playlist)… but this will be very player dependent so it won’t be easy to implement.

I wonder if this would work… use a chromecast audio (or some other source) just for TTS audio alerts. Connect both the chromecast audio and your squeezebox to the same audio input on your receiver via an audio splitter (Y) cable. You could pause the music on your squeezebox… then play the TTS alert on the chromecast… and then resume the music on the squeezebox. Not elegant, but I think it would work.

The solution i still think of is having a way to save the playlist and song info temporarily in a file. Than i could save it, play via normal TTS and then restore playlist-info from the file…

@mrMuppet: You mean like this?

I updated the script so it will save the settings file (player_name.txt) in the same directory as the script files. See the configuration.yaml example below for an example of how to call the scripts.

NOTE: As before, you will need to update the “JSONRPC=” line in both of the script files.


Example configuration.yaml entries (not complete)

automation:
  - alias: "Play lamp on audio alert"
    trigger:
      - platform: state
        entity_id: switch.lamp
        to: 'on'
    action:
      - service: homeassistant.turn_on
        entity_id: script.play_light_on_alert_in_kitchen

shell_command:
  playlist_save_kitchen:    bash /volume1/homeassistant/squeezebox_alert_save_playlist.sh "kitchen" "00:04:20:01:02:03" 65
  playlist_restore_kitchen: bash /volume1/homeassistant/squeezebox_alert_restore_playlist.sh "kitchen" "00:04:20:01:02:03"

script:
  play_light_on_alert_in_kitchen:
    alias: "Play light alert"
    sequence:
      - service: shell_command.playlist_save_kitchen
      - service: tts.google_say
        entity_id: media_player.kitchen
        data:
           message: 'The lamp is on.'
      - service: shell_command.playlist_restore_kitchen

squeezebox_alert_save_playlist.sh

#!/bin/bash

#squeezebox_alert_save_playlist.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_volume=60

playlist_name=$1
mac=$2
alert_volume=$3

#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"

#write settings to file
DIRECTORY=$(cd `dirname $0` && pwd)
echo "Save settings to file: $DIRECTORY/$playlist_name.txt"
> "$DIRECTORY/$playlist_name.txt"
printf '%s\n%s\n%s\n%s\n%s\n' $prev_power $prev_playmode $prev_time $prev_volume $prev_repeat > "$DIRECTORY//$playlist_name.txt"

# 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"

---
squeezebox_alert_restore_playlist.sh
---

#!/bin/bash

#squeezebox_alert_restore_playlist.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"

playlist_name=$1
mac=$2

# 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"

# read settings from file
DIRECTORY=$(cd `dirname $0` && pwd)
echo "Restore settings from file: $DIRECTORY/$playlist_name.txt"
IFS=$'\n' read -d '' -r -a lines < "$DIRECTORY/$playlist_name.txt"
prev_power="${lines[0]}"
prev_playmode="${lines[1]}"
prev_time="${lines[2]}"
prev_volume="${lines[3]}"
prev_repeat="${lines[4]}"
echo "prev_power=$prev_power"
echo "prev_playmode=$prev_playmode"
echo "prev_time=$prev_time"
echo "prev_volume=$prev_volume"
echo "prev_repeat=$prev_repeat"

noplay=1
if [ $prev_playmode -eq 1 ] ; then
  noplay=0
fi

# 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
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"

# 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
7 Likes