# Calculated field

**URL:** <https://community.influxdata.com/t/calculated-field/25011>\
**Category:** InfluxDB 2\
**Tags:** influxdb, flux\
**Created:** [May 12, 2022, 10:03am UTC](https://community.influxdata.com/t/calculated-field/25011 "2022-05-12T10:03:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mayelle26](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mayelle26/32/9920_2.png) [@Mayelle26](https://community.influxdata.com/u/Mayelle26)\
**Post date:** [May 12, 2022, 10:03am UTC](https://community.influxdata.com/t/calculated-field/25011/1 "2022-05-12T10:03:08Z")

</div>

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

```auto

  |> 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.2831_H^0.2735_T + 0.0003018\* H^2 + 0.01289\*H - 4.0962  
with T : temperature and H: humidity

Thank you for your answer.

---

<div class="post-metadata">

**Author:** ![Juan\_Alonso\_Pla](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/juan_alonso_pla/32/9659_2.png) [@Juan\_Alonso\_Pla](https://community.influxdata.com/u/Juan_Alonso_Pla)\
**Post date:** [May 12, 2022, 1:25pm UTC](https://community.influxdata.com/t/calculated-field/25011/2 "2022-05-12T13:25:08Z")

</div>

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:

> [@Multiply two field values and store the result in a new field](https://community.influxdata.com/t/multiply-two-field-values-and-store-the-result-in-a-new-field/11951):
>
> 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 …

---

<div class="post-metadata">

**Author:** ![Mayelle26](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mayelle26/32/9920_2.png) [@Mayelle26](https://community.influxdata.com/u/Mayelle26)\
**Post date:** [May 12, 2022, 1:39pm UTC](https://community.influxdata.com/t/calculated-field/25011/3 "2022-05-12T13:39:13Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Juan\_Alonso\_Pla](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/juan_alonso_pla/32/9659_2.png) [@Juan\_Alonso\_Pla](https://community.influxdata.com/u/Juan_Alonso_Pla)\
**Post date:** [May 13, 2022, 9:55am UTC](https://community.influxdata.com/t/calculated-field/25011/4 "2022-05-13T09:55:42Z")

</div>

No worries, let me know if it works 🙂

---

<div class="post-metadata">

**Author:** ![Mayelle26](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mayelle26/32/9920_2.png) [@Mayelle26](https://community.influxdata.com/u/Mayelle26)\
**Post date:** [May 13, 2022, 11:51am UTC](https://community.influxdata.com/t/calculated-field/25011/5 "2022-05-13T11:51:06Z")

</div>

we will say that it works halfway

i added the function :

```auto
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
  }))

```

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/9/98d21257e015338445378bf458e754d1f6ef4dc8.png)

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/c/cd7e5f76ec02c694bd00eaeac55ed689acda885b.png)

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”

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [May 13, 2022, 3:55pm UTC](https://community.influxdata.com/t/calculated-field/25011/6 "2022-05-13T15:55:05Z")

</div>

@Mayelle26 You need to [pivot](https://docs.influxdata.com/flux/v0.x/stdlib/universe/pivot/) the data before map so that each row has `uplink_message_decoded_payload_Hum_SHT` and `uplink_message_decoded_payload_TempC_SHT` columns:

```javascript
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,
            }),
    )

```

---

<div class="post-metadata">

**Author:** ![Mayelle26](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mayelle26/32/9920_2.png) [@Mayelle26](https://community.influxdata.com/u/Mayelle26)\
**Post date:** [May 17, 2022, 8:03am UTC](https://community.influxdata.com/t/calculated-field/25011/7 "2022-05-17T08:03:36Z")

</div>

Thank you very much, it’s perfect!!

have a very nice day 🙂
