The objective is to create a dynamically populated list of radio stations in the Home Assistant dashboard. Each radio station can be played using the Music Assistant integration, and its corresponding logo will be displayed as a button. Instead of manually adding each radio station as a button beforehand, I aim to create a solution that automatically generates buttons based on radio station logos placed in a specific folder.
What I Have So Far
Folder Sensor I am using the folder platform sensor to detect and list all the image files (e.g., .jpg) in the /config/www/radio directory. Here is the configuration for the folder sensor:
This sensor retrieves a list of image files representing the radio station logos.
Template to Iterate Over Image Files The following Jinja2 template iterates over the file_list attribute of the folder sensor (sensor.radio). It generates a YAML structure for each radio station, including its name (derived from the image filename), the image URL, and a button configuration to play the station using the music_assistant.play_media service.
{%- set files = namespace(value=[]) -%}
{%- for item in state_attr('sensor.radio','file_list') -%}
{%- set files.value = files.value + [item] -%}
{%- set name = item.replace('/config/www/radio/', '').replace('.jpg','').replace('.png','') -%}
{%- set image = item.replace('/config/www', '/local')-%}
- type: picture
name: {{ name }}
image: {{image}}
tap_action:
action: call-service
service: music_assistant.play_media
service_data:
entity_id: media_player.dt
media_id: {{ name }}
media_type: radio
{% endfor %}
Template Output Below is an example of the YAML output generated by the template for two radio stations:
- type: picture
name: 1.FM - Chillout Lounge Radio
image: /config/www/radio/1.FM - Chillout Lounge Radio.jpg
tap_action:
action: call-service
service: music_assistant.play_media
service_data:
media_id: 1.FM - Chillout Lounge Radio
media_type: radio
- type: picture
name: Radio Swiss Pop
image: /config/www/radio/Radio Swiss Pop.jpg
tap_action:
action: call-service
service: music_assistant.play_media
service_data:
media_id: Radio Swiss Pop
media_type: radio
Question
How can I dynamically generate and display these picture buttons on the Home Assistant dashboard? Ideally, the solution should create these buttons automatically without requiring manual configuration whenever new radio station logos are added to the folder.
I’m looking forward solving the last part with the help of the community.
go to Developer tools/States and check if the folder sensor has the attribute with the expected file list, you can find the sensor by name sensor.radio
go to Developer tools/Template and copy the template part to see if it works correctly by generating the expected dashboard config.
I’m interested in your experience with the rendering times when the dashboard loads, is it acceptable for you? How many radio stations do you have?
Let me know if you need any further assistance, I’m glad to help.
Thank you for the write up and help with this. I’ve made some progress.
I tried copying the code as is and still nothing showed up.
I looked in the logs (Settings > System > Logs > Selected Home Assistant Core in the top-right). I got lost there, not knowing what to look for. I did search for “onkyo” because that was a unique word. There was an error and I’m providing the log at the bottom for reference.
I re-read your post and realized that I need to add the sensor.radio to the configuration.yaml file > verified configruation.yaml file in developer tools > Re-started Home Assistant > now the icons populate
I’ve recently started using scripts to make my setup more robust by separating actions from triggers. In this case, the triggers are the Radio Buttons, and the actions define what happens when a button is clicked.
The script leverages the Music Assistant Integration media player by calling play_media with the Radio Station Name as media_id and media_type: radio. In my setup, this turns on the Onkyo AVR and streams the selected Radio Station via AirPlay.
I’ve only been using this approach for a few days. Previously, I relied on the Net-TuneIn functionality on the Onkyo AVR for several years. However, all my favorite stations were lost overnight, so I came up with this new setup.
Here’s the script in case you’d like to replicate my setup:
alias: system_tune_onkyo_radio
variables:
volume: 0.37
sequence:
- action: music_assistant.play_media
data:
media_id: "{{ media_id }}"
media_type: radio
target:
entity_id: media_player.onkyo_2
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
enabled: true
- data:
entity_id: media_player.onkyo
volume_level: "{{ volume }}"
enabled: true
action: media_player.volume_set
mode: single
icon: mdi:radio
fields:
media_id:
selector:
text: null
name: media_Id
description: >-
URI or name of the item you want to play. Specify a list if you want to
play/enqueue multiple items.
description: ""
Did you already install the Music Assistant Addon + Music Assistant Integration?
In the Settings of Music Assistant I’ve installed the RadioBrowser Music Provider and Airplay under Player Providers.