Automation for Playing Music (Spotify) by Scanning a RFID Card through Magic Cards Project

Having problem creating an automation which reads the data from the scanned card and pass it on to media_player.spotify_poras_chaudhary to play the relevant song from Spotify URI.

I’m running the Magic Cards Project on a raspberry pi and using an RFID card scanner to scan the cards.

When I scan a card I get the following data in the “magic_card_scanned” event -

Event 0 fired 1:39 PM:
{
    "event_type": "magic_card_scanned",
    "data": {
        "card_code": "0000314284",
        "card_type": "album",
        "card_arturl": "https://i.scdn.co/image/ab67616d0000b2734e2aa94e199e50bc8e10646f",
        "card_title": "Dil Se (Original Motion Picture Soundtrack)",
        "card_subtitle": "A.R. Rahman",
        "card_uri": "spotify:album:54NUwj7U1MOhA1ZGbnhiMz",
        "magic_cards_room": "Office"
    },
    "origin": "REMOTE",
    "time_fired": "2022-05-02T08:09:12.793694+00:00",
    "context": {
        "id": "43fc5c85a55945c08e686ae244ce4c6a",
        "parent_id": null,
        "user_id": "dd676a1912a24e2dab3d42cedda30267"
    }
}

Now, what should be my automation to play a song based on the data captured from the event?
my media player is: media_player.spotify_poras_chaudhary

Thanks!

Any help would be highly appreciated!

Whoever is seeing this please leave a comment so that I can post in this thread after some time to keep it alive. Home Assistant community is not what it used to be anymore. A lot of threads go uncommented and problems go unsolved. Please support!

Well, there are people that read your message, but not everyone can help with your case. I also want to enable playing spotify playlist on Sonos but by pressing a button as trigger. So far still searching

1 Like

How’s your search going?

1 Like

The project is sort of on-hold for me right now. Would love for someone to come up with a solution. Would be great. Thanks for dropping by.

Anyone here to help?

I’m not familiar with the Magic Cards process, but did find the following article on the GitHhub Magic Cards docs page on how to setup an automation once a card is scanned.

automation:
  - alias: Play favorite from Magic Cards
    trigger:
      - platform: event
        event_type: magic_card_scanned
        event_data:
          card_type: album
    action:
      - service: media_player.select_source
        data_template:
          entity_id: "media_player.{{ trigger.event.data.card_uri.split('|')[0] }}"
          source: "{{ trigger.event.data.card_uri.split('|')[1] }}"

Something seems off about the trigger.event.data.card_uri.split lines in that example though. I don’t think there is an entity_id: value in the card_uri value of the data block. The source: value looks solid, as it is returning the the spotify URI value to select. You may have to reach out to the Magic Cards doc for an updated example.

Hope it helps!

1 Like

Thanks Lucas,

So I got around doing it with the help of chat GPT and the automation works great, just as expected!

Here is the automation-


alias: Play Media on RFID Card Scan on Spotify
description: ""
trigger:
  - platform: event
    event_type: magic_card_scanned
action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.card_type == 'song' }}"
        sequence:
          - service: media_player.play_media
            data_template:
              entity_id: media_player.spotify_poras_chaudhary
              media_content_id: "{{ trigger.event.data.card_uri }}"
              media_content_type: music
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.card_type == 'playlist' }}"
        sequence:
          - service: media_player.play_media
            data_template:
              entity_id: media_player.spotify_poras_chaudhary
              media_content_id: "{{ trigger.event.data.card_uri }}"
              media_content_type: playlist
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.card_type == 'album' }}"
        sequence:
          - service: media_player.play_media
            data_template:
              entity_id: media_player.spotify_poras_chaudhary
              media_content_id: "{{ trigger.event.data.card_uri }}"
              media_content_type: album


Here’s a video of it in action-

cool. I’ve been playing around with this, as I was curious about event types. As it turns out, you can use the Events tab of developer tools to fire simulated events. So I setup the following:

then I setup an automation to trigger off the fired event:

alias: Magic Cards Event Test
description: Magic Cards Event Test
trigger:
  - platform: event
    event_type: magic_card_scanned
    event_data:
      card_type: album
condition: []
action:
  - service: spotifyplus.player_media_play_context
    data:
      entity_id: media_player.spotifyplus_todd_l
      context_uri: "{{ trigger.event.data['card_uri'] }}"
      offset_position: 0
      device_id: "*"
mode: single

And the album started playing on my SoundTouch device. :smiley:

Note that this uses my SpotifyPlus integration, and not the stock Spotify integration - no Spotcast necessary either.

Hope it helps!