Count the number of variable exceed a value

I’m using a query to show data like

from(bucket: "LaboratorioProduzione")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Postazione1")
  |> filter(fn: (r) => r["TipoTest"] == "34")
  |> filter(fn: (r) => r["_field"] == "contatore M1")
  |> aggregateWindow(every: 5s, fn: mean, createEmpty: false)
  |> yield(name: "mean")

Now I need to count the time the data goes above a certain value (300)

I can’t understand how to use the stateCount with my actual query

Hello @NicoCaldo,
I would recommend doing the following:

from(bucket: "LaboratorioProduzione")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Postazione1")
  |> filter(fn: (r) => r["TipoTest"] == "34")
  |> filter(fn: (r) => r["_field"] == "contatore M1")
  |> aggregateWindow(every: 5s, fn: mean, createEmpty: false)
  |> filter(fn: (r) => r._value >= 300.0)
  |> count() 
  |> yield(name: "mean")

thanks. The only problem is that with this approch it counts a crazy number of time when, instead, I just need to count the rising fronts. Is there a way to do that?

How about this?

from(bucket: "LaboratorioProduzione")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "Postazione1")
  |> filter(fn: (r) => r["TipoTest"] == "34")
  |> filter(fn: (r) => r["_field"] == "contatore M1")
  |> aggregateWindow(every: 5s, fn: mean, createEmpty: false)
  |> derivative(unit: 1s, nonNegative: true)
  |> filter(fn: (r) => r["_value"] > 0)
  |> count() 
  |> yield(name: "mean")