Daily value

I have a item, which is increasing depends on the power consumption.(let say 2.4. at 00:00 value was 4500 and 3.4. at 00:00 value was 4800 - so a day value is 300)
Is correct calculation for current monht this ?
item.sumSince(now.minusDays(now.withDayOfMonth.dayOfMonth))
and for today this ?
item.sumSince(now.withTimeAtStartOfDay)

But what’s for previous month and previous day ?

Thanks for help
Alex

If the Item is forever increasing than no that’s not correct. The correct calculation will be the current value minus the value it was at the time you care about.

items.state - item.historicState(now.minusDays(now.withDayOfMonth.dayOfMonth))

For today it’s the same thing, the value now minus the time 24 hours ago.

The value at the end of the month minus the value at the beginning of the month.

Note, if the value ever resets, this all gets very complicated.

Note, in OH 3+ there is no withTimeAtStartOfDay. You’ll have to use withHour(0).withMinute(0).withSecond(0).withNano(0) to get midnight.

Thanks,
I tried but I’m doing somethink wrong :

so consumption from the start of the day :

consumption.state=measurement.state - measurement.historicState(now.minusDays(now.withHour(0).withMinute(0).withSecond(0).withNano(0)))

I use this calculation

var local_timestamp_yesterday = time.toZDT('23:59:59').minusDays(1);

local_number_wallplug2dailytotalenergy = items.Z_way_number_WallPlug2_totalpower.numericState - items.Z_way_number_WallPlug2_totalpower.history.historicState(local_timestamp_yesterday).numericState

So what’s in the logs? Break the line apart and log each and every part.

The only apparent thing is that, per the docs, historicState returns a HistoricItem not a State. So you need to call .state on the result of that call to get at the value you might be able to do math with. If there was a problem or there is no data null will be returned.

consumption.state = measurement.state - measurement.historicState(now.minusDays(now.withHour(0).withMinute(0).withSecond(0).withNano(0))).state

result NULL

error log:

Type mismatch: cannot convert from ZonedDateTime to long; line 1, column 143, length 55

Your calculation is a consumption from the start of the day ?

Z_way_number_WallPlug2_totalpower ia the total power my zway plug sees …so I subtract the total power from the last day from the overall total power than I have the actual total power…

Ok, for me for consumption calculation from the start of the day it’s working this :

item.deltaSince(now.with(LocalTime.MIN))

So now I need a calculation from start of the month, previous day and previous month

my idea for previous day was this :

item.deltaBetween(now.with(LocalTime.MIN).minusDays(1),now.with(LocalTime.MIN))

but not working

tried other :

(ZonedDateTime.now().with(LocalTime.MIDNIGHT).minusDays(1))

but also this minusDays isn’t working

I had a lot of work with my rules using time values after changing from OH 2.5 to OH 3.x.

Some general infomation:
I replaced “now.withTimeAtStartOfDay” with “now.truncatedTo(DAYS)”
“yesterday” = now.minusDays(1).truncatedTo(DAYS)
“firstOfMonth” = now.minusDays(now.getDayOfMonth() - 1).truncatedTo(DAYS)
“firstOfLastMonth” = firstOfMonth.minusMonths(1)
“lastOfLastMonth” = firstOfMonth.minusSeconds(1)