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.
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, 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.
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.
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.
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.
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 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.