How to get end states of stateDuration?

example:
type:move moveDuration:1 044 stopDuration:-1
type:move moveDuration:1 057 stopDuration:-1
type:move moveDuration:1 061 stopDuration:-1
type:stop moveDuration:-1 stopDuration:0
type:stop moveDuration:-1 stopDuration:49
type:stop moveDuration:-1 stopDuration:60

etc. with many repeate states. I want to get just last values of states:
move duration max
stop duration max
move duration max
stop duration max

Hello @s.venger,
Welcome!
So you’re using the stateDuration function?
What is moveDuration? Or are these your fields? Can you please clarify? I’m a little confused.

To get the max values I would query for all my data and store it in a variable:

myData = from(bucket: "my bucket")
                 |> range(start: <my start date>, stop: <my stop date>)
                 |> filter(fn: (r) => r._measurement ==  "my measurement")
                |> filter(fn: (r) => r.<myTagKey> ==  "my tag values")

myData |> filter(fn: (f) => r._field == "moveDuration")
|> max() 
|> yeild(name: "max moveDuration) 

myData |> filter(fn: (f) => r._field == "stopDuration")
|> max() 
|> yeild(name: "max stopDuration) 

Does that help?

I have a car with a tracker, I want to get trips and parking. The max function gives me only one event out of all, I need to get all the events chronologically. There can be a lot of sequences of recurrences of events

Yes, function is stateDuration.
For example my data screenshot for now:

there is my code in flux:

selectData = (tables=<-) => tables
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer" and r.topic =~ /561706/)

  |> filter(fn: (r) =>
    r._field =~ /position/
  )

  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> drop(columns: ["_start", "_stop", "host", "topic", "position.timestamp"])

from(bucket: "...")
  |> selectData()

  |> map(fn: (r) => ({r with
    type: if r["position.speed"] > 0 then "move" else "stop"
  }))

  |> stateDuration(fn: (r) => r.type == "move", column: "moveDuration", unit: 1s)
  |> stateDuration(fn: (r) => r.type == "stop", column: "stopDuration", unit: 1s)