# Sum of two values (bytes-in + bytes-out)

**URL:** <https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435>\
**Category:** Fluxlang\
**Created:** [March 29, 2022, 4:28am UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435 "2022-03-29T04:28:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [March 29, 2022, 4:28am UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/1 "2022-03-29T04:28:18Z")

</div>

Hello,

I’m trying to add two values (bytes-in + bytes-out) in flux only as Grafana transform is not working as I need.  
My query is :

import “date”  
month = date.truncate(t: now(), unit: 1mo)  
from(bucket: “telegraf/autogen”)  
|\> range(start: month)  
|\> filter(fn: (r) =\> r["\_measurement"] == “snmp\_interfaces” and r[“hostname”] == “$hostname” and r.\_field =~ /bytes-/)  
|\> last ()  
|\> pivot(rowKey:["\_time"], columnKey: ["\_field"], valueColumn: “\_value”)  
|\> map(  
fn: (r) =\> ({  
\_time: r.\_time,  
\_measurement: r.\_measurement,  
\_field: “totalConso”,  
\_value: int(v: bytes-in + bytes-out)  
})  
)

I’v tried to do it like it’s wrote in documentation, but that didn’t work as expected.  
I have this error : invalid: compilation failed: error at @13:34-13:36: invalid expression: invalid token for primary expression: IN error at @13:39-13:44: invalid expression @13:34-13:36: in  
Does someone could help me with that ?

Regards

---

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [March 31, 2022, 8:15am UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/2 "2022-03-31T08:15:11Z")

</div>

Hello,

I “up” this topic because I haven’t found the solution to this problem.  
Can someone could help me with this ?

Regards,

---

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [March 31, 2022, 12:13pm UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/3 "2022-03-31T12:13:35Z")

</div>

Following advice on slack, I changed the query like that :  
`import "date"`  
`month = date.truncate(t: now(), unit: 1mo)`  
`from(bucket: "telegraf/autogen")`  
` |> range(start: month)`  
` |> filter(fn: (r) => r["_measurement"] == "snmp-interface")`  
` |> filter(fn: (r) => r["host"] == "${hostname}")`  
` |> filter(fn: (r) => r["host"] == "${Interface}}")`  
` |> filter(fn: (r) => r["_field"] == "bytes-in" or r["_field"] == "bytes-out")`  
` |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`  
` |> map(fn: (r) => ({ _value: r.bytes-in + r.bytes-out }))`  
` |> sum()`  
But I still have de same error

Regards

---

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [March 31, 2022, 1:03pm UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/4 "2022-03-31T13:03:20Z")

</div>

Hey,

In telegraf, I moved from bytes-in to bytesIn and rewrite my queries.  
Everything is working now.  
I don’t know if it’s the “-” ou “in” which if the problem.

Regards,

Vincent

---

<div class="post-metadata">

**Author:** ![ypnos](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ypnos/32/7630_2.png) [@ypnos](https://community.influxdata.com/u/ypnos)\
**Post date:** [April 1, 2022, 11:05am UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/5 "2022-04-01T11:05:08Z")

</div>

Probably it would work if you used `r["bytes-in"] + r["bytes-out"]`.  
The dot notation is just syntactic sugar and it doesn’t work for all column names as you can see.

Here is what you had just written with different spacing. Spot the problem?  
`_value: r.bytes - in+r.bytes - out`

---

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [April 4, 2022, 10:08am UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/6 "2022-04-04T10:08:31Z")

</div>

@ypnos.

Thank you for the tips. I’ll keep it for futur.  
I still have a flux problem.I have 2 queries : The first one show the traffic evolution during the current month

 ![SnapCrab_NoName_2022-4-4_12-3-39_No-00](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/efee692e21f086da7cf47eaf2d8eed21117937d0.png)  
The second should be the sum of in and out bytes.  
 ![SnapCrab_NoName_2022-4-4_12-4-50_No-00](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/b/b96446a5377a5bd58bddbc0b65182c0a62b5e410.png)  
As you can see, I should have around 6GB, but I got 102.

```auto
My queries are:
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()

```

```auto
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesOut")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()

```

For the evolution and

```auto
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
  |> last ()
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({r with _value: (r.bytesIn + r.bytesOut) / 1073742824 }))
  |> sum ()

```

For the sum.  
What I doing wrong with the second query ?

Regards,

---

<div class="post-metadata">

**Author:** ![ypnos](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ypnos/32/7630_2.png) [@ypnos](https://community.influxdata.com/u/ypnos)\
**Post date:** [April 4, 2022, 10:22pm UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/7 "2022-04-04T22:22:01Z")

</div>

What do you use last() for?

As you are calculating the overall sum, you also don’t need to pivot and calculate sum of in+out in the map. This should work as well:

```auto
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
  |> group()
  |> sum()
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

```

---

<div class="post-metadata">

**Author:** ![Tiki10](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tiki10/32/9735_2.png) [@Tiki10](https://community.influxdata.com/u/Tiki10)\
**Post date:** [April 6, 2022, 12:27pm UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/8 "2022-04-06T12:27:34Z")

</div>

> [@ypnos](#):
>
> ```auto
> import "date"
> month = date.truncate(t: now(), unit: 1mo)
> from(bucket: "telegraf/autogen")
> |> range(start: month)
> |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
> |> filter(fn: (r) => r["hostname"] == "${hostname}")
> |> filter(fn: (r) => r["if-name"] == "${Interface}")
> |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
> |> group()
> |> sum()
> |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
> 
> ```

Hello Ypnos,

Thank you for your reply and your help.  
In fact, the way how I try to get values is wrong.  
bytesIn and bytesOut are counter, so they are alway incremented.  
With $month variable, I’m able to know from how much this counter was incremented since the begin of the month.  
This is the queries I’m using (thank for Slack help):

```auto
import "date"
month = date.truncate(t: now(), unit: 1mo)
from(bucket: "telegraf/autogen")
  |> range(start: month)
  |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
  |> filter(fn: (r) => r["hostname"] == "${hostname}")
  |> filter(fn: (r) => r["if-name"] == "${Interface}")
  |> filter(fn: (r) => r["_field"] == "bytesIn")
  |> map(fn: (r) => ({r with _value: r._value / 1073741824}))
  |> increase()

```

And the same for bytesOut.  
If I want to know the sum of bytesIn or bytesOut, I can use my calculator 😃 or use the Grafana Transform (not working as good as it should do) or use Flux to make the sum of consumption In and Out. This is where I fail.  
could I put the result of query in variable and then sum these queries ?

regards

---

<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:** [April 6, 2022, 3:48pm UTC](https://community.influxdata.com/t/sum-of-two-values-bytes-in-bytes-out/24435/9 "2022-04-06T15:48:06Z")

</div>

@Tiki10 You should only need one query for the evolution line graph. This should work:

```javascript
import "date"

month = date.truncate(t: now(), unit: 1mo)

from(bucket: "telegraf/autogen")
    |> range(start: month)
    |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
    |> filter(fn: (r) => r["hostname"] == "${hostname}")
    |> filter(fn: (r) => r["if-name"] == "${Interface}")
    |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
    |> increase()
    |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

```

And for the sum total of both bytesIn and bytesOut since the beginning of the month:

```javascript
import "date"

month = date.truncate(t: now(), unit: 1mo)

from(bucket: "telegraf/autogen")
    |> range(start: month)
    |> filter(fn: (r) => r["_measurement"] == "snmp_interfaces")
    |> filter(fn: (r) => r["hostname"] == "${hostname}")
    |> filter(fn: (r) => r["if-name"] == "${Interface}")
    |> filter(fn: (r) => r["_field"] == "bytesIn" or r["_field"] == "bytesOut")
    |> increase()
    |> group()
    |> sum()
    |> map(fn: (r) => ({r with _value: r._value / 1073741824}))

```

This is a combination of your current query and what @ypnos is suggesting.
