# Get each max or min from cycles

**URL:** <https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197>\
**Category:** Systems\
**Tags:** influxdb, flux\
**Created:** [May 29, 2022, 8:34am UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197 "2022-05-29T08:34:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![julien2](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julien2](https://community.influxdata.com/u/julien2)\
**Post date:** [May 29, 2022, 8:34am UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197/1 "2022-05-29T08:34:29Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/f/f0b498078e3fa46b396dcb82d27af06fa511ca44.png)

---

<div class="post-metadata">

**Author:** ![Jay\_Clifford](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jay_clifford/32/8287_2.png) [@Jay\_Clifford](https://community.influxdata.com/u/Jay_Clifford)\
**Post date:** [May 30, 2022, 12:07pm UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197/2 "2022-05-30T12:07:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![julien2](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julien2](https://community.influxdata.com/u/julien2)\
**Post date:** [May 30, 2022, 2:23pm UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197/3 "2022-05-30T14:23:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jay\_Clifford](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/jay_clifford/32/8287_2.png) [@Jay\_Clifford](https://community.influxdata.com/u/Jay_Clifford)\
**Post date:** [May 30, 2022, 2:50pm UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197/4 "2022-05-30T14:50:03Z")

</div>

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

```auto
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")

```

---

<div class="post-metadata">

**Author:** ![julien2](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julien2](https://community.influxdata.com/u/julien2)\
**Post date:** [May 30, 2022, 3:36pm UTC](https://community.influxdata.com/t/get-each-max-or-min-from-cycles/25197/5 "2022-05-30T15:36:36Z")

</div>

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.
