How to persist daily energy consumption

My (Growatt) solar inverter has channels for cumulated energy consumed and produced per day. The value resets to zero at midnight and increases throughout the day. I want to persist the full daily consumption/production values i.e. the maximum number before it resets to zero. Due to slight time differences between the OH computer and the inverter, the time of the cumulated value reset may be slightly before or after midnight as seen by the OH computer. => Any thoughts how to do this?

In JS Scripting there exists a function maximumBetween for the ItemPersistence object. So you could trigger a rule e.g. five minutes after midnight and get the maximum value of the last 10 minutes. This should be the maximum of the previous day. You may assign this value to a separate item and persist this value afterwards.

If it resets to zero you could also use that as trigger (“Item changed to 0”), and you could get the previous value from the event object. This would of course only work if you are able to always get zero as value and not a new small contribution for the new day.

Yes I also thought about that but thought that there might be some edge cases where this might fail. Since the values should be very low at the beginning of the day you could also increase the maximumSince window to make sure that you really get the value you want (e.g. one hour after midnight and then get maximum of the last two hours)

You could use default persistence (persists on every change) and retrieve item.maximumSince(now.minusDays(1)).

You can use a rule condition to only run the rule when the new state is less than the previous state. That would let you use this overall approach without relying on the first state of the day being exactly 0.

Using persistence is probably the way I would implement this though.

Agreed, this is more reliable and will also handle cases like openHAB being restarted just before the counter is reset.

Do we have direct access to previousState and newState in a rule condition or are you speaking about a script as a rule condition?

A script condition gets the event Object same as the script actions do. Everything available in the action will be in the condition.

A regular old UI condition does not have access though and if it did it wouldn’t have a way to use it.

In JS scripting such a script condition would look something like (assuming UoM):

Quantity(event.itemState).lessThan(event.oldItemState);
2 Likes

thanks for the clarification

Thanks guys for all the suggestions.

My purpose is to get a rolling average of the full day energy consumption over the last 7 days (say). (Where each day’s full day energy consumption is the maximum number for that day before it rolls over to zero). And compare that average versus the coming day’s solar energy forecast (from solar forecast binding). And if the solar forecast is less than the past average consumption, the binding commands the inverter to top up the battery during the night time low tariff window for the difference between past usage and forecast solar production.

Does the inverter also has a total production meter? If you persist that you will be able to get the production over any period from hours to months just by deducting the end value from the beginning value.

@barneyd yes it does. That is a brilliant insight. Many thanks. I was blindly following a complex ‘solution’ when a really simple one was looking at me all the time.