Reduce binary time series

Hi!

I’m using influx to gather the rotational speed of some motors via OPC UA. this works great and now I have the task to process the data further.

In the screenshot you see the raw values of the sensor as blue graph:

In a first step I used the map() function to define a threshold to distinguish a work cycle. In the code below every time the speed exceeds 200 rpm the value is capped at 200. In the end the mapped values would be 1 and 0 but for development I like to see the graph.

from(bucket: "system_data")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_field"] == "FEU-BS01")
  |> map(fn: (r) => ({_measurement: r._measurement,
                      _time       : r._time,
                      _field      : r._field, 
                      _value      : if r._value > 200 then 200 else 0})
        )

This gives me the purple graph which only has two values, 200 or 0, like a digital signal only has “high” or “low”.

The next step would be to reduce the data further, and this is where I fail. In the end I need a table/bucket where just the begin and the end when the graph is “high” or “low”. This should look like this:

value        start                        stop
200          2022-08-01T06:00:00          2022-08-01T06:12:20
  0          2022-08-01T06:12:20          2022-08-01T06:23:40
200          2022-08-01T06:23:40          2022-08-01T06:31:00
  0          2022-08-01T06:31:00          2022-08-01T06:35:10
200          2022-08-01T06:35:10          2022-08-01T06:35:10
...

How can I do this?

Kind regards Christoph