Hello,
I’m relatively new to Influx/Grafana and have been sitting for 2 days now with a problem that I just can’t get on with.
It should be relatively simple, but somehow I do not get it.
I have an electricity meter reading, per “Bar gauge” I would like to display the consumption of the last 7 days (total consumption in 7 days).
If I look at the data per:
|> aggregateWindow(every: 1d, fn: last, timeSrc: "_start", offset: 4d)
|> difference()
then the data is correct.
But as soon as I enter 7d, only the consumption of today is displayed.
I understand that 7d = 1w and Influx/aggregatewindow is group by the Unix epoch week.
But how can i solve that.
import "timezone
option location = timezone.location(name: "Europe/Berlin")
from(bucket: "data")
|> range(start: -14d, stop: now())
|> filter(fn: (r) => r["_measurement"] == "Kühlschrank.ENERGY_Total")
|> filter(fn: (r) => r["_field"] == "value")
|> aggregateWindow(every: 7d, fn: last, timeSrc: "_start", offset: 4d)
|> difference()
|> yield(name: "last")
My expectation was to see the consumption summed up (Last 7 days) 11.10.2022 0:00 - 17.10.2022 now()
So 4,144 kWh
GoodOld
October 22, 2022, 12:33pm
2
Find a Solution:
data = from(bucket: "iobroker")
|> range(start: -7d)
|> filter(fn: (r)
=> r["_measurement"] == "Kühlschrank.ENERGY_Total")
|> filter(fn: (r) => r["_field"] == "value")
min = data
|> min()
|> set(key: "_field", value: "delta")
max = data
|> max()
|> set(key: "_field", value: "delta")
union(tables: [min, max])
|> difference()
1 Like
@GoodOld ,
Thank you for sharing your solution!
This might also be related:
opened 09:21PM - 13 Jul 22 UTC
enhancement
`window()` and `aggregateWindow()` window based on Unix epoch time `1970-01-01`.… It's necessary to use an offset, which is available in both `window()` and `aggregateWindow()` functions, in order to window based on the range start date.
For example, for a 30d window based on the start date, without an offset, the first window is only 7 days long:
```js
import "experimental/array"
import "date"
start_date = time(v: 2021-11-22T00:00:00.000Z)
data_raw = [
{_time: 2021-11-22T00:10:00.000Z, id: "bar", _value: 5},
{_time: 2021-11-26T00:00:00.000Z, id: "bar", _value: 10},
{_time: 2021-12-03T00:00:00.000Z, id: "bar", _value: 30},
{_time: 2022-01-05T00:00:00.000Z, id: "bar", _value: 50}
]
data = array.from(rows: data_raw)
|> group(columns: ["id"])
data
|> range(start: start_date, stop: date.add(d: 60d, to: time(v: start_date)))
|> window(every: 30d)
```
<img width="1525" alt="Screen Shot 2022-07-13 at 2 15 10 PM" src="https://user-images.githubusercontent.com/330044/178837649-51d9805f-afb4-474b-b3b9-9d06a1f7b210.png">
To window by 30 days from the start, it's necessary to add an `offset` like:
```js
import "math"
...
days_since_epoch = uint(v: start_date) / uint(v: duration(v: 1d))
offset = duration(v: string(v: math.mod(x: float(v: days_since_epoch), y: float(v: days_since_epoch / uint(v: 30) * uint(v: 30)))) + "d")
...
|> window(every: 30d, offset: offset)
```
<img width="1525" alt="Screen Shot 2022-07-13 at 2 17 48 PM" src="https://user-images.githubusercontent.com/330044/178838010-69aa5f09-9a52-49f8-bd6b-f6c7d9768713.png">
It would be easier if there were an option in `window()` and `aggregateWindow()` to start the windowing from the start date rather than epoch time.