Data from the first day of month

I am quite new to influx queries and I struggle with the syntax a lot.
What I want to do is to get the data for only the first day of the month. I tried this:

import “date”

month = date.month()
range(start: month)
range(stop: month*+360000000000*24)
from(bucket: “tasmota”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “tasmota”)
|> filter(fn: (r) => r[“_field”] == “ENERGY_Total”)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> last()

but that does not work out somehow. Can anyone help me?

Hi @yves and welcome to the Influx Community Forum.

This can indeed be a bit tricky. There may be better ways to handle this using Flux, but this should work.

import "date"  // The date package provides date and time constants and functions.

firstDayOfMonth = date.truncate(t: now(), unit: 1mo)  // gets the current time (`now()`), and then truncates it to the beginning of the month (`1mo`) effectively giving you a timestamp for the first day of the current month.

from(bucket: "tasmota")
  |> range(start: firstDayOfMonth, stop: date.add(d: 1d, to: firstDayOfMonth)) // date.add() adds a duration to a time value and returns the resulting time value.
  |> filter(fn: (r) => r["_measurement"] == "tasmota")
  |> filter(fn: (r) => r["_field"] == "ENERGY_Total")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> last()

thank you very much! looks good, but I ran into a new problem with that code:

 error: record is missing label windowPeriod

Acc. to this post, that error is specific to Influx Data Explorer.

When I run a query in Grafana with the same general parameters for start and stop, and also aggregate using v.windowPeriod, it works.

ah, I see. Thank you