Squeezebox Audio Alert Script

@smazman Hey thanks for the addon, Awesome!

But i’m having an issue got it installed and running no problem but the audio stops for the tts and speaks and then doesn’t restore. I do not see the *.txt file. Where is it created under the /config? or somewhere else?

here is the last bit in the logs from the addon I changed my mac address to 00:00:00:00:00 just for the post :slight_smile:

alert song stopped playing
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   186  100    99  100    87     99     87  0:00:01 --:--:--  0:00:01  181k
{"params":["00:00:00:00:00",["mixer","volume","87"]],"method":"slim.request","id":1,"result":{}}restore prev_volume setting
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   190  100   101  100    89    101     89  0:00:01 --:--:--  0:00:01  185k
{"params":["00:00:00:00:00",["playlist","repeat","0"]],"method":"slim.request","id":1,"result":{}}restore prev_repeat setting
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   222  100   117  100   105    117    105  0:00:01 --:--:--  0:00:01  1620
{"method":"slim.request","params":["00:00:00:00:00",["playlist","resume","office","noplay:0"]],"result":{},"id":1}resume previous playlist
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   190  100   102  100    88    102     88  0:00:01 --:--:--  0:00:01  185k
{"params":["00:00:00:00:00",["time","104.44559410286"]],"method":"slim.request","id":1,"result":{}}skip ahead in song to prev_time
input={"playlist_name": "office", "mac": "00:00:00:00:00", "alert_volume": 65}

Did you try adjusting (increasing) the delay in the yaml file as I mentioned in my last post?

Note for the addon the *.txt files are not used to save the settings. (Not needed)

Yes I saw in the script is calling the settings then trying to apply them. the issue seems to be when its trying to resume the same state

{"id":1,"result":{},"method":"slim.request","params":["00:01:02:03:04:05",["playlist","resume","office","noplay:0"]]}resume previous playlist
noplay

what is that for?
There seems to be no data for

{"params":["00:01:02:03:04:05",["playlist","repeat","0"]],"method":"slim.request","id":1,"result":{}}restore prev_repeat setting

But there was no repeat set so that is fine right?

Here is all that was captured do you see what I can try to fix?

[Info] Read alert_volume: 90
restore_playlist=1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   101  100    77    101     77  0:00:01 --:--:--  0:00:01  173k
prev_power=1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
prev_playmode=1
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   158  100    85  100    73     85     73  0:00:01 --:--:--  0:00:01  154k
{"method":"slim.request","id":1,"result":{},"params":["00:01:02:03:04:05",["pause"]]}pause currently playing song
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   188  100   112  100    76    112     76  0:00:01 --:--:--  0:00:01  183k
prev_time=16.7563521347046
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   254  100   151  100   103    151    103  0:00:01 --:--:--  0:00:01  1968
{"params":["00:01:02:03:04:05",["playlist","save","office","silent:1"]],"method":"slim.request","id":1,"result":{"__playlist_id":54569,"writeError":1}}save current playlist
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   198  100   112  100    86    112     86  0:00:01 --:--:--  0:00:01 39600
prev_volume="70"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   203  100   114  100    89    114     89  0:00:01 --:--:--  0:00:01 40600
prev_repeat="0"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   184  100    99  100    85     99     85  0:00:01 --:--:--  0:00:01 92000
{"method":"slim.request","result":{},"id":1,"params":["00:01:02:03:04:05",["mixer","volume","90"]]}set ALERT_VOLUME
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   188  100   101  100    87    101     87  0:00:01 --:--:--  0:00:01 37600
{"method":"slim.request","id":1,"result":{},"params":["00:01:02:03:04:05",["playlist","repeat","0"]]}set repeat_setting to 0
waiting for external song to start playing
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 44750
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01  174k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 44750
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01  174k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 35800
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01  174k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 44750
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 25571
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01 59666
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01  174k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   179  100   103  100    76    103     76  0:00:01 --:--:--  0:00:01  174k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k

