If statement in query

Hello, I am new to influxdb and running influxdb2.2 with telegraf collecting data from my inverters through Modbus.
My inverter will provide a register for power and one for the scale factor, which changes depending on the current power.

This is my current query for both values

from(bucket: "Energy")

  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)

  |> filter(fn: (r) => r["_measurement"] == "modbus")

  |> filter(fn: (r) => r["name"] == "Fronius")

  |> filter(fn: (r) => r["slave_id"] == "1")

  |> filter(fn: (r) => r["_field"] == "power1" or r["_field"] == "W_SF")

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

  |> yield(name: "mean")

How could I do to create a if statement so that if W_SF is == 0, power1 is not modified. If W_SF == -1, power1 is multiplied by 0.1 and if W_SF == -2 power1 is multiplied by 0.01.

Thank you very muchPreformatted text

Hello @mafu,
Welcome!!
You’ll want to conditionally transform values with the map() function:

Let me know if you want more help! :slight_smile:

Thenk you. I have tried this

from(bucket: "Energy")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "modbus")
  |> filter(fn: (r) => r["name"] == "Fronius")
  |> filter(fn: (r) => r["slave_id"] == "1")
  |> filter(fn: (r) => r["_field"] == "power1" or r["_field"] == "W_SF")
  |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map( fn: (r) => ({r with _value: 
      if r.W_SF == -2 then r.power1*0.01
      else if r.W_SF == -1 then r.power1*0.1
      else r.power1*1}))

But I get a message that it is expecting a float and it found an INT. I have changed the modbus input in telegraf to FIXED so it will receive as a float. But I still get the same message. How could I fix this?

Thank you