Template on RESTful sensor

Hi,

I have a RESTful sensor connected to the Domotz Public API. When querying for the internet speed, the reply is an array of the measurements of the last 7 days (see below for an example). I need to get the last element from that array, can I do this in a template? There is no way to be sure in advance how many measurements will be sent through.

The api call does have a way to time-limit the responses to less than 7 days, but that would require me to use a template in the resource: part of the sensor, which does not seem to be possible.

Additional issue: the reply is likely to be longer than 255 characters, so I need to pass this through json_attributes, but I don’t know how to do this as there is no named object at the top level.

Sample of the array:

[
    {
        "timestamp": "2019-04-05T19:20:58+00:00",
        "values": [
            186969402,
            19004487
        ]
    },
    {
        "timestamp": "2019-04-05T20:39:29+00:00",
        "values": [
            180379702,
            18999822
        ]
    },
    {
        "timestamp": "2019-04-05T21:59:37+00:00",
        "values": [
            175382534,
            19045850
        ]
    },
    {
        "timestamp": "2019-04-06T02:39:29+00:00",
        "values": [
            189920953,
            19108310
        ]
    },
    {
        "timestamp": "2019-04-06T08:39:31+00:00",
        "values": [
            188248922,
            19089989
        ]
    }
]

you can do something like

sensor:
  - platform: rest
    resource: whatever
    method: whatever
    payload: whatever
    value_template: '{{ value_json[-1].timestamp }}'
    json_attributes:'{{ value_json[-1] }}'

That should get you a sensor with the state of the timestamp, and 2 attributes, timestamp and value.

Thanks for the tip, this can help me get the value. However, using templates in json_attributes does not seem to work for me.

So I ended up using below configuration. Only downside is that I have to make 2 sensors as parsing the value afterwards does not seem to be possible either (it is passed as a string, not an object it seems).

  - platform: rest
    resource: SERVER_HERE/public-api/v1/agent/249612/history/network/speed
    name: Download Speed
    scan_interval: 3600
    headers:
      x-api-key: 'KEY HERE'
      Accept: 'application/json'
    value_template: '{{ (value_json[-1]["values"][0] | float / 1048576) | round(1) }}'
    unit_of_measurement: "Mb/s"
  - platform: rest
    resource: SERVER_HERE/public-api/v1/agent/249612/history/network/speed
    name: Upload Speed
    scan_interval: 3600
    headers:
      x-api-key: 'KEY HERE'
      Accept: 'application/json'
    value_template: '{{ (value_json[-1]["values"][1] | float / 1048576) | round(1) }}'
    unit_of_measurement: "Mb/s"