wait for alert song to stop playing
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 35600
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 29666
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 29666
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 35600
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 29666
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 35600
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01 89000
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   178  100   102  100    76    102     76  0:00:01 --:--:--  0:00:01  173k
alert song stopped playing
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   186  100    99  100    87     99     87  0:00:01 --:--:--  0:00:01 46500
{"result":{},"id":1,"method":"slim.request","params":["00:01:02:03:04:05",["mixer","volume","70"]]}restore prev_volume setting
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   190  100   101  100    89    101     89  0:00:01 --:--:--  0:00:01  185k
{"params":["00:01:02:03:04:05",["playlist","repeat","0"]],"method":"slim.request","id":1,"result":{}}restore prev_repeat setting
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100   222  100   117  100   105    117    105  0:00:01 --:--:--  0:00:01   556
100   222  100   117  100   105    117    105  0:00:01 --:--:--  0:00:01   556
{"id":1,"result":{},"method":"slim.request","params":["00:01:02:03:04:05",["playlist","resume","office","noplay:0"]]}resume previous playlist

Ok I think I had a permission issue I chmodded the script and also +x the script/bash file and now I get the volume changing to the setting an lowering back. Now the playlist/music still isn’t resuming after the speaking.

Timing for delay longer shorter any ideas?

Ok I got it to work but Now the only thing is that I had to hard code the restore previous with this

# 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","'"chainsmokers"'","noplay:'$noplay'"]]}' $JSONRPC
  echo "resume previous playlist"
fi

So I think it’s a issue with how it gets the current playlist and passes it after its done. Any Ideas how to fix so it is a variable get then restore?

Also only works once after start of Addon for resuming of playlist??

@smazman
getting a write error??

{"result":{"__playlist_id":54576,"writeError":1},"id":1,"method":"slim.request","params":["00:01:02:03:04:05",["playlist","save","office","silent:1"]]}save current playlist

Version 2 of squeezebox_alert addon:

  • Updated so delay is no longer needed in configuration.yaml for TTS.
  • <player_name>.lock file is used to only permit 1 audio alert per player at a time (created under config dir)
  • The following configuration.yaml file entries have changed (see example configuration.yaml for details):
    ‘playlist_name’ => ‘player_name’ (NOTE: For TTS, the string value for ‘player_name’ must match your squeezebox media_player name exactly)
    ‘alert_song’ => ‘alert_mp3’
    Added: ‘tts_message’ (NOTE: ‘tts_message’ is ignored if ‘alert_mp3’ is defined. I have not tested passing templates or other weird things in as the ‘tts_message’)

Dockerfile (same as previous version):

ARG BUILD_FROM
FROM $BUILD_FROM

# Add env
ENV LANG C.UTF-8

# Install requirements for add-on
RUN apk add --no-cache jq curl

# Copy data for add-on
COPY squeezebox_alert.sh /
RUN chmod a+x /squeezebox_alert.sh
COPY run.sh /
RUN chmod a+x /run.sh

CMD [ "/run.sh" ]

config.json (NOTE: Update json_rpc below. This can also be done in the addon Options in the web interface)

{
  "name": "Squeezebox Alert",
  "version": "2",
  "slug": "squeezebox_alert",
  "description": "Play audio alerts through LMS",
  "startup": "application",
  "boot": "auto",
  "homeassistant_api": true,
  "stdin": true,
  "map": ["config:rw"],
  "options": {
    "json_rpc": "http://user:[email protected]:9000/jsonrpc.js"
  },
  "schema": {
    "json_rpc": "str"
  }
}

run.sh

#!/bin/bash
set -e

CONFIG_PATH=/data/options.json

JSONRPC=$(jq --raw-output ".json_rpc" $CONFIG_PATH)
echo "JSONRPC=$JSONRPC"

while read -r input; do
  echo "input=$input"

  PLAYER_NAME="$(echo "$input" | jq --raw-output '.player_name')"
  echo "[Info] Read player_name: $PLAYER_NAME"

  MAC="$(echo "$input" | jq --raw-output '.mac')"
  echo "[Info] Read mac: $MAC"

  ALERT_MP3="$(echo "$input" | jq --raw-output '.alert_mp3')"
  echo "[Info] Read alert_mp3: $ALERT_MP3"

  #NOTE: tts_message is ignored if alert_mp3 is defined
  TTS_MSG="$(echo "$input" | jq --raw-output '.tts_message')"
  echo "[Info] Read tts_message: $TTS_MSG"
  
  ALERT_VOLUME="$(echo "$input" | jq --raw-output '.alert_volume')"
  echo "[Info] Read alert_volume: $ALERT_VOLUME"
  
  ./squeezebox_alert.sh $JSONRPC $PLAYER_NAME $MAC "$ALERT_MP3" "$TTS_MSG" $ALERT_VOLUME &
done 

squeezebox_alert.sh

#!/bin/bash

#squeezebox_alert.sh

