[Solved] Send Answers over MQTT to Rhasspy Intent Requests

Hi,

don’t know if this is the right forum - maybe this is only for the built in TTS in OH??

I’m using Rhasspy for voice recognition, and I managed to get it working with openhab through MQTT. This works fine so far, but now I’m trying to setup some rules…

So my biggest problem is to find any information about how to send answers to the requests e.g. “what time is it” or similar… I know I’m not the only one using Rhasspy with OH so maybe one could give me a little more information how this answer code would look like in a DSL rule.

A good information would be, which mqtt-topic to use to get Rhasspy speaking to me :wink:

maybe some examples how to configure mqtt…

this is my configuration so far:

rhasspy.things

Bridge mqtt:broker:Rhasspy "MQTT - Rhasspy Broker" @ "MQTT" [ 
    host="192.168.xxx.xxx",
    secure=false,
    port=1883,
    qos=2,
    retainMessages=false,
    clientID="Openhab3",
    keep_alive_time=60000,
    reconnect_time=30000,
    username="anyname",
    password="anypasswd"
]
{
    // Rhasspy Intent
    Thing topic Rhasspy_Intent "MQTT - Rhasspy Intents" @ "MQTT" {
        Channels:
            Type string : Rhasspy_Time "Rhasspy Time Intent" [
                stateTopic="hermes/intent/GetTime"
            ]
            Type string : Rhasspy_Date "Rhasspy Date Intent" [
                stateTopic="hermes/intent/GetDate"
            ]
            Type string : Rhasspy_Day "Rhasspy Day Intent" [
                stateTopic="hermes/intent/GetDay"
            ]
            Type string : Rhasspy_Alarm "Rhasspy Alarm Intent" [
                stateTopic="hermes/intent/SetAlarm"
            ]
            Type string : Rhasspy_Timer "Rhasspy Timer Intent" [
                stateTopic="hermes/intent/SetTimer"
            ]
            Type string : Rhasspy_Temperature "Rhasspy Temperature Intent" [
                stateTopic="hermes/intent/GetTemperature"
            ]
            Type string : Rhasspy_Humidity "Rhasspy Humidity Intent" [
                stateTopic="hermes/intent/GetHumidity"
            ]
            Type string : Rhasspy_Light "Rhasspy Light Intent" [
                stateTopic="hermes/intent/ChangeLightState"
            ]
            Type string : Rhasspy_LightScene "Rhasspy Light Scene Intent" [
                stateTopic="hermes/intent/ChangeLightScene"
            ]
            Type string : Rhasspy_Volume "Rhasspy Volume Intent" [
                stateTopic="hermes/intent/SetVolume"
            ]
    }
}

rhasspy.items

