hourSelection() not working in Grafana

So I have this Flux query:

from(bucket: v.defaultBucket)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: ® => r["_measurement"] == “Energy”)
|> filter(fn: ® => r["_field"] == “value”)
|> aggregateWindow(every: 10y, fn: sum)

(don’t mind the 10y aggregate window. Without this it doesn’t work)
This works fine, it returns a single point with the consumed energy and displays it into a gauge.

Now I want to filter by certain hours. So I use hourSelection():

But adding this line returns no data when there really is. Am I doing something wrong?

Hello @Marti_B,
It’s hard to tell without looking at the annotated CSV.
Can you verify that you have data in that time period? Also I believe you want to reverse the stop and start times. I

Thanks for your response

No, I want to pick the data from 22h to 12h. So all the night and morning data.

Without hourSelection():

I only get 1 data because agregateWindow is 1y (this is another problem that I think is not related to influxDB but Grafana).

If I try the same but with hourSelection() (the image on the first post) I get no data when I try to inspect it.

By the way the query that I’ve shown above is done inside the 22-12h range:
This query is done inside the 22-12h:
image

Hello @Marti_B,
I’m sorry I should have clarified, I’m not sure it works across two days like that. I’m checking with the Flux team.

1 Like

Hi @Marti_B,

Unfortunately, it seems like hourSelection doesn’t presently do what you want or need. I’ve created an issue to address this: https://github.com/influxdata/flux/issues/3372.

In the interim, I do think you should be able to use filter to accomplish what you need.

import "date"

|> filter(fn: (r) => {
  hour = date.hour(t: r._time)
  return hour >= 22 or hour < 12
})

This needs to be done before calling aggregateWindow to aggregate the data. Once you use any aggregate, the original times are lost and aggregateWindow will only likely return one row anyway because of the long period time period. I’d probably go with something like this instead (after the filter).

|> sum()
|> duplicate(column: "_stop", as: "_time")
|> drop(columns: ["_start", "_stop"])

This is effectively what aggregateWindow does, but you’re skipping the windowing part and just getting the stop time directly. It should also improve the speed of the query.

1 Like

Amazing!!

Thank you very much for addressing this issue.

I will definetly take a look at your solution :slight_smile:

Thanks!!