Fluxlang query tu sum when value is high

Hallo

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

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):

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

thanks for help
Piotr

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:

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:

|> 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)

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.

"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

Hello @mitrus,
This is the translation:

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