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)
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.
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?
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
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.
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?
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
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.