String  Rhasspy_Time        "Rhasspy Time Intent"           (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Time" }
String  Rhasspy_Date        "Rhasspy Date Intent"           (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Date" }
String  Rhasspy_Day         "Rhasspy Day Intent"            (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Day" }
String  Rhasspy_Alarm       "Rhasspy Alarm Intent"          (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Alarm" }
String  Rhasspy_Timer       "Rhasspy Timer Intent"          (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Timer" }
String  Rhasspy_Temperature "Rhasspy Temperature Intent"    (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Temperature" }
String  Rhasspy_Humidity    "Rhasspy Humidity Intent"       (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Humidity" }
String  Rhasspy_Light       "Rhasspy Light Intent"          (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Light" }
String  Rhasspy_LightScene  "Rhasspy Light Scene Intent"    (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_LightScene" }
String  Rhasspy_Volume      "Rhasspy Volume Intent"         (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Volume" }

here is an example rule for light control

rule "Rhasspy Light Control"
when
    Item Rhasspy_Light received update
then
    var String intentItemSlot = transform("JSONPATH", "$.slots[?(@.slotName=='name')].value.value", Rhasspy_Light.state.toString)
    var String intentStatusSlot = transform("JSONPATH", "$.slots[?(@.slotName=='state')].value.value", Rhasspy_Light.state.toString)

    if ( intentItemSlot == "Wohnzimmer" ) {
        logDebug ("Rhasspy", "Light Voice Command Received")
        if ( intentStatusSlot == "ein" ) {
            LightLivingroomColor.sendCommand("100")
        }
        if ( intentStatusSlot == "aus" ) {
            LightLivingroomColor.sendCommand("0")
        }
    }
    if ( intentItemSlot == "Standleuchten" ) {
        logDebug ("Rhasspy", "Light Voice Command Received")
        if ( intentStatusSlot == "ein" ) {
            gStandsBulbsSwitch.sendCommand("ON")
        }
        if ( intentStatusSlot == "aus" ) {
            gStandsBulbsSwitch.sendCommand("OFF")
        }
    }
    if ( intentItemSlot == "TV-Wand" ) {
        logDebug ("Rhasspy", "Light Voice Command Received")
        if ( intentStatusSlot == "ein" ) {
            LightTvPower.sendCommand("ON")
        }
        if ( intentStatusSlot == "aus" ) {
            LightTvPower.sendCommand("OFF")
        }
    }

    logDebug ("rhasspy.rules testing", "Rhasspy raw JSON: " + Rhasspy_Light.state.toString)
    logDebug ("rhasspy.rules testing", "Rhasspy intentItemSlot: " + intentItemSlot)
    logDebug ("rhasspy.rules testing", "Rhasspy intentStatusSlot: " + intentStatusSlot)
end

Thanx in advance

Dan

1 Like

https://rhasspy.readthedocs.io/en/latest/reference/#tts_say

1 Like

thanks a lot - thats what I was looking for :slight_smile:

1 Like

Ok,

I think I should use "hermes/tts/say" or "hermes/tts/sayfinished" as topic
I’m not really sure about that… and should I add a command topic to every single channel or is it better to create a global “answer” channel that uses a command topic?

Another problem for me is to guess the syntax for the command I send to the mqtt item…

should it look something simple like this:

Rhasspy_Light.sendCommand("Your Command Here")

or how do I define to use the text slot from Rhasspy?

Any help appreciated

The first I herd of Rhasspy was your post.

My favourite tool for MQTT integration is http://mqtt-explorer.com/

First is to figure out how to get it to say anything.
You send a JSON string to hermes/tts/say

Looking at the doc try

{“text”:“The light was turned off”}

thanks @denominator :wink:

it’s not completely new - there are other posts on the forum about rhasspy… but none of them reveals my needed information and so the rhasspy forum. But I think that’s because rhasspy was originally designed to use with home assistant… but homeassistant uses mqtt too to talk to rhasspy - so I thought I’ll give it a try…

but for me who’s native language is german it’s sometimes hard to understand english docs because they’re often a little bit overwhelming to me.

I will try your suggestion and hopefully I can get a step further - but I think this syntax makes sence in case of JSON… so thanks a lot!!

have nice weekend!

Ok

after a bunch of quotation errors I finally got it working :wink:

one should generate an additional “answer” channel like this:

Channels:
            Type string : Rhasspy_Answer "Rhasspy Answer Intent" [
                commandTopic="hermes/tts/say"
            ]

and the according item of course

String  Rhasspy_Answer      "Rhasspy Answer Intent"         (gRhasspy)    { channel="mqtt:topic:Rhasspy:Rhasspy_Intent:Rhasspy_Answer" }

and then you’re able to send commands like here

rule "Rhasspy Get Time"
when
    Item Rhasspy_Time received update
then
    var Number TimeHours = now.getHour()
    var Number TimeMinutes = now.getMinute()
    Rhasspy_Answer.sendCommand('{"siteId":"SATELLITE1","text":"es ist '+TimeHours+' uhr und '+TimeMinutes+' minuten"}')
    logDebug ("rhasspy.rules testing", "Rhasspy raw JSON: " + Rhasspy_Time.state.toString)
    logDebug ("rhasspy.rules testing", "Hours: " + TimeHours)
    logDebug ("rhasspy.rules testing", "Minutes: " + TimeMinutes)
end

thanks for your suggestions this send me on the right track

the only problem now is that rhasspy doubles the answer… I think maybe this is caused by not using “hermes/tts/sayFinished” - I have to review that…