Playing mp3 through Snips

I have HA .100.3 responding to Snips to give me the weather, a la the HA snips sample code.

What I’m wondering is if it is possible to play a web based mp3 file over Snips. I’d like to write a Good Morning intent that when asked gives the weather via TTS and then plays the National News mp3 file.

You can only do so if you use a custom TTS provider. There is some support now for mp3 directly but it is somewhat limited. Here is my code that uses Home Assistants TTS to generate the voice that creates the mp3 file for custom voices. I added some pseudo code to show how you could fetch a web based mp3 and play it.

#set -x
API_KEY=$HASSIO_TOKEN
PLATFORM=$1
FILE=$2
LANG=$3
TEXT=$4

MESSAGE="\"{\\\"message\\\": \\\"$TEXT\\\", \\\"platform\\\": \\\"$PLATFORM\\\"}\""
echo "$MESSAGE"

# we got a url instead of text
if [ "$TEXT" startswith "http"; then
   # fetch the file and write it to where snips expects it
   rm /tmpfs/temp.mp3
   curl -s "TEXT" -o /tmpfs/temp.mp3
   if [ -f /tmpfs/temp.mp3 ]; then
     /usr/bin/mpg123 -w "$FILE" /tmpfs/temp.mp3
   fi
   rm /tmpfs/temp.mp3
   exit
fi

RESPONSE=$(eval curl -s -H \"x-ha-access: "$API_KEY"\" -H \"Type: application/json\" http://hassio/homeassistant/api/tts_get_url -d "$MESSAGE")
if [ "$RESPONSE" = "" ]; then
    exit 1
fi
echo "$RESPONSE"

URL=$(echo "$RESPONSE" | jq --raw-output '.url')
if [ "$URL" = "" ]; then
    exit 1
fi

rm /tmpfs/temp.mp3
curl -s -H "x-ha-access: $API_KEY" "$URL" -o /tmpfs/temp.mp3
if [ -f /tmpfs/temp.mp3 ]; then
  /usr/bin/mpg123 -w "$FILE" /tmpfs/temp.mp3
fi
rm /tmpfs/temp.mp3

@tschmidty What exactly do you mean by a custom TTS provider? Do you mean something like PicoTTS or MaryTTS?

Since my knowledge of code and scripting wouldn’t fill a thimble I am unsure of where or how to use your code.

Also I am running regular HA and not Hassio but I’m not sure if that matters

I use mpg123 as an exec command that I pass the mp3 too from my action code.
Johannes

@JGKK can you share an example?

Oh I’m not sure if it matters or not but I am running HA and not Hassio

You just need a way to execute a command on the command line from your script. If your snips system is Linux based you can apt-get install mpg123. After that you can call it from your action code with the path or url to your mp3 as an argument. If you write the action code in python you would use the subprocess module to do it: https://docs.python.org/3.5/library/subprocess.html
Johannes

@JGKK,

Oh, thanks for the explanation but I have almost 0 knowledge of Python and the only thing I’ve gotten Snips and HA doing is telling me the weather and turning lights on and off.

I have no experience with ha or hassio. But what you want to do is a basic intent that needs to execute some action. This is completely independent of ha or a platform like that. You need to have something that listens to snips Intents and than does something. This can be anything. Have you ever played with nodered. There it would be really easy to implement what you want to do with nearly no programming.

I haven’t played with Node Red. It’s been on my list that I never get to.

1 Like