Skip to content

Instantly share code, notes, and snippets.

@tillsc
Last active January 3, 2021 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tillsc/2d71f3e89409049833e75ed22689ad40 to your computer and use it in GitHub Desktop.
Save tillsc/2d71f3e89409049833e75ed22689ad40 to your computer and use it in GitHub Desktop.
query influx data with unevenly distributed points
// Replace this with your specific query/selection
base = from(bucket: "hassio")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "kw")
|> filter(fn: (r) => r["entity_id"] =~ /bhz3_leistung_zus/)
|> filter(fn: (r) => r["_field"] == "value")
|> window(every: v.windowPeriod)
|> sort(columns: ["_time"], desc: false)
// This does NOT caclulate the correct mean (maybe `timeWeightedAvg` would be the right choice here but I couldn't
// test it for now)
means = base
|> mean()
|> duplicate(column: "_stop", as: "_time")
|> set(key: "_col", value: "mean")
|> drop(columns: ["_start", "_stop"])
// This table contains the last datapoint from the previous bin
lastValueInPreviousBin = base
|> last()
|> map(fn: (r) => ({ r with _time: time(v: uint(v: r._stop) + uint(v: v.windowPeriod)) }))
|> set(key: "_col", value: "lastValueInPreviousBin")
|> drop(columns: ["_start", "_stop"])
// The following `union` with `pivot` is more or less a left+right join. Since influx own `join` function is
// only capable for `inner` joins for now this is just a hack to do more or less the same... Maybe the new experimental
// `join` function will be a better choice but I dont understand it for now...
union(tables: [means, lastValueInPreviousBin])
|> pivot(rowKey:["_time", "_measurement", "_field" "entity_id", "domain"], columnKey: ["_col"], valueColumn: "_value")
|> map(fn: (r) => ({ r with _value: if exists r.mean then r.mean else r.lastValueInPreviousBin }))
|> drop(columns: ["mean", "lastValueInPreviousBin", "_field"])
|> rename(columns: {entity_id: "_field"})
|> sort(columns: ["_time", "_field"], desc: false) // I Do not understand why this is needed again... But it is...
@tillsc
Copy link
Author

tillsc commented Jan 3, 2021

Note: This only fixes the problem of "empty" bins filled by an averaged value and not the last value which would be the right value then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment