I am trying to get my speakers to answer an intent with a sound, an mp3 file. I have a main server and then a few rpi satellites. I have a python script that handles the intents. So i create an MQTT client and connect to a broker.
I normally answer intents with a sentence and it gets published to hermes/tts/say. the little code snippet is below
site_id = nlu_payload["siteId"]
sentence = "answer to the intent"
client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))
So I was thinking there would be a different client.publish I could do for the mp3. something like
But I am a little new with the hermes stuff so i wasnt sure if that was possible
I have seen other threads that mention cmd line commands that can be run. But wasnt sure what the requestID is or how to get it. Also not sure if that will work in a python script
or the hermes audio server. So i was thinking could i use hermes/audioServer/siteId/playBytes/requestId as the first part of the client.publish() but with the variables it expects, I wasnt sure how that would work.
So if anyone has got this to work before I would be grateful to learn how you did it
Hey dblanc28,
Below is a modification of the very first python script I wrote (just changed how I’m calling aplay). I use it in conjunction with HA to play a variety of wav files in response to Rhasspy and HA automations. You coule swap out play for another audio player that supports MP3. I have it autostarting via systemd, work for my purposes (fun announcements, Rhasspy “Fire Phasers”!)
# !/usr/bin/env python
# encoding: utf-8
import paho.mqtt.client as mqtt #import the client, paho.mqtt.client, if you need to install: pip install paho-mqtt
import subprocess
def on_connect(client, userdata, flags, rc):
# print("Connected with result code " + str(rc)) # for debugging, uncomment to check if connected, should be result code 0
client.subscribe("ha/playaudio/#")
def on_message(client, userdata, message):
# print("message received " ,str(message.payload.decode("utf-8"))) # for debugging, uncomment to check message
# print("message topic=",message.topic) # for debugging, uncomment to check topic
# print("message qos=",message.qos) # for debugging, uncomment to check quality of service
# print("message retain flag=",message.retain) # for debugging, uncomment to check retained message flag
wavefile = ''
wavefile = str(message.payload.decode("utf-8"))
# print ("aplay" + " " + "--device" + " " + "plughw:1,0" + " " + wavefile) # for debugging, uncomment to check message
subprocess.Popen(['aplay', '--device','plughw:1,0', wavefile])
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("XXX.XXX.XXX.XXX", 1883, 60) # your mosquitto server info
client.loop_forever()