thanks @jimtng ! very elegant! this is where I am with it thoughâŠ
i wanted to change the time from midnight to noon. the heater doesnt run much at all during the day over the season. so really the time i need is how much it ran overnight. so noon makes more sense. so i changed the code and changed MIDNIGHT to NOON and then the val from midNight to noon. Did not like that. So i just changed MIDNIGHT to NOON and left the midNight. that works. sorta. now i have funky numbers.
Last Runtime was 19 s
HeaterDailyRuntime: 0 s
HeaterWeeklyRuntime: 312 s
HeaterMonthlyRuntime: 293 s
So that caused the daily runtime to stop counting. and the weekly and monthly dont have the same numbers even though they should. thought about setting them back to zero and see if they just had something stored in there already.
// Number:Time HeaterRuntime "Heater's Last Runtime"
// Set it to persist on every UPDATE, not every change
rule 'Heater runtime'
when
Item main_heater_power changed to OFF
then
val previousState = main_heater_power.previousState(true, "influxdb")
logInfo("heater runtime", "Previous state result is {}", previousState);
logInfo("heater runtime", "Previous state timestamp is {}", previousState.timestamp.toString)
val runtime = Duration.between(previousState.timestamp, now)
HeaterRuntime.postUpdate(String.valueOf(runtime.toMillis / 1000) + " s")
logInfo("heater runtime", "Last Runtime was {}", HeaterRuntime.state);
val midNight = now.with(LocalTime::NOON)
HeaterDailyRuntime.postUpdate(HeaterRuntime.sumSince(midNight, "influxdb"))
logInfo("Heater Runtime", "HeaterDailyRuntime: {}", HeaterDailyRuntime.state)
HeaterWeeklyRuntime.postUpdate(HeaterRuntime.sumSince(midNight.with(java.time.temporal.ChronoField.DAY_OF_WEEK, 1), "influxdb"))
logInfo("Heater Runtime", "HeaterWeeklyRuntime: {}", HeaterWeeklyRuntime.state)
HeaterMonthlyRuntime.postUpdate(HeaterRuntime.sumSince(midNight.with(java.time.temporal.ChronoField.DAY_OF_MONTH, 1), "influxdb"))
logInfo("Heater Runtime", "HeaterMonthlyRuntime: {}", HeaterMonthlyRuntime.state)
end
Error when I change midNight to noon.
no viable alternative at input 'noon'
[19,56]: no viable alternative at input 'noon'
[19,73]: mismatched input ')' expecting 'end'
// Number:Time HeaterRuntime "Heater's Last Runtime"
// Set it to persist on every UPDATE, not every change
rule 'Heater runtime'
when
Item main_heater_power changed to OFF
then
val previousState = main_heater_power.previousState(true, "influxdb")
logInfo("heater runtime", "Previous state result is {}", previousState);
logInfo("heater runtime", "Previous state timestamp is {}", previousState.timestamp.toString)
val runtime = Duration.between(previousState.timestamp, now)
HeaterRuntime.postUpdate(String.valueOf(runtime.toMillis / 1000) + " s")
logInfo("heater runtime", "Last Runtime was {}", HeaterRuntime.state);
val noon = now.with(LocalTime::NOON)
HeaterDailyRuntime.postUpdate(HeaterRuntime.sumSince(noon, "influxdb"))
logInfo("Heater Runtime", "HeaterDailyRuntime: {}", HeaterDailyRuntime.state)
HeaterWeeklyRuntime.postUpdate(HeaterRuntime.sumSince(noon.with(java.time.temporal.ChronoField.DAY_OF_WEEK, 1), "influxdb"))
logInfo("Heater Runtime", "HeaterWeeklyRuntime: {}", HeaterWeeklyRuntime.state)
HeaterMonthlyRuntime.postUpdate(HeaterRuntime.sumSince(noon.with(java.time.temporal.ChronoField.DAY_OF_MONTH, 1), "influxdb"))
logInfo("Heater Runtime", "HeaterMonthlyRuntime: {}", HeaterMonthlyRuntime.state)
end