Magic Cards - Queue music, play movies, or trigger events with RFID cards

I have a script in home assistant which requires me to give the full path to a file on my NAS server in double quotes, e.g.:
file: "smb://192.168.1.5/Movies/movie.avi"

for that, I am using an automation that runs when a trigger event is detected, part of it is as follows:

item:
  file: "{{ trigger.event.data.card_uri }}"

the problem is, that I cannot get magic cards to deliver the double quotes as well - although I am adding the double quotes within the Card URI field within magic cards - which are necessary for the file path on the smb share in order to play the file. any suggestions how I could solve it?

You donā€™t need the double quotes.

hmm, you are right. i dont need them in the hassio config. just tested it. but then might there be a problem with the ā€œserialized dataā€ which gets delivered from the Card URI field?

the script in hassio with the file smb://192.168.1.5/Movies/movie.avi works but when putting the same path into Card URI, and testing it, nothing happens.

any idea what might be going wrong? does serialized data ignore upper/lower cases or empty spaces in, e.g. file names?

Nope, it just passes the string.

You can check the logs to see what data the event is getting in hass.

hassio logs say the following:

> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.components.http.view] Serving /api/events/magic_card_scanned to ------------ (auth: True)
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event magic_card_scanned[R]: card_code=0800465ac1, card_type=movie, card_arturl=, card_title=frozen, card_subtitle=princess, card_uri=smb://192.168.1.11/Movies/movie.avi, magic_cards_room=Living Room>
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.components.automation] Executing Play Magic Card Movie
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=Play Magic Card Movie, message=has been triggered, domain=automation, entity_id=automation.play_magic_card_movie>
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.helpers.script] Script Play Magic Card Movie: Running script
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.helpers.script] Script Play Magic Card Movie: Executing step call service
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=remote, service=turn_on, service_data=activity=31765771, entity_id=['remote.harmony_hub']>
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]>
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.helpers.script] Script Play Magic Card Movie: Executing step call service
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=media_player, service=kodi_call_method, service_data=entity_id=media_player.kodi, method=Player.Open, item=file={{ trigger.event.data.card_uri }}>
> 2018-09-11 00:21:41 ERROR (MainThread) [homeassistant.components.media_player.kodi] Run API method media_player.kodi.Player.Open({'item': OrderedDict([('file', '{{ trigger.event.data.card_uri }}')])}) error: {'code': -32602, 'message': 'Invalid params.'}
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event kodi_call_method_result[L]: entity_id=media_player.kodi, result=code=-32602, message=Invalid params., result_ok=False, input=method=Player.Open, params=item=file={{ trigger.event.data.card_uri }}>
> 2018-09-11 00:21:41 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]>

ĀÆ(Ā°_o)/ĀÆ it looks like the right data is going across just fine. You might want to check your script.

:grin:

the script does work and magic cards would also work if I just use the card_uri for running the script instead of running the file. But this would be more of a workaround, as this way, I would have to create an automation and script within HASS for every movie that I would like to map to a card.

when looking at the logs more closely, I now believe that there is a problem with the Kodi component itself, as it does not seem to understand this part:
{{ trigger.event.data.card_uri }}

The logs say (see the error line in the logs above) that the kodi component is trying to run the file ā€œ{{ trigger.event.data.card_uri }}ā€ instead of the content of the card_uri. Do you agree or believe something else might be the issue?

Would it be possible to use other rfid reader as input device, e.g. RC522 or PN532?

If the device shows up to the computer as a simple keyboard, like this RFID scanner does, then yes.

Just be sure that the scanner returns the same unique ID under the computer youā€™re using to create cards, and the raspberry pi. The RFID card scanner, out of the box, was in a mode that didnā€™t work well for both macOS and Linux. After changing itā€™s mode, it did.

You just want to make sure the card id youā€™re adding to your cards in the UI is the same that will be read when scanned in your pi setup.

This seems not to be possible except when abstracting the RC522 by a self made driver which exposes a keyboard interface.
What about interfacing with a mqtt broker? This would allow a lot more flexibility to distribute the components involved.

Hi @maddox ,

I would like to use the same card design as it can be seen on the magic cards website (picture above and title below).

is there a template you can share with us so that we can just swap the image and replace the title?

