Display dynamic json entries

Good morning,

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)

{"releases": [{"name": "MovieA", "value": "True"}, {"name": "MovieB", "value": "True"}, {"name": "MovieC", "value": "false"}]}

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.

What would be the best way to accomplish that?

BR thomas

I think there are several options:

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

releases: 
  - name: MovieA
    value: 'True'
  - name: MovieB
    value: 'True'
  - name: MovieC
    value: 'false'
{% 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)?

Sure - here is the final json with the releases section included:

{"updates":[{"name":"pihole","update_available":""},{"name":"prtg","update_available":""},{"name":"truenas","update_available":""},{"name":"homeassistant","update_available":"Update verfügbar!"},{"name":"plex","update_available":""},{"name":"unifi","update_available":""}],"info":{"name":"lastrun","value":"05/11/2022 - 15:00:10"},"energy":[{"name":"daily_consumption","value":15.633},{"name":"init_value","init_value":"3182.13"},{"name":"yearly_consumption","value":3521.326}],"releases":[{"name":"MovieA","value":"True"},{"name":"MovieB","value":"True"},{"name":"MovieC","value":"False"}]}

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.

create one sensor from the source html and put ‘releases’ as attrubute?

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:

I did this quickly…

  1. create a test.json on /www (local)…copie dyour json in it
  2. created below sensor
  - platform: rest
    resource: http://192.168.1.20:8123/local/test.json
    name: testmovies2
    unique_id: testmovies2
    value_template: "OK"
    json_attributes:
      - releases

results in a sensor that can then be used with my above mentioned proposal on card

Perfect - thank you very much - its working :slight_smile:

And now the card…some joggling with template and css and html :slight_smile:

As I have this sort-off ready for anotter thing… you can also add if statement and use the css for coloring or other styles.

type: markdown
content: |
  <div>Movies</div>
    <table>
    {% set movies = state_attr('sensor.testmovies2','releases')%}
  <tr>
  <td><h4>Name<h3></td>
  <td><h4>Shown</td>
  </tr>
    {% for i in range(0, movies | count ) %}
    <tr>
    <td>
    {{ movies[i]['name'] }}</td>
    <td>{{ movies[i]['value'] }}</td>
  </tr>
  {% endfor %}
card_mod:
  style:
    .: |
      ha-card ha-markdown {
        padding:0px
      }
    ha-markdown $: |
      h1 {
          font-weight: normal;
          font-size: 24px;
      }
      div {
          background-color:rgb(100, 100, 100);
          padding: 12px 12px;
          color:white;
          font-weight:normal;
          font-size:1.2em;
            border-top-left-radius: 5px; 
            border-top-right-radius: 5px; 
      }
      table{
        border-collapse: collapse;
        font-size: 0.9em;
        font-family: Roboto;
        width: auto;
        outline: 0px solid #393c3d;
        margin-top: 10px;
      } caption {
          text-align: center;
          font-weight: bold;
          font-size: 1.2em;
      } td {
          padding: 5px 5px 5px 5px;
          text-align: left;
          border-bottom: 0px solid #1c2020;
      }
      tr {
          border-bottom: 0px solid #1c2020;
      }
      tr:nth-of-type(even) {
          background-color: rgb(54, 54, 54, 0.3);
      }
      tr:last-of-type {
          border-bottom: transparent;
      }
      mark {
          background: lightgreen;
          color: #222627;
          border-radius: 5px;
          padding: 5px;
      }
      span {
          background: orange;
          color: #222627;
          border-radius: 5px;
          padding: 5px;
      }
      span {
          padding: 5px;
      }
      tr:nth-child(n+2) > td:nth-child(2) {
        text-align: left;
      }

image

Perfect, thank you :slight_smile:

when done, please mark my last post as ‘solution’ …this helps also others when searching

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.

of course… something alike

{% if releases[i]['value'] == 'false' %}
not released
{% else %}
released
{% endif %}

Thank you - was messing arround with the brackets - now its working :slight_smile:

good… well…as per my earlier request, please mark my post for ‘solution’… I will detach from this one now