Record/graph time lights are on/off

Is there a better way to do this? Basically I want to measure and show (in grafana) how long each light is on a day. This is giving me a count with a timesamp but I’ll have to write queries to calculate the actual on/off time. Is there a way this can be done in OH?

rule "Pantry light switches"
when
  Item PantryLight_Brightness changed
then
  if (PantryLight_Brightness.state == 100) {
    Pantry_Switch_Count.postUpdate(1)
  }else{
    Pantry_Switch_Count.postUpdate(0)
  }
end

There are various methods of cumulative calculation here