Calculate total of digital values

I’m wondering if there’s any ways to calculate the total time that a field value is equal to a certain value? Example:

A light has ON and OFF states. When it’s ON, the light_state = 1, when it’s off, light_state=0. I’d like to calculate the total time that the light has been on in a certain time period. What should I do?

It is similar to integral(), but slightly different because the values are digital. And I only need the time duration.

Any suggestions?