Get each max or min from cycles

Hi, I’m monitoring the temperatur of a compressor. I know when it is ON or OFF and, of course, his temperature. What I would like to get is each max when it’s ON and each min when it is OFF.
I could achieve a lot with that script below, but the given temperatures are when the system switches ON or OFF, not the min/max somewhere in between. Any advices would be helpfull. Thanks

span = -8h
raw = from(bucket: “compres”)
|> range(start: span)
|> filter(fn: (r) =>
(r[“_field”] == “ComprON”))
|> aggregateWindow(every: 1s, fn: first)
|> fill(column: “_value”, usePrevious: true)
|> map(fn: (r) =>
({r with _level: if r._value > 0.0 then “swON” else “swOFF”}))
|> keep(columns: [“_measurement”, “_time”, “_level”])
tempe = from(bucket: “compres”)
|> range(start: span)
|> filter(fn: (r) =>
(r[“_field”] == “TempKomp”))
|> aggregateWindow(every: 1s, fn: mean)
|> fill(column: “_value”, usePrevious: true)
|> map(fn: (r) =>
({r with Tkomp: r._value}))
|> keep(columns: [“_measurement”, “_time”, “Tkomp”])
ON = raw
|> monitor.stateChanges(fromLevel: “swOFF”, toLevel: “swON”)
OFF = raw
|> monitor.stateChanges(fromLevel: “swON”, toLevel: “swOFF”)
over = union(tables: [ON, OFF, tempe])
|> group(columns: [“_time”], mode: “by”)
|> sort(columns: [“_time”])
|> fill(column: “Tkomp”, usePrevious: true)
|> tail(n: 1)
|> group()
|> filter(fn: (r) =>
(exists r._level))
temp = over
|> elapsed(unit: 1s, timeColumn: “_time”, columnName: “tcycle”)

Hi @julien2,
I hope you are doing well. Sorry, I am a little confused about what results your trying to gather. Could you provide some sample data and maybe your expected result?

Thanks,
Jay

Hi @Jay_Clifford,
In other words, what I would like to do is to “aggregateWindow()” but the window is not defined by time but by another variable like, in my case, ComprON. I’m new user so can’t upload data set but here are some points:

_time,TempComp,ComprON
2022-05-30T13:06:24Z,56.101875,1
2022-05-30T13:06:25Z,56.101875,1
2022-05-30T13:06:26Z,55.9775,1
2022-05-30T13:06:27Z,55.9775,1
2022-05-30T13:06:28Z,55.7909375,1
2022-05-30T13:06:29Z,55.6665625,1
2022-05-30T13:06:30Z,55.604375,1
2022-05-30T13:06:31Z,55.48,1
2022-05-30T13:06:32Z,55.355625,0
2022-05-30T13:06:33Z,55.355625,0
2022-05-30T13:06:34Z,55.355625,0
2022-05-30T13:06:35Z,55.48,0
2022-05-30T13:06:36Z,55.48,1
2022-05-30T13:06:37Z,55.5421875,1
2022-05-30T13:06:38Z,55.72875,1
2022-05-30T13:06:39Z,55.853125,1
2022-05-30T13:06:40Z,55.9153125,1
2022-05-30T13:06:41Z,55.9775,1
2022-05-30T13:06:42Z,56.0396875,0
2022-05-30T13:06:43Z,56.1640625,0
2022-05-30T13:06:44Z,56.2884375,0
2022-05-30T13:06:45Z,56.2884375,0
2022-05-30T13:06:46Z,56.2884375,1
2022-05-30T13:06:47Z,56.475,1
2022-05-30T13:06:48Z,56.5371875,1
2022-05-30T13:06:49Z,56.5371875,1
2022-05-30T13:06:50Z,56.599375,1

Then from each sequence where ComprON is 1 I would like to get max of TempComp.
Thanks
Julien

Ah sorry, @julien2 completely understand now! You can do this by grouping on ComprON

import "csv"

csvData =
    "
_time,TempComp,ComprON
2022-05-30T13:06:24Z,56.101875,1
2022-05-30T13:06:25Z,56.101875,1
2022-05-30T13:06:26Z,55.9775,1
2022-05-30T13:06:27Z,55.9775,1
2022-05-30T13:06:28Z,55.7909375,1
2022-05-30T13:06:29Z,55.6665625,1
2022-05-30T13:06:30Z,55.604375,1
2022-05-30T13:06:31Z,55.48,1
2022-05-30T13:06:32Z,55.355625,0
2022-05-30T13:06:33Z,55.355625,0
2022-05-30T13:06:34Z,55.355625,0
2022-05-30T13:06:35Z,55.48,0
2022-05-30T13:06:36Z,55.48,1
2022-05-30T13:06:37Z,55.5421875,1
2022-05-30T13:06:38Z,55.72875,1
2022-05-30T13:06:39Z,55.853125,1
2022-05-30T13:06:40Z,55.9153125,1
2022-05-30T13:06:41Z,55.9775,1
2022-05-30T13:06:42Z,56.0396875,0
2022-05-30T13:06:43Z,56.1640625,0
2022-05-30T13:06:44Z,56.2884375,0
2022-05-30T13:06:45Z,56.2884375,0
2022-05-30T13:06:46Z,56.2884375,1
2022-05-30T13:06:47Z,56.475,1
2022-05-30T13:06:48Z,56.5371875,1
2022-05-30T13:06:49Z,56.5371875,1
2022-05-30T13:06:50Z,56.599375,1
"

data = csv.from(csv: csvData, mode: "raw")
|> group(columns: ["ComprON"])
|> yield(name: "raw")|> map(fn: (r) => ({ r with _value: float(v: r.TempComp)}))

max = data 
  |> max()
  |> yield(name: "max")

min = data 
  |> min()
  |> yield(name: "min")

Hi @Jay_Clifford ,
Thank you for your time and proposal.
In fact, I should get three max values, as there is three sequences where ComprON is 1.
You see my point ?
In advance thank you.