InfluxDB 2.0 extend column mathematically

I’m just upgrading from InfluxDB 1.8 to 2.0 and I still have my weaknesses here.
With the following code I calculate the average consumption per hour. Here I want to add another column with a simple mathematical calculation.
Unfortunately it doesn’t work that way. Can someone show me where the error is.

from(bucket: "stromzaehler")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "stromzaehler")
  |> aggregateWindow(every: 1h, fn: spread, createEmpty: false)
  |> yield(name: "mean")
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with _value: r._stromzaehler * 100.0 }))

Hi @Cavekeeper

What does the error message say?

Just a guess here, but does this work?

from(bucket: "stromzaehler")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "stromzaehler")
  |> aggregateWindow(every: 1h, fn: spread, createEmpty: false)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with NewColumnHere: r._stromzaehler * 100.0 }))
  |> yield(name: "YourNewStream")

There is no error message.
The problem is that the result of the calculation is “null”.

Bild

What is the result when you remove the map() function? Please post here.

I found the solution.
The key code in the map line is:

r.value

and not

r._stromzaehler

This is the code that works:

from(bucket: "stromzaehler")
  |> range(start: today())
  |> filter(fn: (r) => r["_measurement"] == "stromzaehler")
  |> aggregateWindow(every: 1h, fn: spread, createEmpty: false)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({ r with NewColumnHere: r.value * 100.0 }))
  |> yield(name: "YourNewStream")
1 Like