Hello,
Sorry my english is not the best one 
I have a source that gives me the electrical consumption since my subscription. So it is an only increasing source, meaning that if I subtract the value retrieved at 2pm from the value at 3pm, I have my electrical consumption between 2pm and 3pm.
I retrieve multiple values in an hour, so i have multiple choices:
- I can subtract the first value from 2pm to the value from 3pm.
from(bucket: "bucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measure" and r["_field"] == "toto")
|> aggregateWindow(every: 1h, fn:first)
|> difference()
The drawback is that I do not have any realtime
value, because the consumption of the current hour is not displayed and cannot be. To get the current consumption I need the first
of the next hour, which does not currently exist.
- I can use the
spread
function which is doing last() - first()
. I do have realtime display, because a last
value always exist. However, I am loosing some computations:
F = FIRST, S=SECOND, L=LAST
| F1 S1 ..... L1 | F2 S2 .... L2 | F3 S3 ... L3 |
Consumption Hour1 = L1-F1
Consumption Hour2 = L2-F2
Consumption Hour3 = L3-F3
In that computation that is performed by the following function
from(bucket: "bucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measure" and r["_field"] == "toto")
|> aggregateWindow(every: 1h, fn:spread)
I am losing the following values : F2-L1
, F3-L2
, so the result is not perfect.
- Finally, my preferred method is to use the
last
function:
from(bucket: "bucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "measure" and r["_field"] == "toto")
|> aggregateWindow(every: 1h, fn:last)
|> difference()
I have the realtime display (because I do not need any value from the next hour), I am not losing any values (Consumption Hour2 = L2-L1
) however there is one drawback 
The consumption of the Hour1 needs this computation : L1-L0
, and unfortunately the range Today so far
from grafana does not give me the L0
value which belong to the previous day…
I do not have this problem with influxql, for an unknown reason… and when I compare both results (Influxql vs flux), it is different for this first hour due to the fact that L0
is missing.
I hope I am a bit clearer 
Thank you