I have a manually created template sensor which contains my streaming radio station data as an attribute in the sensor.
Radio Station Template Sensor
Note: My sensor actually has dozens of stations, it is just cut down here for the example.
- unique_id: radio_stations
name: Radio Stations
icon: mdi:radio
state: ok
attributes:
stations: >
{{
[
{
"name":"Flex 98.5 - Hip Hop",
"url":"https://streaming.live365.com/a23768",
"image":"https://media.live365.com/download/a255d860-3c9d-4aad-8a6c-867e082b1861.jpeg"
},
{
"name":"That 70's Channel",
"url":"https://ais-edge37-live365-dal02.cdnstream.com/a09646",
"image":"https://media.live365.com/download/bb807a58-fc07-4d1e-bda8-d814078f4113.png"
},
{
"name":"Top 80 Radio",
"url":"http://uk5.internet-radio.com:8011/stream",
"image":"https://ca0-cdn.onlineradiobox.com/img/l/5/43165.v3.png"
}
]
}}
The purpose of this sensor is to populate the radio stations input_selects for my radio launcher, and provide the matching stream and image urls when the radio stream is started. Everything works perfectly with the manually created sensor.
Station Selection Example
Automation to Populate Input Selects
- id: radio_station_update
alias: "[Radio] Station Update"
description: Update radio station input options.
mode: restart
variables:
selected_stations: "{{ expand('group.radio_selected_stations')|map(attribute='entity_id')|list }}"
stored_stations: "{{ expand('group.radio_stored_stations')|map(attribute='entity_id')|list }}"
trigger:
- platform: state
id: startup
entity_id: input_boolean.startup_pending
to: 'off'
- platform: state
entity_id: sensor.radio_stations
attribute: stations
action:
- service: automation.turn_off
target:
entity_id: &radio_automations
- automation.radio_store_station_selection # don't overwrite stored values
- automation.media_player_media_auto_resume # triggered by input_select.radio_station
data:
stop_actions: false
- service: input_select.set_options
target:
entity_id: &radio_selects
- input_select.radio_station
- input_select.media_preset_radio_wake
- input_select.media_preset_radio_morning
- input_select.media_preset_radio_sleep
- input_select.media_preset_radio_jason
- input_select.media_preset_radio_sheri
- input_select.media_preset_radio_shower
- input_select.media_preset_radio_company
- input_select.alarm_clock_radio_auto_alarm
- input_select.alarm_clock_radio_manual_alarm
- input_select.alarm_clock_radio_nap_alarm
data:
options: >
[{% for item in state_attr('sensor.radio_stations','stations') -%}
"{{- item.name }}"{{ ',' if not loop.last -}}{% endfor %}]
- repeat: # restore previous selection (values reset when options reloaded)
count: "{{ selected_stations|count|int }}"
sequence:
- choose:
- conditions: "{{ states(stored_stations[repeat.index-1])|lower not in ['','unknown','unavailable','none'] }}"
sequence:
- service: input_select.select_option
target:
entity_id: "{{ selected_stations[repeat.index-1] }}"
data:
option: "{{ states(stored_stations[repeat.index-1]) }}"
- service: automation.turn_on
target:
entity_id: *radio_automations
Automation to Store Current Station Selection
- id: radio_store_station_selection
alias: "[Radio] Store Station Selection"
description: Update stored radio station selection.
mode: restart
trigger:
- platform: state
entity_id: *radio_selects
action:
- service: input_text.set_value
target:
entity_id: "{{ trigger.entity_id|replace('input_select','input_text') }}"
data:
value: "{{ states(trigger.entity_id) }}"
Play Media Service Call
- service: media_player.play_media
data:
entity_id: "{{ player }}"
media_content_id: >
{% for item in state_attr('sensor.radio_stations','stations') -%}
{% if is_state('input_select.radio_station',item.name) %}
{{ item.url }}
{% endif %}
{% endfor %}
media_content_type: music
extra:
title: "{{ states('input_select.radio_station') }}"
thumb: >
{% for item in state_attr('sensor.radio_stations','stations') -%}
{% if is_state('input_select.radio_station',item.name) %}
{{ item.image }}
{% endif %}
{% endfor %}
What I would like to do is store this data in a text file in a csv format or similar format like this and then be able to turn this file into the stations attribute of the radio stations template sensor.
"Flex 98.5 - Hip Hop","https://streaming.live365.com/a23768","https://media.live365.com/download/a255d860-3c9d-4aad-8a6c-867e082b1861.jpeg"
"That 70's Channel","https://ais-edge37-live365-dal02.cdnstream.com/a09646","https://media.live365.com/download/bb807a58-fc07-4d1e-bda8-d814078f4113.png"
"Magic 80s Florida","http://airspectrum.cdnstream1.com:8018/1606_192","https://ca0-cdn.onlineradiobox.com/img/l/5/43165.v3.png"
Eventually the idea is to get to maybe create some sort of UI to manage (add/update/delete) streaming radio stations, but if I can at least get this working I can manage them with a spreadsheet until I figure out a UI.
Even better would be a scraper that could get this data automatically from popular radio stream sites so the even the text file could be automatically created with a few clicks in a UI. The current manual process is rather tedious. I’d like to eventually get to where my wife can maintain her HA radio station list as easily as her Spotify playlists.
But I digress. Any thoughts on getting the text file into a template sensor? I’m not even sure if this is possible or where to start really.