Query at exact timestamp for different days

Hi,

im new to flux. How do i filter a data at a specific time from the past 3 days.

specifically i want to query data at exactly 8:00:00 from the past 3 days.

Thank u

@nikiniki What version of InfluxDB are you using? The reason I ask is because different versions support different query languages.

I believe its 1.8. Im using it inside Grafana to extract data from InfluxDB

For this specific type of query, you need to use Flux. In InfluxDB 1.8, Flux isn’t enabled by default, so you need to enable it. You’ll also need to create a new connection in Grafana that uses Flux as the query language.

In you’re query, you need to filter by specific time parts. To do this, you have to evaluate the timestamp of queried data to make sure the hour, minute, and seconds meet your query criteria. You can use functions in the Flux date package, specifically:

  • date.hour()
  • date.minute()
  • date.second()

If your data is more precise than 1s, you’ll need to compare smaller units of time as well.

The query is going to look something like this:

from(bucket: "example-db/example-rp")
    |> range(start: -3d)
    |> filter(fn: (r) => r._measurement == "example-measurement")
    |> filter(fn: (r) => date.hour(t: r._time) == 8 and date.minute(t: r._time) == 0 and date.second(t: r._time) == 0)

Also note that all timestamps stored in InfluxDB are stored in UTC time.

ohhh thanks alot.
is it also possible to do the filtering with date?
for example filtering data from the first days of within past three months at 8oclock?

im actually using version 0.194.5. apologies for the mistake

Yes. Use the exact same method, but with date.monthDay() and a query range of 3 months (3mo):

from(bucket: "example-db/example-rp")
    |> range(start: -3mo)
    |> filter(fn: (r) => r._measurement == "example-measurement")
    |> filter(fn: (r) => date.monthDay(t: r._time) == 1 )
    |> filter(fn: (r) => date.hour(t: r._time) == 8 and date.minute(t: r._time) == 0 and date.second(t: r._time) == 0)

I got this error:
invalid: error @8:24-8:28: undefined identifier date error @8:55-8:59: undefined identifier date error @8:88-8:92: undefined identifier date error @7:24-7:28: undefined identifier date

I think its because I initially mentioned the wrong version

No, I forgot the import statement that imports the date package:

import "date"

from(bucket: "example-db/example-rp")
    |> range(start: -3mo)
    |> filter(fn: (r) => r._measurement == "example-measurement")
    |> filter(fn: (r) => date.monthDay(t: r._time) == 1 )
    |> filter(fn: (r) => date.hour(t: r._time) == 8 and date.minute(t: r._time) == 0 and date.second(t: r._time) == 0)

its worked. Thank you sir :heart: