Duration of switch ON time in hour_minutes_seconds

InfluxDB in IOT.
I have a switch, sensor, motor, … - they have ON, OFF status. I would like a calculate the ON, OFF duration of the device.

If Iam choosing an interval I would like to see how much time was ON and OFF that specific device.
I would like the correct duration value not just approximation, becuse I have all the data in DB. Is tehre any method, aggregation, any tool what can I use for such calculation ?

Is this posibbile with InfluxDB ? Do you have a working example? If not is there any plugin for that ? Maybe I need to use different DB then InfluxDb ?
Thx for the response.