Parse a nested JSON with timeseries data using the RESTful integration

I’d be able to do this easily using pure Python, but I want to use Home Assistant to save the data into the DB and track it over time.

The JSON looks something like this

   "creation_forecast": "2023-12-22 07:42:46+01:00",
   "curve_forecast": {
      "data": [
            "timestamp": "2023-12-21 22:00:00+01:00",
            "astro": 651,
            "curveforecast": null,
            "measurement": 938
            "timestamp": "2023-12-22 11:50:00+01:00",
            "astro": 726,
            "curveforecast": 999,
            "measurement": null

The […] indicates that there are a lot of values in between. In fact this array is quite long and represents an object similar to what a weather forecast API would return.

I tried to add this in my configuration.yaml

  - resource: https://url_to/file.json
    scan_interval: 900
      - name: "Forecast"
        value_template: >-
          {% for index in value_json["curve_forecast"]["data"] -%}
            {{ index }}
          {%- endfor %}.
          - "timestamp"
          - "astro"
          - "curveforecast"
          - "measurement"

But I always get this error homeassistant.exceptions.InvalidStateError: Invalid state with length 157668. State max length is 255 characters.

You’re putting too much information into value_template. If you want the count to be the state…

        value_template: "{{ | length }}"

Right, you’re putting every index into the main state. So if your data is 500 listed items, your main state will be


instead of 500

Ok I understand why now.
This works, but it doesn’t parse the attributes but only set the status with the length of the array.

Is there a way to parse the attributes in a way they are defined as a weather forecast-like entity? Do I have to maybe use this Template Weather Provider - Home Assistant?

You’d have to make a template weather entity for that. The rest integration does not support templating the attributes.

Ok but first of all I need to store the response JSON of data somewhere in Home Assistant, right? Otherwise I cannot use it into a template weather entity.

Unfortunately the response is too large to be stored into an attribute. I tried doing this

      - name: "forecast"
        value_template: "{{ | length }}"
        json_attributes_path: "$.curve_forecast"
          - "data"

but I get the obvious warning

WARNING (Recorder) [homeassistant.components.recorder.db_schema] State attributes for sensor.forecast exceed maximum size of 16384 bytes. This can cause database performance issues; Attributes will not be stored

So, I’m not sure how this could be tackled with the default integrations. The idea I had at the beginning was to write my own Python script to run with pyscript-hacs, organize the results and save them into a state.

Your json_attributes_path should be able to filter down to selected elements. There’s a whole syntax language for that.

I understand what you’re proposing, and this works well for a single element, for example

json_attributes_path: "$[:1]"
  - "timestamp"

But it cannot parse an array. If you remove the [:1] nothing get stored in the state attribute. I’m trying to parse the entire array into the attributes. This may just not be the ideal solution…I don’t know how the weather forecast is stored for a weather entity, but this is exactly what I’m trying to achieve.