Hello @mfrice.
Maybe use group:
import "array"
array.from(
rows: [
{_time: 2022-01-01T00:00:03Z, _value: 1, event: "on"},
{_time: 2022-01-01T00:00:05Z, _value: 2, event: "on"},
{_time: 2022-01-01T00:00:08Z, _value: 3, event: "on"},
{_time: 2022-01-01T00:00:12Z, _value: 4, event: "on"},
{_time: 2022-01-01T00:00:17Z, _value: 3, event: "on"},
{_time: 2022-01-01T00:00:18Z, _value: 1, event: "off"},
{_time: 2022-01-01T00:00:20Z, _value: 1, event: "off"},
{_time: 2022-01-01T00:00:23Z, _value: 1, event: "off"},
{_time: 2022-01-01T00:00:27Z, _value: 6, event: "on"},
{_time: 2022-01-01T00:00:29Z, _value: 5, event: "on"},
{_time: 2022-01-01T00:00:36Z, _value: 9, event: "on"},
{_time: 2022-01-01T00:00:40Z, _value: 1, event: "off"},
{_time: 2022-01-01T00:00:43Z, _value: 7, event: "on"},
{_time: 2022-01-01T00:00:48Z, _value: 3, event: "on"},
],
)
|> map(fn: (r) => ({r with region: if r.event == "on" then 1 else 0}))
|> difference(columns: ["region"], nonNegative: true)
|> cumulativeSum(columns: ["region"])
|> group(columns: ["region"])
|> stateDuration(fn: (r) => true)
|> filter(fn: (r) => r.stateDuration <= 10)
|> reduce(
fn: (r, accumulator) => ({_time: r._time,_value: r._value + accumulator._value}),
identity: {_time: now(), _value: 0},
)
|> group()