The card template is discussed in the docs - https://github.com/maddox/magic-cards/blob/master/docs/cards.md#printing-cards

1 Like

Hi.

I didnā€™t have any printers that supported printing directly on the rfid cards, so instead of buying another printer just for this, I went a little ā€œold schoolā€. I bought A4 label sheets, the closest match I got in the local store, was the Avery 3427. Then I went to the Avery site and used their free Design&Print online solution. Set it up using your label sheet type as the template and go from there.


Tweaked this as good as I could, and then printed the sheets, taped these on the rfid card, and use a sharp(!) knife to cut around the edges. Not an optimal solution, but a lot cheaper than buying a printer :slight_smile:

(The frame on the wall contains the rfid-card scanner. Need to get some paint on the cable tough :slight_smile: , but the WAF is fairly good on this )

3 Likes

Thank you for your awesome work. I got it all up and running yesterday and after connecting to HA, making a simple automation (read cardā€”> toggle light) and creating a card it works. But only when i trigger it thrugh the Test Card button. When I scan the card it does nothing.
I programmed the reader via the tool on a windows machine and ading the ID Tag during the card creation also worked like a charm.
How do i troubleshoot this? Maybe the returned ID on the Pi isnā€˜t correctly formatted? Can i see somwhere what the read ID is? Cheers

You could try plugging the pi into a tv/monitor and getting a shell. Then scanning the card and seeing the actual input that is used.

You could also check the logs for any errors.

thank you for your quick reply. unfortunately i run it on Raspbian Stretch Light and donā€™t have a desktop (i should add that Iā€™m not that experienced with linux yet). Do you know of some other was to check what the actual input is? Also, where do I find the logs?

Really sorry to bother you with such basic questions, but I feel like iā€™m really close to getting it to work and being stuck now is really frustrating :wink:

edit: ok, i (think) found the logs: it states

Sep 24 19:32:47 MagicCardsPi mtp-probe: checking bus 1, device 3: ā€œ/sys/devices/platform/soc/20980000.usb/usb1/1-1ā€
Sep 24 19:32:47 MagicCardsPi mtp-probe: bus: 1, device: 3 was not an MTP device
Sep 24 19:32:55 MagicCardsPi mtp-probe: checking bus 1, device 4: ā€œ/sys/devices/platform/soc/20980000.usb/usb1/1-1ā€
Sep 24 19:32:55 MagicCardsPi mtp-probe: bus: 1, device: 4 was not an MTP device

It boots to a shell. So youā€™d just have the CLI stuff on your TV/monitor with a prompt to log in. :slight_smile:

Try running ls -al /dev/input and paste the output in here.

Hi, wow youā€™re fast :smiley: I actually just got it working. Found the log, turned out the ID it read was another that was added automatically in the Card Code field. Swapped those, scanned the card again and voila, light. I mustā€™ve set up the 2nd cardreader wrong or something (duh!). Anyway, it works, iā€™m happy :smiley:
Iā€™m off to writing automations now and setting up the sonos stuff. Thank you again for your help!

Aaand hit another wall. When trying to set up an automation in HA with media_player.select_source it doesnā€™t pass along the string in trigger.event.data.card_title but instead gets:

Invalid service data for media_player.select_source: Entity ID media_player.{{ trigger.event.data.card_title }} is an invalid entity id for dictionary value @ data[ā€˜entity_idā€™]. Got ā€˜media_player.{{ trigger.event.data.card_title }}ā€™

I took the syntax from the magic-cards github, is there something Iā€™m missing?

edit: sorry for spamming today

btw my automation looks like this:

  • id: ā€˜1537819931547ā€™
    alias: Magic Cards Hardcore Playlist
    trigger:
    • event_data:
      card_type: album
      event_type: magic_card_scanned
      platform: event
      condition:
      action:
    • data:
      entity_id: ā€œmedia_player.{{ trigger.event.data.card_title }}ā€
      source: ā€œ{{ trigger.event.data.card_subtitle }}ā€
      service: media_player.select_source

Final Edit: wow, iā€™m an idiot. ā€œdataā€ is supposed to be ā€œdata_templateā€. thatā€™s what you get for writin stuff yourself instead of just copy&paste i guess. but at least i learned something today