Kapacitor - StateDurationNode write only max duration into influxdb

Hello everybody,

I am using the Kapacitor to get the duration of a state with the node StateDurationNode.

For example the input raw data data is:

timestamp [s] value [boolean]
0 FALSE
1 TRUE
2 TRUE
3 TRUE
4 TRUE
5 FALSE
6 FALSE

The expected result is:

timestamp [s] value [s]
1 3

The current result is:

timestamp [s] value [s]
2 1
3 2
4 3

Source code:

stream
    |from()
        .measurement('data')
    |stateDuration(lambda: "state" == TRUE)
        .unit(1s)
        .quiet()
        .as('durations')
    |httpOut('duration')
    |influxDBOut()
        .database('test')
        .measurement('durations')

Is there is more elegent solution?

stream
    |from()
        .measurement('data')
    |stateDuration(lambda: "state" == TRUE)
        .unit(1s)
        .quiet()
        .as('state_duration')
    |delete()
        .field('state')
    |influxDBOut()
        .database('telegraf')
        .measurement('durations')
        
stream
	|from()
    	.measurement('durations')
    |difference('state_duration')
    	.as('diff')
    |where(lambda: "diff" < 0)
    |influxDBOut()
        .database('telegraf')
        .measurement('diff')
```