Rest-Electical prices

Hi all

I am trying to get the current electrical prices from my electricity company that changes every hour of the day, unfortunately they don’t have a place where it says “price right now” so i have to pull out the data from every single hour over the span of 48 hours. The challenge is that at some random time the whole table shifts one day as the day after will have the new prices.

I have used postman to pull out the data to see what i get from that table view.
GET https://api.obviux.dk/v2/forward-prices/2880
headers:
content-type: application/json
x-customer-ip: 94.18.215.66 (don’t worry not my IP)

This will give the following output.

 {
     "0": {
         "valid_from": "2022-01-16 00:00:00",
         "grid_area": "DK2",
         "price": 123.86375
     },
     "1": {
         "valid_from": "2022-01-16 01:00:00",
         "grid_area": "DK2",
         "price": 123.0825
     },

(this is only two out of 48)

I have searched through the internet to find a way to only pull the data from the current date and hour, but havn’t been successful (i might be too stupid :P)

What i did was pulling every single field in the table out in a sensor like this.

 rest:
   resource: https://api.obviux.dk/v2/forward-prices/2880
   scan_interval: 86400
   headers:
     Content-Type: application/json
     x-customer-ip: 94.18.215.66
     user-agent: okhttp/4.8.0
   sensor:
     - name: "El Pris 0"
       device_class: monetary
       unit_of_measurement: "DKK"
       json_attributes_path: "$.[0]"
       value_template: '{{ value_json["0"]["price"] }}'
       json_attributes:
         - valid_from
         - price
     - name: "El Pris 1"
       device_class: monetary
       unit_of_measurement: "DKK"
       json_attributes_path: "$.[1]"
       value_template: '{{ value_json["1"]["price"] }}'
       json_attributes:
         - valid_from
         - price

This leaves thease sensors in HA

The problem with this is that it takes AGES to load HA because of this.
I would really like to find a way where i only have 1 sensor with the current date and time.

Is there anyone with a master brain that can help me figure out what to do?

You need to loop the array and find the valid_from that is less than one hour old or a few minutes in to the future (I guess)?
Or do you want more than one value? Prev hour, now and next?

Yes, i want to loop the array until i find the current hour. So i get the price from between 13:00 and 14:00 which is valid from 13:00:00

I believe something like this?

{% set ns = namespace() %}
{% set ns.price = 0 %}
{% for index in value_json %}
  {% if prices[index]["valid_from"] == now().timestamp() | timestamp_custom("%Y-%m-%d %H:00:00") %}
    {% set ns.price = prices[index]["price"] %}
  {% endif %}
{%- endfor %}

{{ ns.price }}

It seems to work when I have this:

Hi Thanks

I tried to add it as a value template, unfurtunately the sensor only reports “unknown”
Do you think it is posible to grap the right price directly from the Rest call?

rest:
  resource: https://api.obviux.dk/v2/forward-prices/2880
  scan_interval: 86400
  headers:
    Content-Type: application/json
    x-customer-ip: 94.18.215.66
    user-agent: okhttp/4.8.0
  sensor:
    - name: "El pris Lige nu"
      device_class: monetary
      unit_of_measurement: "DKK"
      value_template: >
        {% set ns = namespace() %}
        {% set ns.price = 0 %}
        {% for index in value_json %}
          {% if prices[index]["valid_from"] == now().timestamp() | timestamp_custom("%Y-%m-%d %H:00:00") %}
            {% set ns.price = prices[index]["price"] %}
          {% endif %}
        {%- endfor %}
        {{ ns.price }}
      json_attributes:
        - valid_from
        - price

I don’t know what could be the issue.
It seems to work when I test it in the developer tools.

Changed prices[index] to value_json[index] and now it works. Thank you very much.

- name: "El pris Lige nu"
      device_class: monetary
      unit_of_measurement: "DKK"
      value_template: >
        {% set ns = namespace() %}
        {% set ns.price = 0 %}
        {% for index in value_json %}
          {% if **value_json[index]["valid_from"] == now().timestamp() | timestamp_custom("%Y-%m-%d %H:00:00") %}
            {% set ns.price = value_json[index]**["price"] %}
          {% endif %}
        {%- endfor %}
        {{ ns.price }}

Off course!
My bad. It was because I had to test it with a variable.

One more question though. In the end of the output there is distribution prices, in a perfect world i want the right distribution price to be added to the current electrical price.

{
    "0": {
        "valid_from": "2022-01-17 00:00:00",
        "grid_area": "DK2",
        "price": 14.052500000000002
    },
    "distribution_prices": [
        {
            "price_format": "Øre/kWh inkl. Moms",
            "company": "radius",
            "prices": [
                {
                    "end_time": "01:00",
                    "start_time": "00:00",
                    "price": 156.06999999999999
                }
            ]
        }
    ]
}

I tired to apply same logic as before, but as this output is alittle bit different, i can only get the whole table out at once. Do you know if there is a way you can filter the output?

Is it always one hour windows at that part too?

Yes, most of the time the numbers are identical except for 3 hours a day from 1/10 to the 31/03, but they might change it in the future so i think id better be prepared :slight_smile:

I think this could work.

    - name: "El pris Lige nu"
      device_class: monetary
      unit_of_measurement: "DKK"
      value_template: >
        {% set ns = namespace() %}
        {% set ns.price = 0 %}
        {% for index in value_json["distribution_prices"][0]["prices"] %}
          {% if value_json["distribution_prices"][0]["prices"][index]["start_time"] == now().timestamp() | timestamp_custom("%H:00") %}
            {% set ns.price = value_json["distribution_prices"][0]["prices"][index]["price"]
          {% endif %}
        {%- endfor %}
        {{ ns.price }}

If there is anything behind value_json in the foreach statement it returns UndefinedError: ‘value_json’ is undefined

Any way to get past that, I was trying to find a way to make the foreach look under distribution prices yesterday, but didn’t succeed, ofcause my coding experiance is very limited.

I can’t even add the configuration you posted before in my config because it fails.

But perhaps it needs to be something like this or a variant of it.

{% for index in value_json.distribution_prices[0].prices %}

Disregard last message.

As i’m testing in developer tools i forgot to change value_json to the varible with the table. but there is an issue.

It gives me a
UndefinedError: list object has no element {'end_time': '01:00', 'start_time': '00:00', 'price': 156.07}

I added one more field with current time. and it only shows table of the first.

{% set transportprices = {
    "0": {
        "valid_from": "2022-01-17 00:00:00",
        "grid_area": "DK2",
        "price": 14.052500000000002
    },
    "distribution_prices": [
        {
            "price_format": "Øre/kWh inkl. Moms",
            "company": "radius",
            "prices": [
                {
                    "end_time": "08:00",
                    "start_time": "07:00",
                    "price": 156.06999999999999
                },
                {
                    "end_time": "09:00",
                    "start_time": "08:00",
                    "price": 156.06999999999999
                }
            ]
        }
    ]
}
%}

This is what I expected the previous answer to work like, but it didn’t.
Now this seems to work like this:

    - name: "El pris Lige nu"
      device_class: monetary
      unit_of_measurement: "DKK"
      value_template: >
        {% set ns = namespace() %}
        {% set ns.price = 0 %}
        {% for index in value_json["distribution_prices"][0]["prices"] %}
          {% if index.start_time == now().timestamp() | timestamp_custom("%H:00") %}
            {% set ns.price = index.price %}
          {% endif %}
        {%- endfor %}
        {{ ns.price }}

This is great. THANK you so much.

it ended up looking like this.

{% set ns = namespace() %}
{% set ns.price = 0 %}
{% for index in prices %}
  {% if prices[index]["valid_from"] == now().timestamp() | timestamp_custom("%Y-%m-%d %H:00:00") %}
    {% set ns.price = prices[index]["price"] %}
  {% endif %}
{%- endfor %}

{% set ns.transportprice = 0 %}
{% for index in prices["distribution_prices"][0]["prices"] %}
  {% if index.start_time == now().timestamp() | timestamp_custom("%H:00") %}
    {% set ns.transportprice = index.price %}
  {% endif %}
{%- endfor %}

 {{ (ns.price / 100) + (ns.transportprice / 100) | float }}

And the sensor looks like this.

rest:
  resource: https://api.obviux.dk/v2/forward-prices/2880
  scan_interval: 60
  headers:
    Content-Type: application/json
    x-customer-ip: 94.18.215.66
    user-agent: okhttp/4.8.0
  sensor:
    - name: "El pris Lige nu"
      device_class: monetary
      unit_of_measurement: DKK/kWh
      value_template: >
        {% set ns = namespace() %}
        {% set ns.price = 0 %}
        {% for index in value_json %}
          {% if value_json[index]["valid_from"] == now().timestamp() | timestamp_custom("%Y-%m-%d %H:00:00") %}
            {% set ns.price = value_json[index]["price"] %}
          {% endif %}
        {%- endfor %}
          
        {% set ns.transportprice = 0 %}
        {% for index in value_json["distribution_prices"][0]["prices"] %}
          {% if index.start_time == now().timestamp() | timestamp_custom("%H:00") %}
            {% set ns.transportprice = index.price %}
          {% endif %}
        {%- endfor %}
          
        {{ (ns.price / 100) + (ns.transportprice / 100) | float }}

This can be used by others having varible prices through Seas-nve in eastern denmark. (you can change the postal code in the API URL)

The only thing I would have wanted was to have the distribution price and consumption price in the attributes separated.
Not sure if that is possible

Yeah that would have been awesome, but atleast the base function is in place now.

I know that is possible in Node red, but I don’t know how to do it in yaml.