How to react on MQTT payload

Hi!
Probably this question is very simple to answer, but i’m struggeling with YAML, as it is completly new to me.

I want to do something like that:

if MQTT payload = "1" then
play song "a" on my sonos
else if MQTT payload = 2then
play song "b" on my sonos
else if .....

Receiving the message is working (ESP_RSID/ESPRFID/Tag : 1674124971).
I can also play songs on sonos, but I do not know how to code the if else part…

Thanks for helping!

What’s does the RFID tag have to do with receiving an MQTT payload?

https://www.home-assistant.io/docs/scripts/#if-then

Putting an Tag on the Reader sends that Tag via Mqtt to homeassistant. Depending on the Tag a Song is started

I’ll try. Soory, for some reason i didnt read that manual

Use an automation with an MQTT Trigger.

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.

{{ songs.get(trigger.payload) }}

Wow, thank you a lot! This helps a lot!

You’re welcome!

So does the received payload simply contain the tag id, like 1674124971, or it more complex, perhaps in JSON format or containing other information?

Can you post an example of the payload?

Also, how is each song’s information presented? Is it a URL to an internal/external resource?

Hi,
my time is very limited at the moment, so it will take a while to get it done, but the tag is only containig the id… should be simple :wink:

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.

Thanks for you help

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.

Just a guess but this is approximately what the template might look like:

media_content_id: 'plex://{ "library_name": "Music", "title_name": "{{ songs.get(tag_id) }}" }'

Works perfectly!!!
Thanks a lot!!

Hi!
one more question:
is it possible to select playlists and music?!

the following code only works for tag 2, as media_content_type: is music. If I change it to playlist, only tag 1 and 3 are working!

alias: RFID
trigger:
  - platform: mqtt
    topic: ESP_RSID/ESPRFID/Tag
condition:
  - condition: template
    value_template: "{{ trigger.payload in songs.keys() }}"
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.xxxx
    data:
      media_content_type: music
      media_content_id: "{{ songs.get(trigger.payload) }}"
variables:
  songs:
    "1": "plex://{ \"playlist_name\": \"meinePlaylist\" }"
    "2": >-
      plex://{ "library_name": "Hörspiele", "artist_name": "hörspielInterpret", "album_name": "AlbumName"}
    "3": "plex://{ \"playlist_name\": \"zweitePlayist\" }"

Add a template for the value of media_content_type.

The following example uses an Immediate If that reports playlist if it finds playlist_name in the song otherwise it reports music.

alias: RFID
trigger:
  - platform: mqtt
    topic: ESP_RSID/ESPRFID/Tag
condition:
  - condition: template
    value_template: "{{ trigger.payload in songs.keys() }}"
action:
  - variables:
      song: "{{ songs.get(trigger.payload) }}"
  - service: media_player.play_media
    target:
      entity_id: media_player.xxxx
    data:
      media_content_type: "{{ iif('playlist_name' in song, 'playlist', 'music') }}"
      media_content_id: "plex://{{ song }}"
variables:
  songs:
    "1": '{ \"playlist_name\": \"meinePlaylist\" }'
    "2": '{ \"library_name\": \"Hörspiele\", \"artist_name\": \"hörspielInterpret\", \"album_name\": \"AlbumName\"}'
    "3": '{ \"playlist_name\": \"zweitePlayist\" }'

Auch das funktionier einwandfrei!!!
danke für die Hilfe

1 Like

You’re welcome!

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.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

My Tasmota is reporting this

18:50:52 MQT: tele/tasmota_powerstrip/RESULT = {"Time":"2024-01-05T18:50:52","RfReceived":{"Data":"0x591F12","Bits":24,"Protocol":1,"Pulse":346}}

How do I react to the 0x591F12?