How to remove unwanted string in sensor value?

Hi!

Working with scraping for the first time and i am near my goal.
Managed to remove everything after the wanted value, but have to remove everything before it to. Or even better just fetch the value itself without unwanted characters.

This is my code:

- platform: scrape
  resource: https://www.fjordkraft.no/Templates/Fjordkraft/webservices/PriceMap.asmx/GetDailyPricesJson?regionPriceMapPageId=1
  name: Fjordkraft
  select: "string"
  value_template: '{{ ((value.split(" ")[0]) | replace (",", ".")) }}'
  unit_of_measurement: "øre/kWh"

And currently this gives this output: {“Areas”:[{“id”:“1”.“price”:"69.03

I just need the value of the “price”.
I am sure it is several ways to do this, but so far i have not managed to fix it.

Some help is very appreciated!

There’s probably an easier way to do this as it looks like json data. Can you post the original returned value without any alterations?

Hi!

Yes, but i found out myself some minutes ago, maybe not the nicest way but it works :slight_smile:

 value_template: "{{ value.split(':')[3]|replace('id', '')|replace('}', '')|replace('{', '')|replace('\"','')|replace('øre/kWh,','') }}"

It could be as simple as this:

value_template: "{{ value_json.Areas[0].price }}"

I giving it a try now, makes sense.

Thanks Tom! you are absolutely right, it returned the right value without using all that replace.
It returned 63,43 øre/kWh now, i am just gonna strip øre/kwh because i want it as a integer and has the unit_of_measurement: “øre/kWh” there.

1 Like