Rest-Electical prices

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.

Yeah i have tried most of the day trying to find a way to add these two numbers as an attribute, but i havn’t succeeded… yet.

You probably can do it with the API but it won’t be persistent.
An option is three entities one for each and one for sum.
Or Node red.

Hi,
I have been using this api for quite a while now and very happy with that.
But it has stopped working recently.
Any knowledge of the reason, and how to fix it if possible,
or is there another alternative API for pulling prices from Andel energy for example.

Yeah i switched over to the nordpool integration using HACS to install it.

i have added the additional prices in order to give me the correct prices. They are as follows.

'{% set s = {
"afgift": 0.1,
"winter_lavlast": 0.2296,
"winter_hojlast": 0.6889,
"winter_spidslast": 2.0666,
"summer_lavlast": 0.2296,
"summer_hojlast": 0.3445,
"summer_spidslast": 0.8955,
"andel": 0.05,
"afrunding": 0.06
}
%}
    {% if now().month >= 4 and now().month <10 %}
        {% if now().hour >= 0 and now().hour <6 %}
            {{s.summer_lavlast+s.afgift+s.andel+s.afrunding|float}}
        {% elif now().hour >= 17 and now().hour <21 %}
            {{s.summer_spidslast+s.afgift+s.andel+s.afrunding|float}}
        {% else %}
            {{s.summer_hojlast+s.afgift+s.andel+s.afrunding|float}}
        {% endif %}
    {% else %}
        {% if now().hour >=0 and now().hour <6 %}
            {{s.winter_lavlast+s.afgift+s.andel+s.afrunding|float}}
        {% elif now().hour >= 17 and now().hour <21 %}
            {{s.winter_spidslast+s.afgift+s.andel+s.afrunding|float}}
        {%else%}
            {{s.winter_hojlast+s.afgift+s.andel+s.afrunding|float}}
        {% endif %}
    {% endif %}'

I have done a webscrape of radius webpage to get the current tariffs and i am working to integrate it into the additional costs in nordpool as well.

Hi @PuppetMaster,

Could you share the Scrape sensor for tarrif, or which url do you find these?

Hi @ghassan

Of cause, i haven’t figured a way to incorporate into the NordPool integration as there is a problem with quotations(as the nordpool integration requires additional costs to be in single quotations).

Settings → Devices & Services → + Add Integration → Search for Scrape
when you have added the first resource (Radius webpage) and one sensor, you can add more sensors on the scrape integration it self by clicking “configure”

Web Resource
URL Resource: https://radiuselnet.dk/elnetkunder/tariffer-og-netabonnement/
Method: GET
Leave rest with default settings.

Scrape sensors:
Tariff Vinter Lavlast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(2)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

Tariff Vinter højlast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(3)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

Tariff Vinter spidslast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(2) > td:nth-child(4)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

Tariff Sommer Lavlast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(3) > td:nth-child(2)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

Tariff Sommer højlast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(3) > td:nth-child(3)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

Tariff Sommer Spidslast:
Selector: #elementor-tab-content-8881 > table:nth-child(1) > tbody > tr:nth-child(3) > td:nth-child(4)
Value Template: {{ value.split(" ")[0] | replace(",", "") | int * 1.25 / 10000 | round(4) }}
Device Class: Monetary
State Class: Total
Unit of Measurement: DKK/kWh

@PuppetMaster — please format code snippets correctly.

@Troon Thanks, i have highlighted the code, but the codes format is like how i pasted it. It just need to be added in the web scrape integration sensor under “value template”

Please let me know if there is something i have missed in my formatting

Hi @PuppetMaster ,

Thanks,
I haven’t tried it yet, but I wonder why you multiply by 1.25 to add taxes.
Looks like the page by default shows prices including taxes!

Hello again,

I figured out that multiplying by 1.25 was necessary any way but you have to replace
elementor-tab-content-8881 by elementor-tab-content-2011
in order to get it to work now.

Furthermore I had to add some parenthesis to get the rounding to work. So my version of the value is like this:

{{ (value.split(" ")[0] | replace(",", "") | int * 1.25/ 10000) | round(4) }}

I don’t integrate these additional costs directly in the Nordpool integration but I make another sensor that combines data from the nordpool integration and theses scrape sensors.

Now my prices are exactly as those supplied by Andel my energy provider.
The result is then displayed on an apexcharts card and looks like this:

image

I am very glad for the result. Thank you for your help :slight_smile:

1 Like

@ghassan You’re welcome, and thank you for perfecting it :slight_smile: I wasn’t even thinking about combining them outside of the integration, which of cause makes sense in this instance :stuck_out_tongue: