Statistics for heating

I have my heatings cables connected to the gpio of my pi, then I have several rules which switches them on and off.

Just for curiosity I would like to know how much of the time the heating is on. So I would like to have a string item which says : Heating LivingRoom 35% in last 24H and 14% so far this year, ca 1kWh last 24H, ca 340kWh so far this year. Or something similiar to this.

I guess I need to make some items that I will set in my heating on rule.

timeOn = now()

then in heating off:

deltaT = now()-timeOn
totalT = totalT+deltaT

totalPercent = toallT/(now()- startUpTime)

But I am a bit lost how to do it exactly, especially with persitance.