Sensor values or attributes from json array

I’m having a lot of trouble doing something that I’m sure is really simple.
I have read numerous posts and guides to this, but I’m getting either errors or all the data at once in one attribute.
Most other posts are about setting the sensor value from one record, by selecting the wanted value name.
After a week of messing around, I give up and come here for help.

json:

{
  "total": 13,
  "records": [
    {
      "Hour": "2022-11-22T11:00:00",
      "Price": 1666.890015
    },
    {
      "Hour": "2022-11-22T12:00:00",
      "Price": 1583.589966
    },

Ideally I would create a sensor with attributes for every record, with the attribute name being the index [0-number of records] or name being the value of Hour, and the value being Price.

I can create a sensor with the state being Price of the first record:

      resource: https://api.xxxx
      name: price_now
      value_template: "{{ value_json.records[0]['Price'] | round(2) | float / 1000  }}"

But I need to extract the Price from every record (in this case 13, but it varies) and store it somehow, for use in automations.
Of course I could create a long list of sensors, with state being the value of Price, but that seems less elegant and requires too many resource requests so I would get blocked.

    - platform: rest
      scan_interval: 3600
      resource: https://api.xxxxx.xx
      name: price_future
      value_template: "OK"

#      json_attributes:
#      - Hour
#      - Price

    - platform: template
      sensors:
        price_1:
        value_template: "{{ value_json.records[0]['Price'] | round(2) | float / 1000  }}"
        price_2:
        value_template: "{{ value_json.records[1]['Price'] | round(2) | float / 1000  }}"

gives me nothing.

    value_template: >
             {% for x in range(value_json.records|count) %}
               {{ value_json["records"][x]["Price"] }}
             {% endfor %}

also nothing…

      json_attributes:
      - price_future
{% for x in range(value_json.records|count) %}
{{ state_attr('sensor.price_future[x]', 'Price') }}
{% endfor %}

I think attributes must be declared before, so this will also not work.

Creating the sensors one by one is working for me now, from a single resource call:

      sensor:
      - name: "price_now" 
        value_template: "{{  (value_json.records[0].Price | float / 1000) | round(3) }}"
        device_class: monetary
        unit_of_measurement: "Kr"
      - name: "price_1"
        value_template: "{{ (value_json.records[1].Price | float / 1000) | round(3)  }}"
        device_class: monetary
        unit_of_measurement: "Kr"

etc.

I still have no idea how to add the Time to the sensor, but since they are spaced one hour apart I can get around that.

I’m sure there’s a better solution.

Not yet so it seems, multiple posts want a similar solution. Mine was different, I put the whole response in the attribute and then created graphs out of that…I only needed graphs.

1 Like

Thank you.
May I know how you created graphs from the json response?
I was wondering how to make a graph of all those sensors together, since they are spaced with one hour between them, it would be a nice touch.

There is no single answer on this as depends on data presented

As an example (!) Apexcharts allows you to page through attributes nicely and (!) it already is smart enough to not need to specify all. The trick (and this is indeed knowledge) is to know ‘how’ to do this.
For e.g. standard (!) weather sensors, having a forecast atttibure over mutliple days/hours, apexhcarts would need this

  - entity: weather.openweathermap
    name: Openweather
    data_generator: |
      return entity.attributes.forecast.map((entry) => {
            return [new Date(entry.datetime), entry.temperature];
          });

Not sure if I can help you to the end but if you provide a data set and what you want to achieve then … let’s see.
Lastly …if you have an attribute with json structure then jinja can do (almost) all to get data out.

EDIT: sadly not yet in full from the REST integration

Thank you, I will experiment some more.