Create fields with sum of fields using Flux

I’m migrating my dashboard from InfluxDB 1.x to 2.0. I’m also trying to use Flux and I’ve been trying to figure out how to create a field that contains the sum of other fields. This is very simple using InfluxQL but seems to be very complicated using Flux.

I have a measurement containing 4 fields:

  1. Electricity supplied T1
  2. Electricity supplied T2
  3. Electricity delivered T1
  4. Electricity delivered T2

I want to transform this measurement to have the following fields:

  1. Electricity supplied = Electricity supplied T1 + Electricity supplied T2
  2. Electricity delivered = Electricity delivered T1 + Electricity delivered T2

That’s it! Nothing else fancy, just adding two fields together to a new field. Seems like something that should be super simple, but I have been trying to fix this using Flux for a few hours now without success.

I tried this, but this is not ideal either because I now get only 1 series in my chart. I want to see both series (delivered + supplied).

from(bucket: "p1")
  |> range(start: -24h)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["_field"] == "electricityDeliveredT1" or r["_field"] == "electricityDeliveredT2" or r["_field"] == "electricitySuppliedT1" or r["_field"] == "electricitySuppliedT2")
  |> aggregateWindow(every: 1h, fn: max)
  |> difference()
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({
    r with
      delivered: r.electricityDeliveredT1 + r.electricityDeliveredT2,
      supplied: r.electricitySuppliedT1 + r.electricitySuppliedT2
  }))

It’s honestly a bit frustrating that something that was so easy in InfluxQL seems to be so complicated using Flux. I hope that I’m just looking at it the wrong way.

Hello @mmcnl,
The easiest way to project multiple aggregations with flux is to use multiple tabs.
In Tab one you could do:

from(bucket: "p1")
  |> range(start: -24h)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["_field"] == "electricityDeliveredT1" or r["_field"] == "electricityDeliveredT2")
//group by time to isolate the values you want to sum togehter
  |> group(columns: ["_time"], mode:"by")
  |> sum(column: "_value")
//to ungroup your data provide a group without any columns
  |> group()
  |> yield(name: "delivered")

And in the other script editor tab perform the query for the other fields. Then whether you’re looking at the Explore Data page or a Dashboard you will have two separate lines.

I know Flux is hard to get the hang of at first. Some of these simple things feel unnecessarily tricky, but it makes way for more complicated analysis if you need it later.

You could also use the rename() function if you want to rename your sum frorm “_value” to “delivered”, if renaming the result isn’t sufficient.

Also we’re working on revamping the UI to make working with Flux easier. If you’re interested in participating in the early access program and providing feedback, please let me know.

Great, this is indeed a totally different way of thinking but it works! I have some other suggestions as well to fix, so I’m definitely interested in participating in early access and providing feedback.

@mmcnl,
Awesome! Please join the Influx Slack https://www.influxdata.com/slack and then PM me and I’ll send you to the right places. Thank you!