# How to plot a machine cycle?

Hi Everyone,

We have a requirement wherein we are recording Pressure in our machine to a particular measurement. The Machine starts it Cycle when pressure is > 80 and cycle ends when Pressure < 1. We are using Grafana Timeseries Panel to plot the cycle Graph. I was wondering how can we achieve this? So far I have below query:

``````from(bucket: "MyBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "MyTag")
|> aggregateWindow(every: v.windowPeriod, fn: (column, tables=<-) => tables
|> quantile(q: 0.99, column:column), createEmpty: false)
|> map(fn: (r) => ({ r with _value: (if r["_value"] > 80 then 1 else 0) }))
``````

Problem with above query is that it ends the cycle as soon as pressure < 80 but I need to end the cycle only when it is < 1 and next cycle should start when again pressure > 80.

I think I am missing something trivial here.

Could someone please point me in the right direction here ?

Hi. I am trying to envision this in Grafana, and what I would expect is a time series graph going up and down, sort of like this:

Is your goal to have Grafana display ONLY those sections where pressure reaches 80 and then falls below 1? In other words, just the sections like this?

I do not think Grafana (or Influx) can (via a query) filter out only the time slices you wish to view. You can set alerts or colored thresholds to indicate when you exceed 80 and when it falls below 1.

Maybe I am misunderstanding your goal?

@grant1 Thanks for responding!!
Not exactly what I am looking for. Let me explain it in different manner.
Consider we have a boolean flag which indicates if machine is ON or OFF. We need to turn it ON when pressure > 80 and OFF when < 1. The current implementation has a problem because when pressure falls below 80 it considers it OFF. So below steps should happen:

• pressure > 80 → ON
• Once ON if pressure < 80 but > 1 than still ON
• pressure < 1 than OFF
• Once OFF we turn it ON only when pressure > 80

Hope that helps in understanding the issue that I am facing

Hi @grant1

I have written another query:

``````onFlag = false
level = if float(v:v) >= 80.0 then 1
else if float(v:v) < 80.0 and float(v:v) > 1 and onFlag == true then 1
else if float(v:v) < 80.0 and float(v:v) > 1 and onFlag == false then 0
else 0

onFlag = if level == 1 then true else false

return level
}

from(bucket: "MyBucket")
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "MyTag")
|> aggregateWindow(every: v.windowPeriod, fn: (column, tables=<-) => tables
|> quantile(q: 0.99, column:column), createEmpty: false)
|> map(fn: (r) => ({ r with _valueOld: r["_value"] }))
|> map(fn: (r) => ({ r with _value: alertLevel(v: r["_value"]) }))
|> map(fn: (r) => ({ r with _valueFlag: onFlag }))
``````

As you can notice I am trying to manipulate onFlag variable in alertLevel function but somehow it is not working. Can you suggest what is wrong with it?

Hi again. Your explanation is now very clear, but unfortunately I do not know of a way to do this using Flux. Maybe someone in this community can offer some advice.