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.2735 T + 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:

I’m logging voltage and current of electric appliances (eg. lights) in my use case, the idea being I’d like to calculate the daily energy consumption. Following are the concerns:
How do I multiply voltage and current of each data point, and store it in a new field (Power)
Let’s say the frequency of data collection is 5 minutes. How do I calculate the time difference (in minutes) between two successive data points? This would enable me to multiply power with time, and store the result as a new …

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

1 Like

No worries, let me know if it works

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”

scott
May 13, 2022, 3:55pm
#6
@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