what I really need is a select list of the names
and then from the name be able to send the URL to my media player.
instead of maintaining the player script manually
Paste in some real JSON, properly formatted (like your sensor is); and what you’d like the resultant output of the REST sensor to look like. We can’t use your example JSON above because you’ve pasted it as normal text which the forum software mangles with smart quotes, and I’m not re-typing it all.
Be aware that sensor states have a maximum length of 255 characters.
Sorry first time posting on a forum with code snips. Here it is made pretty - the API minified it I think it is all one line.
I need 1 list with Name
and then in the radio player script the Name and Url .
But I am not trying to get someone to solve it for me, I want to learn how to find the fault and fix it.
and later if possible - I don’t know enough about it yet, can I use CSS to stylize the drop down and put an icon in front of each name if the active flag is set. or sort active to the top.
In your template sensor you reference streams but that doesn’t exist. You want to use value_json, which is the response from the server read into a JSON object. Also, sensor states are limited to 255 characters, which you’ll quickly blow through with that approach. You can use sensor attributes, though: they can store arbitrary data structures like dictionaries and have a much higher size limit.
Here’s a starting point for copy/pasting into the template editor, with the example JSON read into a variable that I’ve called value_json for ease when moving it back into the sensor:
{% set value_json = [
{
"id": 1,
"Url": "http://us-il-chicago-1.listen.com:8000/10001",
"Encoding": "mp3",
"Name": "2022 Inspiration - General Use",
"Username": "#########",
"Password": "#########",
"Active": 0,
"AutoPlay": 0
},
{
"id": 40,
"Url": "http://us-ca-fremont-1.listen.com:8000/1924842",
"Encoding": "mp3",
"Name": "Alexanderfeld Congregation, Hillsboro, KS",
"Username": "#########",
"Password": "#########",
"Active": 0,
"AutoPlay": 0
}
] %}
Your template, adjusted:
{% for stream_dict in value_json %}
{{ stream_dict.Name }}
{{stream_dict.Url}}.{{stream_dict.Encoding}}
{{ stream_dict.Active }}
{% endfor %}
=======
A simple list of Names:
{{ value_json|map(attribute="Name")|list }}
=======
A dictionary of Name: URL:
{% set ns = namespace(d={}) -%}
{% for item in value_json -%}
{% set ns.d = dict(ns.d, **{item['Name']: item['Url']}) -%}
{% endfor -%}
{{ ns.d }}
If I can only get 3 items I can manually manage my list. This API only gives 15. Is there another way to get this API data into my system?
I guess there are really only a couple reasons that I want the API
the list can change from the services web interface,
the active flag shows when a stream is active which I don’t get otherwise.
Yes, of course: my suggestion was just to show that it’s working. You could set up a number of sensors: if you know there are 15 (or any fixed number), you could do something like: