I do something similar for my PV system.
I have an item that represents the current power output of my solar PV: Solar_AC_Power. This gets stored into influxdb persistence on every change.
Then I have a rule in jruby:
rule 'Update max solar power' do
changed Solar_AC_Power
run do
midnight = ZonedDateTime.now.truncatedTo(ChronoUnit::DAYS)
midnight = midnight.minusDays(1) if TimeOfDay.now < '4am'
max_since = Solar_AC_Power.maximum_since(midnight, :influxdb)
Solar_AC_Power_Max.update max_since
Solar_AC_Power_MaxTime.update max_since.timestamp
end
end
You could do this without using persistence if you prefer, simply by comparing the current value vs the new value, e.g.
midnight = ZonedDateTime.now.truncatedTo(ChronoUnit::DAYS)
if Solar_AC_Power_MaxTime.is_before(midnight)
# You could save the previous day's max value here if you want to
Solar_AC_Power_Max.update 0 # Reset this for the new day
end
if Solar_AC_Power > Solar_AC_Power_Max
Solar_AC_Power_Max.update Solar_AC_Power
Solar_AC_Power_MaxTime.update Time.now # You could use ZoneDateTime.now if you prefer typing it
end