How to retrive total duration based on a value state?

Hi,
I trying to get total duration of value.
I have table where possible values for each record is integer 1 or 2. Value 1 means the machine is running and value 2 means the machine is stopped.
Can you please help me how to get total duration where the value is equal 1 (machine is running) ?
Thanks a lot.

this dont work for me

from(bucket: "MotoTest2")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "M1")
  |> stateDuration(fn: (r) => r.state == 2)
  |> yield(name: "state_duration")