Then process the received payload using the trigger.payload variable.
I would use a dictionary to determine which song to play based on the received tag id. Here’s a rough example of what I mean. It assumes the received payload contains the tag id and nothing else (if it does contain other information then the MQTT Trigger will require the use of its value_template option to extract the id).
alias: example
variables:
songs:
'1234567': song A
'9012345': song B
'1234567': song C
'7654321': song D
trigger:
- platform: mqtt
topic: your/topic/goes/here
condition:
- condition: template
value_template: "{{ trigger.payload in songs.keys() }}"
action:
- service: media_player.play_media
target:
entity_id: media_player.your_sonos_speaker
data:
media_content_type: music
media_content_id: "{{ songs.get(trigger.payload) }}"
The automation’s condition checks if the received tag id is in the songs dictionary.
The following line is what selects the song from the songs dictionary based on the received tag id.
ups, only read part of your post!
the payload is like that: ESP_RSID/ESPRFID/Tag : 1674124971
I’m running a plex server, so the songs or playlists will be from there.
In that case, the tag id must be extracted from the payload. If each tag id is always 10 characters long, it can easily be extracted like this:
alias: example
variables:
songs:
1234567890: song A
9012345678: song B
1234567890: song C
7654321012: song D
1674124971: song E
trigger:
- platform: mqtt
topic: your/topic/goes/here
variables:
tag_id: "{{ trigger.payload[-10:] }}"
condition:
- condition: template
value_template: "{{ tag_id in songs.keys() }}"
action:
- service: media_player.play_media
target:
entity_id: media_player.your_sonos_speaker
data:
media_content_type: music
media_content_id: "{{ songs.get(tag_id) }}"
In addition, if the tag id is always numeric, the dictionary’s keys should not be wrapped in quotes (I made that change in the new example).
Refer to the documentation for the Plex integration to see an example of how to play media from a Plex server on a Sonos speaker. As expected, the example I posted will have to be enhanced to support playing from Plex.
Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.