Pass MQTT payload to action?

I want to pass the trigger payload from MQTT to the action, a rhasspy_speak event. The yaml looks like this:

- id: '1653688305070'
  alias: MQTT test
  description: ''
  trigger:
  - platform: mqtt
    topic: test
  condition: []
  action:
  - service: rest_command.rhasspy_speak
    data:
      payload: trigger.payload
  mode: single

The trigger is from a terminal: mosquitto_pub -h 127.0.0.1 -t “test” -m “hello world”
On being triggered by MQTT, rhasspy says “trigger.payload” instead of “hello world.” How can I pass the MQTT payload to the action? Thanks.

I think

payload: {{trigger.payload}}

When this is set, rhasspy reads it verbatim (“left curly bracket”, etc.) then HA set the yaml to read:
‘’’
payload:
‘[object Object]’: null
‘’’
then reads that verbatim on next trigger. I’m thinking I have to declare the variable before using it?

Wrap the template in quotes.

payload: '{{trigger.payload}}'

Without the quotes, it will interpret it as a literal string instead of a Jinja2 template.

This will not work the way you expect it to, sorry. And it is not the way Rhasspy recommends doing such a thing.

There is an extra endpoint in the API from Rhasspy to get text to speech. Especially important is the use of sessions, as these are opened when you speak, and are normally closed when HA gets back to Rhasspy.

The next thing is, payload isn’t something Rhasspy usually sends. If you stayed with the “official” install instructions, it will likely be trigger.event.data.xxx where “xxx” is the substitution from Rhasspy.

I’d recommend to use the Rhasspy defaults, as there are is a lot more help available, if you just Rhasspys “events” or “intents”, both of which are especially tailored for the use of Rhasspy in combination with HA.

When I use ‘{{trigger.payload}}’, then rhasspy says nothing and I get a warning that the visuall editor can’t be used for this.

Rhasspy is used here in the action, not the trigger. MQTT is the trigger. I want to send the message received by MQTT to the service RESTful Command :rhasspy_speak as the payload to be spoken. that command is in configuration.yaml and appears in the dropdown for call service under actions:

rest_command:
  rhasspy_speak:
    url: 'http://192.168.0.114:12101/api/text-to-speech'
    method: 'POST'
    content_type: 'text/plain'
    payload: "{{ payload }}"

The Visual Editor has several limitations and when it can’t handle something it often reports it as a warning and then suggests to switch to YAML mode. What I suggested is correct; the Visual Editor’s warning isn’t the problem.

According to the documentation for MQTT Trigger, trigger.payload is a valid way to reference the received payload. However, to get the value of trigger.payload you have to employ it in a template in the manner I posted above.

1 Like

I’m sorry, I totally misunderstood what your goal is. :flushed:

But after getting it, I can’t see any faults or errors in what you’re doing.

This is how I have set that up, and it works with Rhasspy:

rest_command:
  rhasspy_notification:
    url: http://192.168.178.46:12101/api/text-to-speech
    method: POST
    content_type: 'text/plain'
    payload: '{{ payload_text }}'

and I’m calling it with this in the template editor (in my setup I call it from an alert):

service: rest_command.rhasspy_notification
data: 
  payload_text: 'Tell me something'

I can’t see any difference between yours and mine, so it must be something different, maybe some setup in Rhasspy? Or the dumbest of all possibilities, the naming with “payload” vs. “payload_text”?

I’m thinking now that MQTT trigger only tests the message as a condition and can’t pass its content to the action. Perhaps MQTT sensor does that. If anyone has a way for trigger to pass the payload content to action, maybe they’ll give an example.

In the following example, whatever string is published to the test topic will appear in Home Assistant as a persistent notification.

- alias: example
  trigger:
  - platform: mqtt
    topic: test
  condition: []
  action:
  - service: notify.persistent_notification
    data:
      message: '{{ trigger.payload }}'

According to the documentation @123 linked above, why would than be “payload” available? I don’t think that would be available, if it won’t get passed through.

Have you checked the mqtt topic/message with ie. MQTT-Explorer? Not that the mqtt call would be something difficult, but who knows… :wink:

I’d suggest to go step-by-step, check the mqtt topic, check if HA accepts that topic and so on. Tedious debug work.

A short search here in the forum brought up nothing new, but this post:

mqtt-trigger-not-firing

Maybe you try that as well?