How to use data from a text file to select media in an automation

I don’t know if this would be possible, I have not been able to find an answer for it.

Here’s my problem:
I’m building a jukebox for the kids using RFID cards to play music. I have ripped all my CDs into my NAS, but I have hundreds of songs.
I don’t want to set up several hundred automations, one for each song, and I don’t want to set up an automation with hundreds of IF statements. The maintenance on this would be a headache.

Is there a way to put all the RFID id’s and song titles into a text file or database, and then have the automation scan the file, match the ID to the one in the file and play the corresponding song?

That would make it much easier to maintain, and if I need to change the automation, it would only have to be done in one place.

I am unaware of a way to read a text file in Home Assistant like you describe, but it could likely be done in Node Red.

Good point, thanks.
I haven’t used NodeRed in a while, but if I can’t find a clean HA solution, I might have to look at NodeRed again.

A script can accept data, so when you scan a card an automation calls a script passes the title along. Cards would need the title or a keyword that ties it to the song.
I have no idea how to actually play music, I don’t do that, but I’m guessing Music Assistant or some other player would finish that part out.

Thanks, I got the music playing part working, I just don’t want to repeat the automation part for each song.

I also use them in my config in a few places and my script blueprints…
GitHub - SirGoodenough/Home-Assistant-Config: My HA Configuration... This is what I run for production in my house..
GitHub - SirGoodenough/HA_Blueprints: 🧯 My Collection of Automation and Script Blueprints for Home Assistant 🧯.

There’s the file integration - you would need to configure your data as JSON:

Also the SQL integration:

Either of these may be useful if you want to stick to HA automations.

The file integration only reads the last line of the file every time the file is changed. It won’t grab a whole file.

The most simple native-HA solution is to maintain the database as a variable a script:

sequence:
  - variables:
      song_database:
        13648: "James Gang - Funk #49"
        14339: "Metallica - One"
        32245: "Queen - Fat Bottom Girls"
        43393: "Warren G - Regulate"
. . .
1 Like

You could use a combination of the command line integration and HAOS jq command to read a JSON file. Some examples of this being done here Anyway of reading JSON from config directory in Core - #23 by simonh4

Thanks, this looks interesting.
You’re saying basically to keep the data as a list inside the script, right?
How would I loop through the list to read the IDs?

I would like to keep everything native, since I already have a big HA installation, and I don’t want to add more bloat that I have to.

You don’t need to, just use the id in a template as part of your medial player invocation.

action: media_player.play_media
target:
  entity_id: media_player.chromecast
data:
  media_content_type: music
  media_content_id: " {{ 14339 }}" # modify template as required

Isn’t that just for the sensor… not the file.read_file action?

EDIT:

Just tested it… the action returns more than the last line of the file. There may be some limitation for response variable content length, but it returned all 7960 lines of my automations.yaml file.

2 Likes

The ID that I have is the ID read from the RFID tag, it looks like 74-10-37-94, so I would have to set up the list like:
74-10-37-94: "Song name"
then iterate through the list to find the ID and play the matching song.

Wow, this is new! Apparently the PR came in 2025.10. Thanks for pointing this out!

Well, I think the file.read_file action is a pretty good option now, but if you want to stick with the script option:

  1. In your automation that runs when an RFID tag is scanned, you’d call the new script and pass the RFID along in a variable:
- action: script.song_lookup_based_on_rfid
  data:
    rfid: "25-86-55-90"
  1. Use the passed-in variable name (which I called rfid) to look up the file name in the script:
alias: Song Lookup based on RFID
description: ""
sequence:
  - variables:
      song_database:
        74-10-37-94: "James Gang - Funk #49"
        25-86-55-90: "Metallica - One"
        04-49-44-87: "Queen - Fat Bottom Girls"
        72-18-81-22: "Warren G - Regulate"
  - action: persistent_notification.create
    metadata: {}
    data:
      message: "{{ song_database[rfid] }}"

But instead of creating a persistent notification you’d play the song (of course).

Yep, they’ve been sneaking in some good stuff under the radar this year.

1 Like

Awesome, thanks.
I’m going to take a look at it this weekend, also going to look at file.read_file which I was also unaware of. I’m leaning towards the file_read_file, so I don’t have to keep the full list in memory all the time.
I’ll give some updates when I try it.

Thanks to everyone for all the help.

Not sure that makes a difference.
I hope the automation actions are only loaded if the trigger and condition is met.
But don’t know for sure

yeah I agree it won’t make a different in terms of memory usage. Both methods will put the database into memory when the script is executed and then flush it afterwards.

Calling the script would be the same in either case, but here’s how the script would change if you want to read the database from a file:

alias: Song Lookup based on RFID
description: ""
sequence:
  - action: file.read_file
    data:
      file_name: /config/song_database.yaml
      file_encoding: YAML
    response_variable: file_content
  - action: persistent_notification.create
    metadata: {}
    data:
      message: "{{ file_content['data'][rfid] }}"

In this case, the song_database.yaml file doesn’t have any indentation or formatting, just rfid: song title

Thanks, that’s good to know.
I’ll make some time to play with both methods this weekend.

Thanks to everyone for the help.
I started to work on it, and I think that the file method would have worked, but while testing, I found a better solution for my problem.
I was using a NFC tag reader with a PN532 chip on it. That chip will only return the tag ID with ESPHome, that’s why I was trying to link the tag ID to the media.
While doing some research, I found a tag reader with a RC522 chip, this reader lets me read all the content from the tag with ESPHome. I ordered the reader and also order a NFC writer that I can connect to my PC.
Now I can write all the media information directly into the NFC card from my PC, including the Media ID, Artist, Title, etc. and read it back into ESPHome without having to do any table lookup.
This makes it a lot easier to maintain, since I don’t have to change the YAML every time I add a new song, plus it lets me add other media such as radio stations, videos, etc.

2 Likes