JSONRPC=$1
PLAYER_NAME=$2
MAC=$3
ALERT_MP3=$4
TTS_MSG=$5
ALERT_VOLUME=$6
#restore_playlist=${7:-1}
restore_playlist=1
echo "restore_playlist=$restore_playlist"

#exit script if a lock file named $PLAYER_NAME exists, which signifies this player is being used
LOCK_FILE=/config/$PLAYER_NAME.lock
if [ -f $LOCK_FILE ] ; then
  echo "script is locked...exiting."
  exit 0
fi

#create a lock file named $PLAYER_NAME to signify this player is being used
echo "lock" >> $LOCK_FILE

#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
  echo "pause currently playing song"
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["pause"]]}' $JSONRPC

  #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
echo "save current playlist"
curl -X GET -H "Content-Type: application/json" \
     -d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","save","'"$PLAYER_NAME"'","silent:1"]]}' $JSONRPC

#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
echo "set ALERT_VOLUME"
curl -X GET -H "Content-Type: application/json" \
     -d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["mixer","volume",'$ALERT_VOLUME']]}' $JSONRPC
	 
#set repeat setting to 0
echo "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
 
#if ALERT_MP3 is null, play alert using TTS
if [ $ALERT_MP3 = "null" ] ; then
  echo "play TTS alert"
  curl -X POST -H "x-ha-access: API_TOKEN" -H "Content-Type: application/json" \
       -d '{"entity_id": "media_player.'$PLAYER_NAME'", "message": "'"$TTS_MSG"'"}' \
       http://hassio/homeassistant/api/services/tts/google_say
else
  #play alert mp3
  echo "play alert mp3"
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","play","'"$ALERT_MP3"'"]]}' $JSONRPC
fi

#WAIT FOR ALERT SONG TO STOP PLAYING
echo "wait for alert song to stop playing"
cur_mode="play"
#TODO: Timeout after 10 seconds if song doesn't stop playing
while [[ $cur_mode =~ .*play.* ]]; do
  sleep 0.2
  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
echo "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

#restore prev_repeat setting
echo "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

#resume previous playlist (always resume if previously playing)
if [ $prev_playmode -eq 1 ] || [ $restore_playlist -eq 1 ] ; then
  echo "resume previous playlist"
  curl -X GET -H "Content-Type: application/json" \
       -d '{"id":1,"method":"slim.request","params":["'"$MAC"'",["playlist","resume","'"$PLAYER_NAME"'","noplay:'$noplay'"]]}' $JSONRPC
fi

#RESUME PREVIOUSLY PLAYING MUSIC
if [ $prev_playmode -eq 1 ] ; then
  #skip ahead in song to prev_time
  echo "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
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

#Wait a couple of seconds and then delete lock file (this seems to be necessary to allow curl commands to complete)
sleep 2
rm $LOCK_FILE
exit 0

New example configuration.yaml file (not complete):
example uses in configuration.yaml (1.5 KB)

@DrJeff You shouldn’t need to change permissions of any of the addon files. This should all be done for you when it is installed. Perhaps your playlist file (in your squeezebox playlist directory) is read-only or something? The only other thing I can think of is maybe one of your params has a space or a weird character in it that the addon/script isn’t handling correctly. Hopefully the new version fixes any issues you are seeing. EDIT: If not, please give some more details about your setup. (Version of LMS Server, machine it is running on, type of squeezebox players, etc.)

Would it somehow be possible to get the save/restore functionality built in into the standard Squeezebox component? So you don´t need to call the shell command?

That would be awesome.

1 Like

My mistake, I had a turn_on call further up in my automation so the power was always recognised as on!
It’s now working perfectly - thank you again.

I looked at the github code for the hass squeezebox component, and yes it would certainly be possible. It looks like it is using the lms json rpc interface, similar to my script. But I think the main media component would need to also support “save_playlist” and “restore_playlist”… It already supports “clear_playlist”.

Ah,sounds promising
It would be great to have this feature built in.

FYI, you can get the various pre-alert player parameters in a single compound call to LMS - there’s no need to request the parameters separately. The returned compound json looks like this:

