Play Media related to MQTT Payload

Hi,
I am desperately trying to find a solution for my automation problem.
The MQTT payload should play a specific playlist on a media player.
I am struggeling to pass through the MQTT payload to the service call “media_player.play_media”
Could somebody please have a look on my existing code?
Thanks a lot in forward
trigger:

  • platform: mqtt
    topic: /spotify/schlafzimmer/liste
    condition: []
    action:
  • data:
    entity_id: media_player.nachttisch
    media_content_id: ‘{{states.sensor.spotify_list}}’
    media_content_type: playlist
    service: media_player.play_media#

Without the dinamic payload, which is stored in a MQTT sensor, the automation is working fine.
So the error should be somewhere in the media_content_id.

data_template

Try:

trigger:
  platform: mqtt
  topic: /spotify/schlafzimmer/liste
condition: []
action:
  data_template:
    entity_id: media_player.nachttisch
    media_content_id: ‘{{states.sensor.spotify_list.state}}’
    media_content_type: playlist
  service: media_player.play_media

I think service needs to be aligned with data_template. (back two spaces)

You can get the payload of the MQTT message directly in the automation using {{ trigger.payload }}

I don’t see any guarantee that the automation will run after the sensor is updated.

Thanks for the fast replies. I am still a beginner in editing automations :wink:

Due to the use of the hass.io automation editor, I am still struggeling to get it working.
Do I have to post the data_template within the editor?

I tried to modify the automation.yaml directly and it failed.

@gpbenton I will take care about the triggering in the second step. My first goal is to get the MQTT data into the media_player service.