Hi!
I am trying to get current power prices from api, i have tried two ways but so far with no luck as i have not done this before.
The result on the page is like this, and i just need the first value behind id:1
<string xmlns="http://fjordkraft.no/">{"Areas":[{"id":"1","price":"66,22 øre/kWh"},{"id":"2","price":"66,13 øre/kWh"},{"id":"3","price":"65,92 øre/kWh"},{"id":"4","price":"63,91 øre/kWh"},{"id":"5","price":"66,22 øre/kWh"}]}</string>
I have tried this:
- platform: command_line
name: fjordkraft
command: "curl -X GET https://www.fjordkraft.no/Templates/Fjordkraft/webservices/PriceMap.asmx/GetDailyPricesJson?regionPriceMapPageId=1"
unit_of_measurement: "øre/kwh"
value_template: '{{ value_json.Areas }}'
And this:
- platform: rest
name: fjordkraftpris
resource: https://www.fjordkraft.no/Templates/Fjordkraft/webservices/PriceMap.asmx/GetDailyPricesJson?regionPriceMapPageId=1
json_attributes_path: "$.response"
scan_interval: 15
json_attributes:
- "Areas"
- "price"
- "1"
value_template: '{{ value_json.response.Areas }}'
And i clearly doing something wrong in both cases.
Some advice is appreciated.
1 Like
You’ve probably either solved this or moved on, but in the interests of documenting my own solution to the exact same problem, the problem is that for whatever reason, fjordkraft aren’t returning a json string - they’re returning an xml document, containing a json string. Why they couldn’t store the data in one or the other, rather than both… who knows?
You need to feed the xml to an xml parser to extract the json, then feed the json to a json parser to extract the attribute you want:
curl -X GET https://www.fjordkraft.no/Templates/Fjordkraft/webservices/PriceMap.asmx/GetDailyPricesJson?regionPriceMapPageId=1 | xmlstarlet sel -N x=http://fjordkraft.no/ -t -v //x:string -n | jq -r '.Areas[] | select(.id=="3")'
That yields an end result of
{
"id": "3",
"price": "20,82 øre/kWh"
}
Home assistant can probably do the json parsing for you, so the final pipe to jq
may be redundant.
Hi!
Thank you! But yes i fixed earlier but using scrape instead.
Like this:
- platform: scrape
verify_ssl: false
resource: https://www.fjordkraft.no/Templates/Fjordkraft/webservices/PriceMap.asmx/GetDailyPricesJson?regionPriceMapPageId=1
name: Fjordkraft
select: "string"
value_template: "{{ value_json.Areas[0].price|replace(',', '.')|replace('øre/kWh','') }}"
unit_of_measurement: "Øre/kWh"
scan_interval: 60