# Query specific time range and day

**URL:** https://community.influxdata.com/t/query-specific-time-range-and-day/14503
**Category:** Systems
**Tags:** influxdb, time-series
**Created:** [May 25, 2020, 3:01pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503 "2020-05-25T15:01:23Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Nibeckj](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/nibeckj/32/5008_2.png) [@Nibeckj](https://community.influxdata.com/u/Nibeckj)
#### Post date: [May 25, 2020, 3:01pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/1 "2020-05-25T15:01:23Z")

</div>

Hello,

I want to query influx for times series with :

- Start time at 08h00
- end time at 19:00  
-and for a range day : Monday to Friday

Is there a way to do it ?

Regardds

---

<div class="post-metadata">

### Author: ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)
#### Post date: [May 26, 2020, 3:51pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/2 "2020-05-26T15:51:29Z")

</div>

Hello @Nibeckj,  
Welcome.  
You can create variables for those times and use the flux date package.

> **[Create a dashboard variable | InfluxDB OSS 2.6 Documentation](https://docs.influxdata.com/influxdb/v2.0/visualize-data/variables/create-variable/)**
>
> Create dashboard variables in the Data Explorer, from the Organization page, or import a variable.

> **[date package | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/date/)**
>
> The date package provides date and time constants and functions.

---

<div class="post-metadata">

### Author: ![bjoern\_b](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bjoern_b/32/6951_2.png) [@bjoern\_b](https://community.influxdata.com/u/bjoern_b)
#### Post date: [March 11, 2021, 8:43pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/3 "2021-03-11T20:43:22Z")

</div>

Hello,

I’m struggling with the same problem: I want to show all data with date = current day and time 00:00 to 06:00. As I’m new to Flux I can’t figure out how to create a variable with a dynamic date and a fixed time.

In python or (other languages) I would concatenate a date string and then transform it to a date (or I would create a date and then add the hours/minutes).  
In Flux I tried without any success: my\_start\_time = date.year(t: now()) date.month(t: now()) date.day(t: now()) (06:00:00Z)

Surely there is a simple solution … I only can’t find it (Google is not my friend today ☹).

@Anaisdg: Can you please give another hint how to combine create-variable and the date-packages?  
@Nibeckj: Did you find a solution to your problem

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [March 11, 2021, 10:15pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/4 "2021-03-11T22:15:42Z")

</div>

@bjoern_b There are a few Flux functions that will help you do this:

- Use [`hoursSelection()`](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/hourselection/) to filter by hour of day.
- Use [`date.weekDay()`](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/date/weekday/) to process and filter points by day of the week.
- Use [`date.truncate()`](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/date/truncate/) to truncate timestamps to a specified unit of time.

For @Nibeckj original use case, the query would look something like:

```javascript
import "date"

from(bucket: "example-bucket")
  |> range(start: -90d)
  |> filter(fn: (r) => r._measurement == "example-measurement")
  |> filter(fn: (r) =>
    date.weekDay(t: r._time) >= 1 and
    date.weekDay(t: r._time) <= 5
  )
  |> hourSelection(
    start: 8,
    stop: 19,
  )

```

For your use case, it would look something like:

```javascript
import "date"

today = date.truncate(t: now(), unit: 1d)

from(bucket: "example-bucket")
  |> range(start: -90d)
  |> filter(fn: (r) => r._measurement == "example-measurement")
  |> filter(fn: (r) => date.truncate(t: r._time, unit: 1d) == today)
  |> hourSelection(start: 0, stop: 6)

```

---

<div class="post-metadata">

### Author: ![bjoern\_b](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bjoern_b/32/6951_2.png) [@bjoern\_b](https://community.influxdata.com/u/bjoern_b)
#### Post date: [March 11, 2021, 11:40pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/5 "2021-03-11T23:40:18Z")

</div>

@scott That’s so great. Thanks a lot for the quick help! Such an easy solution. 👍

Just for notice: For my final solution I combined it with a union() so that I can get a result set which spans over midnight (22:00 the day before until 06:00 the current day). Perhaps there is a smarter solution but for me it is working. 😃

```
import "date"

yesterday = date.truncate(t: -1d, unit: 1d)
bucket_yesterday = from(bucket: "telegraf")
  |> range(start: -3d)
  |> filter(fn: (r) => r["_measurement"] == "dBA")
  |> filter(fn: (r) => r["type"] == "output_to")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => date.truncate(t: r._time, unit: 1d) == yesterday)
  |> hourSelection(start: 22, stop: 23)
  |> aggregateWindow(every: 1s, fn: mean, createEmpty: false)

today = date.truncate(t: now(), unit: 1d)
bucket_today = from(bucket: "telegraf")
  |> range(start: -3d)
  |> filter(fn: (r) => r["_measurement"] == "dBA")
  |> filter(fn: (r) => r["type"] == "output_to")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => date.truncate(t: r._time, unit: 1d) == today)
  |> hourSelection(start: 0, stop: 5)
  |> aggregateWindow(every: 1s, fn: mean, createEmpty: false)

union(tables: [bucket_yesterday, bucket_today])
  |> yield(name: "mean")
```

---

<div class="post-metadata">

### Author: ![bjoern\_b](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/bjoern_b/32/6951_2.png) [@bjoern\_b](https://community.influxdata.com/u/bjoern_b)
#### Post date: [March 12, 2021, 12:20am UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/6 "2021-03-12T00:20:06Z")

</div>

@scott I just stumbled on a “bonus question” regarding `hourSelection()`:

I’m live Germany. So my timezone is currently UTC+1 and in summer it’s UTC+2. hourSelection() uses (at least in my Data Explorer) UTC.  
That means, if I want 22 to 6 o’clock local time:

- Currently, during winter time, I have to set `hourSelection(start: 21, stop: 23)` and `hourSelection(start: 0, stop: 5)`
- During summer time I would have to change the query to `hourSelection(start: 20, stop: 23)` and `hourSelection(start: 0, stop: 4)`

Is there any possibility to dynamize this behaviour so that `hourSelection()` uses my locale time (zone) or that I can calculate the start/stop time dynamically based on additional informations (variables, etc.)?

---

<div class="post-metadata">

### Author: ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)
#### Post date: [March 12, 2021, 5:52pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/7 "2021-03-12T17:52:46Z")

</div>

@bjoern_b It’s not a super elegant solution, but it should work. If you know the dates and time that the time changes, you could conditionally set variables:

```javascript
import "date"

daylightSavingsStart = 2021-02-28T04:00:00Z
daylightSavingsEnd = 2021-010-31T05:00:00Z
today = date.truncate(t: now(), unit: 1d)
yesterday = date.truncate(t: -1d, unit: 1d)

yesterdayHourStart = if today > daylightSavingsStart and today < daylightSavingsEnd then 20 else 21
todayHourStop = if today > daylightSavingsStart and today < daylightSavingsEnd then 4 else 5

bucket_yesterday = from(bucket: "telegraf")
  |> range(start: -3d)
  |> filter(fn: (r) => r["_measurement"] == "dBA")
  |> filter(fn: (r) => r["type"] == "output_to")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => date.truncate(t: r._time, unit: 1d) == yesterday)
  |> hourSelection(start: yesterdayHourStart, stop: 23)
  |> aggregateWindow(every: 1s, fn: mean, createEmpty: false)

bucket_today = from(bucket: "telegraf")
  |> range(start: -3d)
  |> filter(fn: (r) => r["_measurement"] == "dBA")
  |> filter(fn: (r) => r["type"] == "output_to")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => date.truncate(t: r._time, unit: 1d) == today)
  |> hourSelection(start: 0, stop: tordayHourStop)
  |> aggregateWindow(every: 1s, fn: mean, createEmpty: false)

union(tables: [bucket_yesterday, bucket_today])
  |> yield(name: "mean")

```

---

<div class="post-metadata">

### Author: ![DavidGaGySro](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/davidgagysro/32/7389_2.png) [@DavidGaGySro](https://community.influxdata.com/u/DavidGaGySro)
#### Post date: [November 9, 2022, 3:14pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/8 "2022-11-09T15:14:56Z")

</div>

Hello,

I was wondering if it is possible to use these queries with the python client.

In order to try your solutions, in python I tried:

```auto
from influxdb_client import InfluxDBClient
client = InfluxDBClient(url=url, token=token, org=org)
query_api = client.query_api()

query_api.query(org=org, query = 'import "date"')

```

But this doesn’t work since it’s a bad request (ApiException: 400) because the Flux script returns no streaming data.

Is there any possibility to do these queries on a specific time range and day with the python client?

---

<div class="post-metadata">

### Author: ![mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mike](https://community.influxdata.com/u/mike)
#### Post date: [November 13, 2022, 10:28pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/9 "2022-11-13T22:28:09Z")

</div>

Hi,  
I’ve seen and used the:

```auto
|> hourSelection(start: 9, stop: 14)

```

Which is nice, but I need to filter data from, say 09:00 to 14:30. What would be my best option?

```auto
|> hourSelection(start: 9, stop: 14)
|> filter(fn: (r) => 
    if date.minute(t: r._time) >= 30 then
        if date.hour(t: r._time) > 14 then
            false
        else
        true      
      else
      true
  )

```

I’ve tried the above, but I can’t seem to chain the date.hour and date.minute together. They both work separately, but not together. I’m probably missing something very basic.

Thanks!  
Micke

---

<div class="post-metadata">

### Author: ![mike](https://avatars.discourse-cdn.com/v4/letter/m/58f4c7/32.png) [@mike](https://community.influxdata.com/u/mike)
#### Post date: [November 14, 2022, 10:46pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/10 "2022-11-14T22:46:20Z")

</div>

Solution to my own question: The above works in the latest version 2.5.1. Didn’t work properly in the last image I was running, I think that was 2.4.0

Still open for cleaner suggestions though, if anyone has one. 🙂

---

<div class="post-metadata">

### Author: ![Tuong](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tuong/32/12796_2.png) [@Tuong](https://community.influxdata.com/u/Tuong)
#### Post date: [March 1, 2023, 10:34am UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/11 "2023-03-01T10:34:54Z")

</div>

I am using version 1.8 and UTC+7.  
I hope it is useful to you.

```auto
import "strings"
import "experimental"

today = time(v: "${strings.substring(v: string(v: experimental.subDuration(d: 17h, from: now())), start: 0, end: 10)}T17:00:00Z" )

from(bucket: "dokiem_hatang") 
  |> range(start: today )
  |> filter(fn: (r) => r._measurement == "dokiem_congsuat" and r._field == "congsuat_chia" )
  |> aggregateWindow(every: 5m, fn: mean)
  |> yield(name: "min")

```

---

<div class="post-metadata">

### Author: ![Maarten](https://avatars.discourse-cdn.com/v4/letter/m/ecccb3/32.png) [@Maarten](https://community.influxdata.com/u/Maarten)
#### Post date: [September 22, 2023, 4:07pm UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/12 "2023-09-22T16:07:26Z")

</div>

Hallo Nibeckj,

Hello  
i am a beginner on using influxdb. I saw your message while looking for a solution for the working days. I can’t get any further with the given answer. Can you help me get started?

Beste Regard, Maarten

---

<div class="post-metadata">

### Author: ![ragman1976](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@ragman1976](https://community.influxdata.com/u/ragman1976)
#### Post date: [March 6, 2024, 6:29am UTC](https://community.influxdata.com/t/query-specific-time-range-and-day/14503/13 "2024-03-06T06:29:43Z")

</div>

Hi,

I also trying to sum up data (energy consumption) between a time range using th hourSelection function.  
But the code that I`m using sum up the whole 24 hours. Any suggetions what`s wrong with the code?

import “timezone”  
option location = timezone.location(name: “Europe/Berlin”)

from(bucket: “HASS”)  
|\> range(start: -7d)  
|\> filter(fn: (r) =\> r[“\_measurement”] == “Wh”)  
|\> filter(fn: (r) =\> r[“\_field”] == “value”)  
|\> filter(fn: (r) =\> r[“domain”] == “sensor”)  
|\> filter(fn: (r) =\> r[“entity\_id”] == “mqtt\_haushaltsstrom\_1\_8\_0”)  
|\> filter(fn: (r) =\> r[“friendly\_name”] == “mqtt\_haushaltsstrom\_1\_8\_0”)  
|\> filter(fn: (r) =\> r[“source”] == “HA”)  
|\> hourSelection(start: 22, stop: 23)  
|\> difference()  
|\> aggregateWindow(every: 1d, fn: sum, timeSrc: “\_start”)
