Hi,
our (rather) fair and decent national electric grid operator Fingrid (in Finland) gives away lot of information for free via open API (of course you paid it already in your electric bill). They have forecast for electricity shortage on three minute timeslices, output is XML or JSON, depending which one you want. Fingrid open API requires personal API token, but you can make 10000 requests a day, which should be enough for most. API and response use UTC-time and all times are formatted as YYYY-MM-DDThh:mm:ssZ
I managed to create sensor (curl / JSON) that returns single value for testing purposes:
how to pass starting time attribute with current time and end time as current time + 12h for example?
response has always āvalueā:0 if no blackouts are expected and other numeric values, if blackouts are expected, numeric state varies by severity. If thereās other state than 0, Iād like to pick first and last response with different value than 0 and their timestamps. I have no need for middle timestamps, because there will be electricity shortage. How to template this first and last value and their timestamps?
Thanks in advance, this may not be complicated, but Iām not expert in templates, even if can do basics.
That is not an answer to your 2nd question thoughā¦ I would know how to do this (iterating) but not sure how I can parse the whole response in one attribute (as json). Rest does not allow much on the attributes side
EDIT: maybe you should indeed use your curl and creata file and from that one use thisā¦maybe others will chime in
For the non-0 value you can paste this in developer tools > templates, which will give you the firstā¦and if you change it all at the bottom to ālastā ā¦ guess
{% set value_json = [
{
"value": 0,
"start_time": "2022-12-17T06:01:00+0000",
"end_time": "2022-12-17T06:01:00+0000"
},
{
"value": 2,
"start_time": "2022-12-17T06:04:00+0000",
"end_time": "2022-12-17T06:04:00+0000"
},
{
"value": 0,
"start_time": "2022-12-17T06:07:00+0000",
"end_time": "2022-12-17T06:07:00+0000"
},
{
"value": 1,
"start_time": "2022-12-17T06:10:00+0000",
"end_time": "2022-12-17T06:10:00+0000"
},
{
"value": 0,
"start_time": "2022-12-17T06:13:00+0000",
"end_time": "2022-12-17T06:13:00+0000"
}
] %}
{% set y = value_json | count %}
{% set ns = namespace(val=[]) %}
{% for x in range(0,y) %}
{% if value_json[x].value != 0 %}
{% set ns.val = ns.val + [value_json[x]] %}
{% endif %}
{% endfor %}
{{ns.val | first}}
Great to notice that you are interested in this topic! Iām hoping that literally everyone (who has extra resources to think about this - some have enough burden already from inflation etc.) would give a thought to these things.
Some notes about possible improvements:
Fingrid API expects timestamps to be in UTC. So utcnow() would most probably give more appropriate results.
If your start_time is in the past, then value_template: ā{{ value_json.0.value }}ā returns the first result. Which is 4h ago due to timedelta (or maybe just -2h as the original poster most probably lives in Finland in timezone UTC+2).
You can hijack XML version of the API with Restful sensor to get all values to the attributes. An example below to get values from the last 60 minutes with the most new one in the sensor state.
I tried the API and it seems that the API https://api.fingrid.fi/v1/variable/336/events/json does not have forecast capability. It seems that it just has current value and historical values. I would love to get that forecast data also but it seems that the API does no have forecast. (Or have I missed something? Could be that also ).
I presume that your use case would have been to do something with expected blackout timestamps?
You may have it right about forecast. My idea was to provide early notification on expected blackout and do some load shedding, but without forecast, it will be just immediate load shedding on water heater and notification.
However, since Fingrid has announced blackouts are likely to be known couple hours in advance, they will probably rise blackout status immediately, maybe I should do load shedding immediately when status differs from value: 0. That should be very easy, since I need to get single value only. Fingrid has followng values for energy situation:
Since last post, Fingrid has reworked their API. And HA has also changed somewhat. Hereās updated code, having separate config files, this is my sensor.yaml:
This ia working one in 11.5.2024, dataset IDs 336 and 209 havenāt changed.
And worth noting, since this is public service offered, even if you need to register to use it, itās completely free to use. Fingrid provides wide variety of information about Finlandās national gridās state, electricity production and connections to neighboring states, their API is real gem for everyone wihing to show this kind of information in their HA setups!