Reading history Values and calculating differences - Energy meter

Im gathering data from my heat pump energy counter.
After few months I see no that this data is not accurate.

I use this rules to calculate usage for this day, this month and this year:

// Triggers:
// - Every 10 minutes

// context: r_pompa_zuzycie1-7
var Number pompa_dzis1
var Number pompa_miesiac1
var Number pompa_rok1

if (pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1).withDayOfYear(1)) === null) { pompa_rok1 = 0 } else {
pompa_rok1 = pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfYear(1)).state as Number }

if (pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)) === null) { pompa_miesiac1 = 0 } else { 
pompa_miesiac1 = pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0).withDayOfMonth(1)).state as Number }

if (pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)) === null) { pompa_dzis1 = 0 } else { 
pompa_dzis1 = pompa_zuzycie.historicState(now.withHour(0).withMinute(0).withSecond(0)).state as Number }

var pompa_obecnie = pompa_zuzycie.state as Number
val pompa_dzis3 = pompa_obecnie - pompa_dzis1
val pompa_miesiac3 = pompa_obecnie - pompa_miesiac1
val pompa_rok3 = pompa_obecnie - pompa_rok1
postUpdate(pompa_dzis, pompa_dzis3)
postUpdate(pompa_miesiac, pompa_miesiac3)
postUpdate(pompa_rok, pompa_rok3)

After all of this when I show this data in grafana I see that summ of values is not same as calculation from rules for this month:
First day of month is accurate:

Summ for two days and more is not accurate:

Anybody can find was cause this difference and how to fix it? Because for whole month difference is so big that it feels like one day is missing. But there isn’t.