Caluclate Energy cost with different prices

Hi :slight_smile:

New day new problem to solve :slight_smile:
I want to calculate my energy cost between two points in time. For example 01.01.2021 and 31.12.2021.

This is the query for my enery meter. The data source adds multiple datapoints per minute. So enough data here to work with…

from(bucket: "iobroker")
  |> range(start: 2021-01-01, stop: 2021-12-31)
  |> filter(fn: (r) => r["_measurement"] == "sonoff__0__Smartmeter__Power_Total_in"
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> spread(column: "_value")

To simplify things, let’s say this will give me an output of 5000 (kwh)
It would be very easy to just multiply this value with my enery cost… But unfortunately the price isn’t a constant value
From 01.01.2021 - 01.05.2021 the enery cost per kwh was 0,28€
And since 01.06.2021 the price was adjusted to 0,41€

There are only two values to work with:

Also notice the timestamp of the first measurement. It isn’t even in the selected range.

Any idea, how to solve this?