Rest sensor problem, how to give json array parent name with phyton?

Hello,

I have read more about this problem, but till now all the solutions I found did not work for me. And since i am new to programming, last few nights I spent a lot of time on a steep learning curve. I got to the point I wanted to ask for some help.

I’m a customer of the dutch electricity company Easyenergy. They change the price of electricity every hour. My goal is to use some machines in my house only in the cheapest hours. So I would like to have a few sensors, for the 2/ 4 or 6 cheapest hours.

I try to use the REST sensor.

They have an API that I can use. The API needs a (variable) timestamp. That part I already solved.

My problem starts with the Json that returns from the API. It is an array of objects, where each object contains a timestamp and some prices.

But, the array has no parent name. And the Rest sensor appears to need a parent name to be able to use attributes. At least that is what I found in all the examples where other users calculated the cheapest price from their electricity company.

I did successfully made a sensor that gets the price now.

  - platform: rest
    name: EasyEnergy
    resource_template: >-
      https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp=
      {% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
      {{ (ts_now - 3*60*60 ) |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ') }}&endTimestamp=
      {{ (ts_now + 1*60*60 ) |timestamp_custom ('%Y-%m-%dT%H:%M:%SZ') }}
    unit_of_measurement: 'ct/kWh'
    value_template: "{{ (value_json[-3].TariffUsage) | multiply(100) | round(2) }}"

But, as told, this way I only get 1 object, the time now, and I want to get all the objects of 24 hours, to be able to calculate the cheapest.

I already found some examples for the calculations, for example in de Awwater topic.
https://community.home-assistant.io/t/problem-in-building-a-json-list-of-data/171736

But here the array has a Title, and attributes uses the title

I also found out that there are two possible solutions:

1, publish the json on mqtt, and create a sensor on the MQTT,

  1. use phyton to give the array a parent name.

Since I’m new to programming I got confused in the part where I had to use phyton. Just a second learning curve pushed me back a bit. And I just thought, maybe someone can help me out.

This is an example of the json data:

[
      {
         "SupplierId": "0",
         "TariffReturn": "0.10503",
         "TariffUsage": "0.127086300000000000",
         "Timestamp": {
            "DateTime": "2021-09-09T00:00:00Z",
            "OffsetMinutes": "0"
         }
      },
      {
         "SupplierId": "0",
         "TariffReturn": "0.10188",
         "TariffUsage": "0.123274800000000000",
         "Timestamp": {
            "DateTime": "2021-09-09T01:00:00Z",
            "OffsetMinutes": "0"
         }
      },
      {
         "SupplierId": "0",
         "TariffReturn": "0.10316",
         "TariffUsage": "0.124823600000000000",
         "Timestamp": {
            "DateTime": "2021-09-09T02:00:00Z",
            "OffsetMinutes": "0"
         }
      }
]

This is an example of a fixed api

https://mijn.easyenergy.com/nl/api/tariff/getapxtariffs?startTimestamp=2020-04-28T22%3A00%3A00.000Z&endTimestamp=2020-04-29T22%3A00%3A00.000Z&grouping=

Update:

Okay, I found out that the phyton code is real simple within phyton, only question remains, how to integrate this in Yaml, within the rest sensor…


data_set = [{"key1": 1}, {"key2": 2}]

final set = {'Main Parent': data_set}

This also seems to be about my topic:
Help with REST that are contained within brackets - Configuration - Home Assistant Community (home-assistant.io)

1 Like

I’m curious if you found a solution for this, I have the same problem.