Calculated field

Hello everyone, I would like to create a calculated field with flux with temperature and humidity but I don’t know how to do it. My query looks like this for now


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

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

  |> filter(fn: (r) => r["_field"] == "uplink_message_decoded_payload_TempC_SHT" or r["_field"] == "uplink_message_decoded_payload_Hum_SHT")

  |> filter(fn: (r) => r["host"] == "abricotdbaronnies")

  |> filter(fn: (r) => r["topic"] == "v3/baronnies-capteurs@ttn/devices/eui-a84041f68184535c/up")

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

  |> yield(name: "mean")

And the calculation is as follows: 0.2831H^0.2735T + 0.0003018* H^2 + 0.01289*H - 4.0962
with T : temperature and H: humidity

Thank you for your answer.

Hello @Mayelle26,

You can do this with the function map():

|> map(

  fn: (r) => ({

      r with

      calculation: 0.2831 * r.rh_column ^ (0.2735 * float(v:r.temp_column)) + 0.0003018 * r.rh_column^ 2.0 + 0.01289 * r.rh_column- 4.0962

  })

)

You can find a similar topic here:

Thank you very much for your answer, I will try that now. And thank you also for the link I’ll look at it, I couldn’t find a similar link there are too many things :slight_smile:

1 Like

No worries, let me know if it works :slight_smile:

1 Like

we will say that it works halfway

i added the function :

from(bucket: "dbbaronnies")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["_field"] == "uplink_message_decoded_payload_Hum_SHT" or r["_field"] == "uplink_message_decoded_payload_TempC_SHT")
  |> filter(fn: (r) => r["host"] == "abricotdbaronnies")
  |> filter(fn: (r) => r["topic"] == "v3/baronnies-capteurs@ttn/devices/eui-a8404173118446cb-philibert1/up")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> map(
  fn: (r) => ({
      r with
      Temp_Hum: 0.2831 * r.uplink_message_decoded_payload_Hum_SHT ^ (0.2735 * float(v:r.uplink_message_decoded_payload_TempC_SHT)) + 0.0003018 * r.uplink_message_decoded_payload_Hum_SHT^ 2.0 + 0.01289 * r.uplink_message_decoded_payload_Hum_SHT- 4.0962
  }))

there is no value in the calculated field and it appears twice (once with the temperature and once with the humidity) I would just like it to add a column with “Temp_Hum”

@Mayelle26 You need to pivot the data before map so that each row has uplink_message_decoded_payload_Hum_SHT and uplink_message_decoded_payload_TempC_SHT columns:

from(bucket: "dbbaronnies")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
    |> filter(fn: (r) => r["_field"] == "uplink_message_decoded_payload_Hum_SHT" or r["_field"] == "uplink_message_decoded_payload_TempC_SHT")
    |> filter(fn: (r) => r["host"] == "abricotdbaronnies")
    |> filter(fn: (r) => r["topic"] == "v3/baronnies-capteurs@ttn/devices/eui-a8404173118446cb-philibert1/up")
    |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
    |> map(
        fn: (r) =>
            ({r with Temp_Hum:
                    0.2831 * r.uplink_message_decoded_payload_Hum_SHT ^ (0.2735 * float(
                                v: r.uplink_message_decoded_payload_TempC_SHT,
                            )) + 0.0003018 * r.uplink_message_decoded_payload_Hum_SHT ^ 2.0 + 0.01289
                        *
                        r.uplink_message_decoded_payload_Hum_SHT - 4.0962,
            }),
    )
1 Like

Thank you very much, it’s perfect!!

have a very nice day :slight_smile: