Rhasspy with MQTT to Node-RED

I have just revisited Paul Romkes’ rhasspy wiki Intent Handling with HA and Node Red tutorial, and discovered that I had set up my configuration wrong - well, it worked; but I thought I was using MQTT when I was actually using a “websocket in” node.

And in swapping over I think the reason was that the “mqtt in” node is so much more confusing than “websocket in” node.

The “websocket in” node returns intent and slots data directly to the msg. object, where they are easy to find and use.

In comparison, the “mqtt in” node is particularly convoluted with excessive nesting of objects - compare “msg.payload.slots[1].value.value” with “msg.slots.state” which both return “off”. Also the rhasspy “slots” are returned in an array which requires functions to extract a data values,

I don’t hope to change the way “mqtt in” node works … but rather than change the rest of my node-RED flows I have added a function to parse the mqtt in payload and copy slots and other relevant data into the same format as returned by the websocket in node.

//  MQTT in node returned the Rhasspy slots in the array msg.payload.slots. 
//  This code parses the array into a list of name and value pairs
var newslots = {};                  // create a new empty object

for (var i in msg.payload.slots) {
    slot = msg.payload.slots[i]     // one array element at a time
    //new name and value pair
    newslots[slot.slotName] = slot.value.value;

msg.slots = newslots;               // save back into msg.

// and a couple of other important values
msg.sessionId = msg.payload.sessionId;
msg.siteId = msg.payload.siteId;

return msg