The answer is partially yes. Persistence will let you sum up (or average, or find the min, or find the max) of all the values stored in the database between now and some time in the past. So if you want to know how much water has been used in the past 30 days, for example, we can easily handle that.
But for that to work, you will need to transfer the running total Item’s value to an Item that just stores the value at midnight just before it gets reset.
Or, you can not have vHotWater_totalGallons persisted automatically and call vHotWater_totalGallons.persist
at midnight to just store the value at midnight. But I don’t recommend that approach because if OH happens to restart during the day, you will lose all the data that was collected up to that point.
To handle periods like the lat 30 days you can just use calls like
vHotWater_totalGallons.sumSince(now.minusDays(30))
If you want just for the current month, you will have to do some tricks to calculate the number of days since the start of the month. The now.withDayOfMonth
will probably be your best bet.
vHotWater_totalGallons.sumSince(now.minusDays(now.withDayOfMonth.dayOfMonth))
But if I understand correctly, you want to have a sum of values just for Mondays, and another one just for Tuesdays, and so on.
To do this I think your approach is probably the way to go but I think there is a better way to do it.
If you name your Items as vHotWater_Gallons_ByDay_1, for example, then you can completely replace the switch statement with a single line:
postUpdate("vHotWater_Gallons_ByDay_"+now.getDayOfWeek, vHotWater_totalGallons.state.toString)
Using the above, you can expand this to also update your current month and current year totals (there is a withDayOfYear).