How to use MQTT payload in an automation action

I have created an Automation (Settings > Automations and Scenes > Automations tab), and set the trigger to listen to an MQTT message:

I understand the payload setting is optional, as it can of course trigger on any payload sent to a given topic.

How can I use the message payload in the actual automation action?

See screenshot below, obviously I’ve just typed “msg.payload” into the “Content ID” field, but that’s where I want to actually use the incoming message payload from MQTT:

trigger.payload

Reference:
Automation Trigger Variables - MQTT

Or trigger.payload_json if the payload is in JSON format.


NOTE

In the future, post YAML instead of screenshots.

FAQ Guideline 11 - Format it properly

1 Like

Okay, so that doesn’t work for some reason.

Do I need to enclose in any special characters? Note it’s a plain text MQTT message.

Is there any way to output trigger.payload to e.g. the screen / a log, to confirm its state?

If I paste the actual payload that I’m sending, into media_content_id, and run the automation, it works.

How can I test further?

alias: Play YouTube Music
description: >-
  When MQTT message arrives, attempt to play youtube playlist present in message
  payload
trigger:
  - platform: mqtt
    topic: media/play/topliving
condition: []
action:
  - service: media_player.play_media
    data:
      media_content_type: playlist
      media_content_id: trigger.payload
    target:
      entity_id: media_player.ytube_music_player
mode: single

You need to use it in a Jinja2 template.

      media_content_id: '{{ trigger.payload }}'

Reference:
Templating