New to Influxdb Grafana query

Yes the example I gave I copied from @grant2 and deleted the first and last from bucket forgot to add the double tt - well observed !.

With the query below Influxdb puts out a number but it’s way too big, with a Past 15m

First = from(bucket: "buckett")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "kwh")
  |> first()
  |> yield(name: "First")

Last = from(bucket: "buckett")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "kwh")
  |> last()
  |> yield(name: "Last")

union(tables: [First, Last])
|> difference()

I’m now I’m trying to find how to have a stat for yesterday of kwh with the accumulated watt hours.

I had a go with ChatGPT and asked it, it came up with

from(bucket: “your-bucket”)
|> range(start: -1d, stop: -0d)
|> filter(fn: (r) => r._measurement == “your-measurement” and r._field == “your-counter-field”)
|> difference()
|> last()

and with my data

from(bucket: “buckett”)
|> range(start: -1d, stop: -0d)
|> filter(fn: (r) => r._measurement == “kwh” and r._field == “kwh”)
|> difference()
|> last()

just returns 1 rather than subtracting difference from the start and stop

@myozone
difference() returns the difference between subsequent values. You are querying a whole day’s worth of readings, so you need to use the spread() function as previously described. Also, there is a way in Flux to specify yesterday (Grafana too, but let’s focus on Flux).

Something like this should work:

import "experimental/date/boundaries"

day = boundaries.yesterday()

from(bucket: "buckett")
|> range(start: day.start, stop: day.stop)
  |> filter(fn: (r) => r["_measurement"] == "kwh")
  |> filter(fn: (r) => r["_field"] == "kwh")
  |> spread()
  |> yield(name: "stat_for_yesterday_kwh")
1 Like

Thanks @grant2 That worked perfectly, I’m slowly leaning but my ideas are ahead of my knowledge. It’s interesting though to use AI for programming with ChatGPT etc, it’s amazing what it can do.