Expression tag error

Hello,
I have created an expression tag to increment a value every second based on State tag and want to retain the value.
My expression is
if({[.]State}=1,{[.]ActualTime}+1,{[.]ActualTime}+0)

If state = 1 increment actual time tag if State tag <> 0 keep actual time last value
but it gives error Expression left operand is null.
but if i write the expression as if({[.]State}=1,{[.]ActualTime}+1,0)
the value increments but as soon as state tag <> 0 value becomes zero.
Any help appreciated.

Regards,
Narayan

Wrap the whole thing in a try() expression function, and its fallback value will be your startup value. If you also want the count to survive expression start, you’ll need to use something else. Also, if you count is supposed to represent time, keep in mind that scan classes/tag group paces are not very precise. If you need to track time, record and do math on timestamps.

1 Like

Thank you sir, this solution helped.
now my expression is
try(if({[.]State}=1,{[.]ActualTime}+1,{[.]ActualTime}+0),0)