Filtering quick changes

hi,

I’m new to kapacitor but i think it can handle my use case:

i have incoming data where devices reports their state in a field: zero or one and have a tag called ID.

i want to filter out quick state changes AKA if i get a one for the given device but i had a zero 30s ago then i want to skip that value

i wrote this script:

dbrp "main"."autogen"

var zero = 30//s
var one = 30//s

stream
    |from()
        .measurement('Devices')
    |elapsed('State',1s)
    |where(lambda: ("elapsed" > one AND "State" == 1) OR ("elapsed" > zero AND "State" == 0))
    |influxDBOut()
        .database('main')
        .measurement('Devices-filtered')

why elapsed clears all fields and tags?

can you recommend an alternative?

Thanks!