# Fluxlang query tu sum when value is high

**URL:** <https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705>\
**Category:** Fluxlang\
**Created:** [February 4, 2022, 8:38pm UTC](https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705 "2022-02-04T20:38:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitrus](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mitrus/32/9416_2.png) [@mitrus](https://community.influxdata.com/u/mitrus)\
**Post date:** [February 4, 2022, 8:38pm UTC](https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705/1 "2022-02-04T20:38:31Z")

</div>

Hallo

I have a query which returns me a value of digital ouput which controls my home heater.

```auto
from(bucket: "hass")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["entity_id"] == "sterowanie_piecem")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> yield(name: "last")

```

is it a way to sum in minutes how long value was high?  
I’ve tried sum, cumulativesum(), aggregativewindow etc without success.

With Influxdb 1.x and Grafana 6 I just had query like below (example is for last 12h):

```auto
"SELECT sum("value") FROM "Status" WHERE ("name" = 'sterowanie_piecem') AND time >= now() - 12h GROUP BY time(1m) fill(previous)"

```

thanks for help  
Piotr

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [February 7, 2022, 6:28pm UTC](https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705/2 "2022-02-07T18:28:57Z")

</div>

Hello @mitrus,  
Welcome!  
I’m not really sure how your InfluxQL query gave you how long a value was high (ie above a certain value) but here’s a translation of your query:

```auto
import "experimental"

from(bucket: "hass")
  |> range(start: -12h, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "Status")
  |> filter(fn: (r) => r["name"] == "sterowanie_piecem")
  |> aggregateWindow(every: 1m, fn: sum, createEmpty: true)
  |> experimental.fill(usePrevious: true)
  |> yield(name: "translation")

```

But your query doesn’t seem to find " to sum in minutes how long value was high?".  
For something like that I would do:

> **[stateDuration() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/stateduration/)**
>
> stateDuration() returns the cumulative duration of a given state.

```auto
|> from(bucket: "bucket")
|> range(start:-1d, stop:now())
|> filter(fn: (r) => r["_measurement"] == "measurement")
|> filter(fn: (r) => r["tagkey"] == "tagvalue")
|> filter(fn: (r) => r["fieldkey"] == "fieldvalue")
|> filter(fn: (r) => r["fieldkey"] == "fieldvalue")
|> stateDuration(fn: (r) => r._value > 10)
|> filter(fn: (r) => r["stateDuration"] > 0)
|> sum(column: "stateDuration)

```

---

<div class="post-metadata">

**Author:** ![mitrus](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/mitrus/32/9416_2.png) [@mitrus](https://community.influxdata.com/u/mitrus)\
**Post date:** [February 7, 2022, 9:45pm UTC](https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705/3 "2022-02-07T21:45:33Z")

</div>

I didn’t say my Flux query works. I just wan’t to find solution. My second query from Influxdb 1.x and Grafana 6 works fine and show correct values.

```auto
"SELECT sum("value") FROM "Status" WHERE ("name" = 'sterowanie_piecem') AND time >= now() - 12h GROUP BY time(1m) fill(previous)"

```

here values are grouped each one minute and sum of groups is presented on Gauge.

But I can’t reproduce this in flux still

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [March 23, 2022, 8:14pm UTC](https://community.influxdata.com/t/fluxlang-query-tu-sum-when-value-is-high/23705/4 "2022-03-23T20:14:50Z")

</div>

Hello @mitrus,  
This is the translation:

```auto
import "experimental"

from(bucket: "hass")
  |> range(start: -12h, stop: now())
  |> filter(fn: (r) => r["_measurement"] == "Status")
  |> filter(fn: (r) => r["name"] == "sterowanie_piecem")
  |> aggregateWindow(every: 1m, fn: sum, createEmpty: true)
  |> experimental.fill(usePrevious: true)
  |> yield(name: "translation")

```

Can you share your raw data, the results from your influxql query and your flux query?  
Thanks
