Store MAX value of an item and when it happened?

Hi!

I’ve been trying to come up with a rule that allows me to store the MAX value (in this case for each of the electricity phases in the house) of an item, and when it happened.

Persistence is rrd4j, I know the timestamp of each record must be there, but I haven’t been able to make it work.

Should I use rr4dj persistence data, or should I try to setup a rule to compare each new value to the previous and store if bigger, along with a timestamp?

Any ideas welcome! Thanks!

The simplest way wold probably be to use two virtual items for each item that you need to store the max value for. One virtual item with the current max value and another virtual item with the date that the max was set. Then it is just a simple rule to check whether an update is larger than the one stored. If so, update the stored max and the date.
Persisting the items doesn’t change the situation much. That just makes sure that everything stays working after a reboot.
You might want to reset the max from time to time.

1 Like

You can store all values and use the maxSince method on the Item in a Rule to extract the max value.

Thanks for info.
I thought of maxSince, but how do I get the corresponding timestamp of when that particular value happened?

Later tonight I’ll try Morten’s suggestion.

Thanks again!

maxSince returns a HistoricState Object which has a timestamp method that gives you the time it was saved.

2 Likes

When using rrd4j as the persistence service, you should be aware of the specifics of that service! When using the default rrd4j.cfg (which is the same as using no such file) request over a period of more then 8 houres will yield only derived average values!

1 Like

Last night I made a couple of items, and added a few lines to a rule which already had current power values for each phase.
It’s not very sophisticated, but I think it is working as it should.

Items are setup like this (x3)

Number		maxF1			"Máximo Fase 1 [%d W]"	<energy>	(gHistorico)
String		dateMaxF1		"Data Máx. F1 [%s]"			<calendar>		(Tudo)

And the rule (simplified version here just for 1 phase, mine actually does more math with solar values and such):

rule "Balanco Consumo Prod Solar"
when
	Item Fase1 changed 
then
	var F1= (Fase1.state as DecimalType).doubleValue

	if(maxF1.state==Uninitialized) maxF1.postUpdate(0)
	
	var maxf1 = (maxF1.state as DecimalType).doubleValue
	
	if(F1 > maxf1)
	{
		maxF1.postUpdate(F1)
		Timestamp = String::format( "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS", new Date() )
		dateMaxF1.postUpdate(Timestamp)
	}
	
end

Any suggestions ti improve this are welcome, but for now I think it does what I need!
Thanks!