Time Sum of item

I have Item and I want Sum of time when switch is ON. Time has to be from 00:00 to 23:59, save to database and reset time.
In sitemap I want display this time with switch with interval ‘current day’ , ‘current day + yesterday’ , ‘week’ , ‘month’.
How to do it?

The easiest way to do this is to set up persistence on the Switch. NOTE: apparently rrd4j no longer works with Switches but I am having success with InfluxDB. When it saves ON to the database it stores it as a 1 which is handy for this. Configure the persistence on this Switch to save every minute. Therefore if we get the sum of values since the start of day it will be the number of minutes the Switch was on.

val Number onToday = MySwitch.sumSince(now.withTimeAtStartOfDay, "influxdb")
val Number onPastTwoDays = MySwitch.sumSince(now.minusDays.withTimeAtStartOfDay, "influxdb")
val Number onPastWeek = MySwitch.sumSince(now.minusDays(7).withTimeAtStartOfDay, "influxdb")
val Number onPastMonth = MySwitch.sumSince(now.minusDays(30).withTimeAtStartOfDay", "influxdb")

So finally I started influxDB. I do not know exactly how to make the sum of times when it was Switch in position ON.

Use the sumSince method.

Not work. Time when switch item kotol was ON is about 3 minutes.
Item:

String	kotolDailyStat "Day stats [%s]"

rule:

rule	"denna statiska spustenia kotlu"
when
	Item kotol changed to OFF
then
	val Number onToday = kotol.sumSince(now.withTimeAtStartOfDay, "influxdb")
	postUpdate(kotolDailyStat,onToday)
end

log:

01:29:00.006 [INFO ] [marthome.event.ItemStateChangedEvent] - kotolDailyStat changed from NULL to 0

I forgot to mention that you still have to save the Item?s state every minute, not just on every change it every today.

I have save every minute. Must be only everyMinute or can be everychage too?

If you use both then you will get incorrect summations. Using sumSince in this way assumes you have one record per minute, no more, no less.

Now I have this in log:

16:33:01.949 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'jk.rules', using it anyway:
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.
The use of wildcard imports is deprecated.

My imports:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.openhab.core.items.*
import org.joda.time.*

If you are on OH 2.x you should no longer import anything from org.openhab nor from org.joda. Remove those imports.

In the future, if you do have something you need to import (e.g. java.util.Map), you should import just those classes you use. Do not use the * wild card in imports, like the warnings say.