David Weber
David Weber

Reputation: 479

Influxdb2 How to Use windowPeriod in Arithmetic

I am trying to calculate my energy cost with datapoints published to my influxdb. I need to utilize windowPeriod in order to go back far enough in time. I am currently taking a sum of my energy usage and trying to do arithmetic on it, which if I use hard coded times is no problem. But that doesn't scale at all.

from(bucket: "energydata")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["device_name"] == "Home-14")
  |> filter(fn: (r) => r["detailed"] == "False")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> sum(column: "_value")
  |> map(fn: (r) => ({r with _value: r._value * 0.13806 / (float(v: int(v: v.windowPeriod)) / 1000000000.0 * 60.0 ) }))

I believe it is when trying to convert the windowPeriod into something useful that it breaks. As I change my period of review my results are all over the place.

Upvotes: 1

Views: 2735

Answers (2)

David Weber
David Weber

Reputation: 479

I was able to figure it out, in case anyone wants to know:

from(bucket: "energydata")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["device_name"] == "Home")
  |> filter(fn: (r) => r["detailed"] == "False")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> sum(column: "_value")
  |> map(fn: (r) => ({r with _value: r._value * (float(v: int(v: v.windowPeriod)) / (16000000000.0 * 60.0 * 60.0 * 60.0) ) * 0.13806 }))

Where 0.13806 is your cost per kWh. This converts windowPeriod into hours, and gets you kilowatt hours which you can use to make calculations.

Upvotes: 1

mhall119
mhall119

Reputation: 577

The v.windowPeriod is a dynamic variable being set by the UI to make graphs looks better, I don't think it's the right thing to use for calculating costs.

And are you sure that you want to take the mean average of your values in windowed intervals before calculating the sum? I'm not sure what your actual data looks like, but this feels like it might be the wrong approach.

Upvotes: 1

Related Questions