{
   "result" : {
      "player_ip" : "192.168.178.55:50476",
      "digital_volume_control" : 1,
      "playlist_cur_index" : "17",
      "playlist shuffle" : 0,
      "time" : 64.8850210094452,
      "playlist_loop" : [
         {
            "playlist index" : 17,
            "title" : "The Working Hour",
            "id" : 348077
         }
      ],
      "playlist repeat" : 2,
      "playlist mode" : "off",
      "player_connected" : 1,
      "duration" : 392.773,
      "playlist_timestamp" : 1516107934.68356,
      "mixer volume" : 48,
      "player_name" : "Bathroom",
      "seq_no" : "114",
      "signalstrength" : 70,
      "can_seek" : 1,
      "playlist_tracks" : 37,
      "rate" : 1,
      "mode" : "play",
      "power" : 1
   },
   "params" : [
      "00:04:20:1a:de:c9",
      [
         "status",
         "-",
         "1",
         "tags"
      ]
   ],
   "method" : "slim.request"
}

To get and the parse the compound status, in perl I use:

$res = ( $client->call($uri, { method  => 'slim.request', params  => [ $player, ['status', '-', '1', 'tags' ] ] }) ) || 0;

$response=$res->jsontext; $data=decode_json $response;
$power=$$data{result}{power}; print "$power\n";
$mode=$$data{result}{mode}; print "$mode\n";
$volume=$$data{result}{'mixer volume'}; print "$volume\n";
$repeat=$$data{result}{'playlist repeat'}; print "$repeat\n";
$time=$$data{result}{time}; print "$time\n";

Trivial to port this to bash if you see what I’m doing here.

@philchillbill, thanks for the info. I remember seeing this, but I forget why I didn’t use it. I’ll try it again. It would be nice to simplify the script and reduce the number of calls to LMS.

FYI, I see HA added a SQUEEZEBOX_CALL_METHOD Service which allows direct calls to the LMS CLI from a HA automation or script. I suppose this could be used to make a HA yaml script/sequence to implement the save/restore LMS playlist functionality… but I’m not sure how easy it is to parse results returned from these calls.

If anyone is working on integrating this into the original Squeezebox component and needs testers please let me know.
This save/restore script is awesome but I can´t find a good way to use it when executing TTS from the player in the HASS UI.

A family member gave me one of these radios as she wasnt using it anymore. Just wondering if the UE or squeezebox firmware is the better way to go??

I would keep it as Squeezebox, more possibilities and plugins into LMS

@freelev, I’m not sure what your are asking for in regards to “executing TTS from the player in the HASS UI”… but recently I added an “Intercom” to the HASS UI and use it with my hass.io squeezebox_alert addon to send TTS audio alerts to a selected squeezebox (I copied the general idea from another thread on the forum). This is working really well. Here is a screenshot and see below for the associated yaml entries (it will require some edits to work with the non-addon squeezebox_alert script).

group:
  intercom:
    name: "Intercom"
    entities:
      - input_select.squeezebox_list
      - input_text.intercom_message
      - input_number.intercom_volume
	  
input_select:
  squeezebox_list:
    name: 'Select Speakers:'
    options:
      - Basement
      - Bedroom
      - Deck
      - Kitchen
      - Living Room
    initial: Kitchen
    icon: mdi:speaker-wireless
	
input_text:
  intercom_message:
    name: Intercom Message

input_number:
  intercom_volume:
    name: Volume
    icon: mdi:volume-high
    initial: 50 
    min: 0
    max: 100
    step: 5
	
automation:
  - alias: "Intercom"
    trigger:
      platform: state
      entity_id: input_text.intercom_message
    condition:
      condition: template
      value_template: '{{ (states.input_text.intercom_message.state | length) > 0 }}'
    action:
    - service: script.send_intercom
	
script:
  send_intercom:
    alias: Send Intercom
    sequence:
      - alias: Send Message
        service: hassio.addon_stdin
        data_template:
          addon: local_squeezebox_alert
          input:
            player_name: >
             {% if is_state("input_select.squeezebox_list", "Basement") %} basement
             {% elif is_state("input_select.squeezebox_list", "Bedroom") %} bedroom
             {% elif is_state("input_select.squeezebox_list", "Deck") %} deck
             {% elif is_state("input_select.squeezebox_list", "Kitchen") %} kitchen
             {% elif is_state("input_select.squeezebox_list", "Living Room") %} living_room
             {% endif %}
            mac: >
             {% if is_state("input_select.squeezebox_list", "Basement") %} 00:01:02:03:04:05
             {% elif is_state("input_select.squeezebox_list", "Bedroom") %} 00:01:02:03:04:06
             {% elif is_state("input_select.squeezebox_list", "Deck") %} 00:01:02:03:04:07
             {% elif is_state("input_select.squeezebox_list", "Kitchen") %} 00:01:02:03:04:08
             {% elif is_state("input_select.squeezebox_list", "Living Room") %} 00:01:02:03:04:09
             {% endif %}
            tts_message: '{{ states.input_text.intercom_message.state }}'
            alert_volume: '{{  states.input_number.intercom_volume.state  }}'
      - alias: Clear Message
        service: input_text.set_value
        data:
          entity_id: input_text.intercom_message
          value: ''
