How to get the values of yesterday --- like this .deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT))

Hi, i want to get the kwh from my powermeter for several times.

For today i use this in a cron rule at 00:00 oclock (midnight).

var Number stromverbrauch_sdm630_heute = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT))

How can i get the value for yesterday, when i start the rule on midnight?

And how can i get the value for the current day, when i start the rule during the day? (from last midnight to now)

1 Like

It’s not clear what value is stored in the database. Does it revert to 0 at midnight or is it forever increasing?

In either case, the absolute easiest and most fool proof would be to store stromverbrauch_sdm630_heute into an Item that represents, “yesterdays” total at midnight.

If the value is forever increasing you can calculate it by subtracting the delta for today from the delta since yesterday.

var todayTotal = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT))
var yesterdayTotal = SDM_kwh_Totalkwh.deltaSince(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(1)) - todayTotal

You already are. You are calculating the delta between now and midnight no matter when this rule runs.

If that works now no problem. But depending on timing, triggering at 00:00 might mean that LocalTime.MIDNIGHT is actually now or no more than a few milliseconds from now. You might trigger this a second before midnight or have to take that into account.

1 Like