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:
- Electricity supplied T1
- Electricity supplied T2
- Electricity delivered T1
- Electricity delivered T2
I want to transform this measurement to have the following fields:
- Electricity supplied = Electricity supplied T1 + Electricity supplied T2
- 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.