Wrong weekstart with difference()

Hello!

I have the following issue with this flux:

import “timezone”
option location = timezone.location(name: “Europe/Berlin”)
from(bucket: “iobroker”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “field” and r[“_field”] == “value”)
|> aggregateWindow(every: 24h, fn: last, timeSrc: “_start”, createEmpty: false)
|> difference()
|> map(fn: (r) => ({r with _kWh: float(v: r._value) / 1000.00}))

When I use this with e.g. “This week so far” as timerange the current week starts at 19.9. and not at 18.9.

Another flux without difference() works as expected:

import “timezone”
option location = timezone.location(name: “Europe/Berlin”)
from(bucket: “iobroker”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “field”)
|> filter (fn: (r) => r[“_field”] == “value”)
|> aggregateWindow(every: 24h, fn: last, timeSrc: “_start”)

Any ideas what I can do to get the right output for the first flux??

Thanks!

Hi @dieterlind

Maybe try putting an offset value in the aggregateWindow line. Try playing around with 1h, -1h, 2h, -2h, and see what happens to the graph.

  |> aggregateWindow(every: 24h, offset: -1h, fn: last, timeSrc: “_start”, createEmpty: false)

Also, in one of my queries that aggregates by week, I have this (just posting here in case it helps):

 |> aggregateWindow(every: 1w, offset: -3d, fn: sum)  // sums the 7 days of each week to get the total used per week, and use -3d as the offset to move the weekly aggregate to start on a Monday

Offset -1h delivers:

So also not correct

-3 also isn’t correct

any other suggestions what I can try to fix this issue?

Thanks

I am sure there is a solution, but at the moment I do not have any good test data that I can mess around with. It will probably be a combination of defining Start and Stop via date.truncate function, the boundaries.Monday function, and some settings in the aggregateWindow function.

1 Like