1 Like

Hi
This is a really nice approach, I´ll try it!
What I meant was if you use the built in TTS that exist in the mediaplayer entity UI it breaks the current playlist loaded for Squeezebox
This because the built in squeezebox component is missing the save/restore functionality.

Your Intercom will be a nice workaround for this

EDIT:
You dont happen to know how to make the calls to save/restore dynamic based on whats selected?

  - 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

Or I need to create on playlist_save/restore for each player?

OK, so I`ve been working with this intercom a bit and this is what I came up with

configuration.yaml:

shell_command:
 playlist_save_lms: bash /home/hass/.homeassistant/squeezebox_alert_save_playlist.sh {{lplayer}} {{lmac}} 100
 playlist_restore_lms: bash /home/hass/.homeassistant/squeezebox_alert_restore_playlist.sh {{lplayer}} {{lmac}}

Intercom script;

  send_intercom:
    alias: Send Intercom
    sequence:
      - alias: Send TTS
        service: script.speech_processing
        data_template:
          speech_message: '{{ states.input_text.intercom_message.state }}'
          lms_player: >
             {% if is_state("input_select.squeezebox_list", "Bathroom") %} media_player.bathroom
             {% elif is_state("input_select.squeezebox_list", "Bedroom") %} media_player.bedroom
             {% elif is_state("input_select.squeezebox_list", "Living room") %} media_player.livingroom
             {% elif is_state("input_select.squeezebox_list", "Kitchen") %} media_player.kitchen
             {% elif is_state("input_select.squeezebox_list", "Dining") %} media_player.dining
             {% endif %}
          lms_volume: '{{ (states("input_number.intercom_volume") | int) /100 }}'
          tts_languge: 'sv'
      - alias: Clear Message
        service: input_text.set_value
        data:
          entity_id: input_text.intercom_message
          value: ''

Speech processing script with save/restore:`

speech_processing:
  sequence:
- condition: CONDITIONS

- service: media_player.turn_on
  data_template:
    entity_id: "{{ lms_player|default('media_player.kitchen') }}"

- service: media_player.volume_set
  data_template:
    entity_id: "{{ lms_player|default('media_player.kitchen') }}"
    volume_level: "{{ lms_volume|default('0.8') }}"

- service: shell_command.playlist_save_lms
  data_template:
    lplayer: >
         {% if 'media_player.kitchen' == lms_player %} kitchen
         {% elif 'media_player.bathroom' == lms_player %} bathroom
         {% elif 'media_player.dining' == lms_player %} dining
         {% elif 'media_player.livingroom' == lms_player %} living
         {% elif 'media_player.bedroom' == lms_player %} bedroom
         {% else %} playlist
         {% endif %}
    lmac:  >
         {% if 'media_player.kitchen' == lms_player %} 00:00:20:17:42:00
         {% elif 'media_player.bathroom' == lms_player %} 00:04:33:1e:d0:d3
         {% elif 'media_player.dining' == lms_player %} 00:04:20:44:d3:52
         {% elif 'media_player.livingroom' == lms_player %} 00:04:55:06:13:2b
         {% elif 'media_player.bedroom' == lms_player %} 00:04:66:26:09:6f
         {% else %} 00:04:20:00:42:21
         {% endif %}

- service: tts.google_say
  data_template:
    entity_id: "{{ lms_player|default('media_player.kitchen') }}"
    message: '{{speech_message}}'
    cache: true
    language: '{{tts_language}}'

- service: shell_command.playlist_restore_lms
  data_template:
    lplayer: >
         {% if 'media_player.kitchen' == lms_player %} kitchen
         {% elif 'media_player.bathroom' == lms_player %} bathroom
         {% elif 'media_player.dining' == lms_player %} dining
         {% elif 'media_player.livingroom' == lms_player %} living
         {% elif 'media_player.bedroom' == lms_player %} bedroom
         {% else %} playlist
         {% endif %}
    lmac:  >
          {% if 'media_player.kitchen' == lms_player %} 00:00:20:17:42:00
         {% elif 'media_player.bathroom' == lms_player %} 00:04:33:1e:d0:d3
         {% elif 'media_player.dining' == lms_player %} 00:04:20:44:d3:52
         {% elif 'media_player.livingroom' == lms_player %} 00:04:55:06:13:2b
         {% elif 'media_player.bedroom' == lms_player %} 00:04:66:26:09:6f
         {% else %} 00:04:20:00:42:21
         {% endif %}

Still some work to do and there is probably room for improvement and it is pretty much based on @smazman and @CCOSTAN code.
Volume should probably be fixed not to send 100 when saving playlist

An idea is to simplify the code a bit and skip the reformatting of playlist name and actually store it as media_player.xxxx, guess a substring would work too but I´m not that into jinja

2 Likes

Taking a look at @philchillbill’s openhab scripts, I cobbled together a light sensor utilizing my squeezebox radio. I called it lux, but the SB doesn’t actually report lux as you will see. Perhaps it will be handy for someone…

First thing you should enable SSH on your squeezebox:

‘Settings → Advanced → Remote Login’ and make sure ‘Enable SSH’ is ticked/selected.

Using putty remote to your squeezebox using root and 1234 as the password.

With vi, create lux.sh:

#constants
CHECK_INTERVAL=60
AMBIENT=/sys/class/i2c-adapter/i2c-1/1-0010/ambient
SENSOR=/api/states/sensor.kitchen_lux_via_api
HASS=blackpogo
MAX_DARK=600

prev=0;
loops=0;

# loop forever
while /bin/true; do

    # sleep a bit
    sleep $CHECK_INTERVAL

    loops=$(( $loops + 1)); if [ "$loops" -gt 1000000 ]; then loops=0; fi

    # read sensor value and truncate newline
    val=`cat $AMBIENT | tr "\n" " "`

    if [ "$val" -gt $MAX_DARK ]; then
            val=$MAX_DARK
    fi

    # convert to lux
    val=$(($MAX_DARK-$val))

    # get difference
    delta=$(($val - $prev))

    # step is the absolute-value of the delta in lux
    if [ "$delta" -ge 0 ]; then
            step=$delta
    else
            step=$((0 - $delta))
    fi

    if [ "$step" -gt 20 ] || [ "$prev" -eq 0 ] || [ $(($loops % 30)) -eq 0 ]; then
            prev=$val
            JSON="{\"state\": \"$val\"}"
            JSON_LEN=$( busybox echo -n ${JSON} | wc -c )
            busybox echo -ne "POST $SENSOR HTTP/1.0\r\nUser-Agent: curl/7.57.0\r\nAccept: */*\r\nContent-Type: application/json\r\nContent-Length: ${JSON_LEN}\r\n\r\n${JSON}" | nc -i 3 $HASS 8123
    fi

  done

The only tricky part here is that the squeezebox doesn’t include curl or a decent version of wget, so I had to manually create the REST service call with netcat. My HASS doesn’t have a password, so I didn’t include it in the http headers - you may have to do this.

chmod the script to allow execution

chmod +x

Run it like this

./lux.sh

In my configuration.yaml file I created a template sensor:

sensor:
  # squeezebox 
  - platform: template
    sensors:
      kitchen_lux_via_api:
        value_template: '{{ states.sensor.kitchen_lux_via_api.state }}'
        friendly_name: Kitchen Lux
        unit_of_measurement: lux

In my automations.yaml file I added a condition to an existing automation to turn my kitchen lights when movement from my PIR is detected and between certain times. The addition will also turn the light on on dark days outside of the time range.

  - id: automation_3_5
    alias: 'Turn on kitchen light when there is movement'
    trigger:
      - platform: state
        entity_id: binary_sensor.family_room_motion
        to: 'on'
    condition:
      - condition: state
        entity_id: switch.kitchen
        state: 'off'
      - condition: or
        conditions:
          - condition: time
            before: '08:30:00'
          - condition: time
            after: '16:30:00'
          - condition: numeric_state
            entity_id: sensor.kitchen_lux_via_api
            below: 500.0
    action:
      - service: switch.turn_on
        entity_id: switch.kitchen


  - id: automation_3_7
    alias: 'Turn off kitchen light when bright'
    trigger:
      - platform: numeric_state
        entity_id: sensor.kitchen_lux_via_api
        above: 550.0
    condition:
      - condition: state
        entity_id: switch.kitchen
        state: 'on'
    action:
      - service: switch.turn_off
        entity_id: switch.kitchen
1 Like