InfluxDB 2 Downsampling

good day. documentation have very little information about how to make complex dowsampling. we have some data from many sensors. this data marked with 3 tags : ControllerName,DeviceName and SensorName select to display sensor mesurment looks like this |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == “mqtt_consumer”)
|> filter(fn: (r) => r[“Controller”] == “ControllerName”)
|> filter(fn: (r) => r[“Device”] == “deviceName”)
|> filter(fn: (r) => r[“Control”] == “SensorName”)
|> filter(fn: (r) => r["_field"] == “value”)
but how can i make one downsampling task that filter out each controller with each device and each sensor and aggregate those seqenses with tags preserved.
i need domething like this:

  foreach(controlerTag: scheme.getTagValues("controller")){
      foreach(deviceTag: scheme.getTagValues("device")){
          foreach(sensorTag: scheme.getTagValues("sensor")){
            data = from(bucket: "Bucket")
            |> range(start: -task.every)
            |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
            |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
            |> filter(fn: (r) => r["controller"] == controlerTag)
            |> filter(fn: (r) => r["device"] == deviceTag)
            |> filter(fn: (r) => r["sensor"] == sensorTag)
            |> filter(fn: (r) => r["_field"] == "value")

            data
            |> mean()
            |> set(key: "agg_type", value: "mean")
            |> set(key: "controller", value: controlerTag)
            |> set(key: "device", value: deviceTag)
            |> set(key: "sensor", value: sensorTag)
            |> to(bucket: "5secAggregationFor1Day", org: "Org", tagColumns: ["agg_type"])
            
            data
            |> last()
            |> set(key: "agg_type", value: "last")
            |> set(key: "controller", value: controlerTag)
            |> set(key: "device", value: deviceTag)
            |> set(key: "sensor", value: sensorTag)
            |> to(bucket: "5secAggregationFor1Day", org: "Org", tagColumns: ["agg_type"])
          }
      }
  }

Hello @ScorpioTheDark,
Welcome!
Your data should already be grouped by default by “Controller”, “Device”, and “Control” if you just filter for the measurement.

from(bucket: "test")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_comsumer")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

I wrote the following data:

mqtt_comsumer,Controller=N1,Device=N1,Control=N1 value=1
mqtt_comsumer,Controller=N1,Device=N1,Control=N1 value=2
mqtt_comsumer,Controller=N2,Device=N2,Control=N2 value=2
mqtt_comsumer,Controller=N2,Device=N2,Control=N2 value=3

And the query above yielded the following output:

As we can see, the tables are grouped by different controller, device, and control names. And their corresponding tags values are included in the output.
Our to() function could look like:

to(
  bucket: "my-mean-data",
  org: "my-org",
  timeColumn: "_time",
  tagColumns: ["Controller", "Device", "Control"],
  fieldFn: (r) => ({ [r._field]: r._value })
)

I might also recommend using the aggregateWindow() function

 |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)

I might be wrong, but I think gaining a better understanding of annotated CSV could help you here:

https://docs.influxdata.com/influxdb/cloud/reference/syntax/annotated-csv/

Please let me know if this helps? Or if I misunderstood your problem.

Thank you.

to(
  bucket: "my-mean-data",
  org: "my-org",
  timeColumn: "_time",
  tagColumns: ["Controller", "Device", "Control"],
  fieldFn: (r) => ({ [r._field]: r._value })
)

I am facing a similar problem but I am getting tags and everything but is there any way we don’t have to give particular tag names InfluxDB should automatically take all tags.
it will be like:

to(
      bucket: "my-mean-data",
      org: "my-org",
      timeColumn: "_time",
      tagColumns: [code for list of all tags or any variable which place all the tag list here],
      fieldFn: (r) => ({ [r._field]: r._value })
    )

Hello @Ravikant_Gautam,
Yes.

Tag columns in the output. Defaults to all columns with type string , excluding all value columns and columns identified by fieldFn .