currently I’m struggeling to display a dynamic json list in my dashboards.
Lets say i have this json content which gets pulled via REST (which is working right now)
In the end I want to display each Movie (A,B,C) in a list with the state next to it (if its available, or not). The problem here is that the list of movies is dynamic, so it is possible to have e.g. MovieA to MovieG
Ive already set up several sensors from REST, but all of them are fixed and not dynamic.
One way would be to make a sensor that contains the value of the key ‘releases’. You’ll get a text sensor that is a json list of all your movies. Using templates you could dynamically display them in the UI.
Another way would be to create a lot of sensors based on the maximum numbers of movies you think there well be in the list at the same time. You could point each sensor to a specific index of the json list: if there is a movie defined for the index the sensor will get a name (saved as sensor attribute) and value (sensor value). If nothing is defined, the sensor will be unavailable I thnk (not sure).
Using a conditional card / auto entities card you could display them in the UI.
If you put the whole thing in one sensor as attributes, then you could iterate through it e.g. via a markdown card with templating.
EDIT, assume this is in the attributes of sensor.something
{% set movies = state_attr('sensor.something','releases')%}
{% for i in range(0, movies | count ) %}
{{movies[i]['name']}} - {{movies[i]['value']}}
{% endfor %}
will provide:
MovieA - True
MovieB - True
MovieC - false
in a markdown card you could use this by creating a table and putting these values in
Thanks - I tried your code with the “sensor.hacs” which has the same structure to test the iterate thing - it is working perfectly
The only question left is, how to create the releases sensor with the attributes from the json?
How do i create multiple name attributes with a dash infront? and how to nest them into the releases?
That I donot know as you donot provide the info how you are extracting the json with rest…does it end up in the state?..i.e. show a bit more info please, maybe dumps/screenshots of your sensor(s)?
And here is a screenshot of the sensor in the configuration yaml. Above the Update Stats resource there is another resource - both resources are in the same rest: Sensor.
As you see, currently I’m value_templating in a static way e.g. different update notifications for some systems.
How can this be accomplished? Or is it even possible to iterate trough the states and not trough the attributes (like you mentioned in your 1st post with the script)?
currently my sensor looks like this - but I dont know how to create the dynamic attributes:
So - Ive finally set up my json file which gets pulled via REST, and also the markdown card is in place.
Your mentioned code lists me the following:
{% set releases = state_attr('sensor.releases','releases')%}
{% for i in range(0, releases | count ) %}
[{{releases[i]['value']}}] {{releases[i]['name']}}
{% endfor %}
[False] MovieA
[False] MovieB
[False] MovieC
Is it possible with if conditions to output another value instead of “False”? e.g. “not released”?
I dont want to change this in the json, because I’m using it for another script also.