maximumSince does not show highest value

OP is not using default persist strategy. Is using default archive strategy, I think that is still running on a minute structured clockwork.

I’d worry that this might be inserting NaN into archives, if no state update happens within the archive period. NaN in rrd4j archives are legitimate but functions like maximumSince do not deal with them nicely.

I copied your rule and adapted your item names to my item naming pattern:

Number          AT_Dachflache_Wind_MaxDay       "Dach Wind Max Tag [%.1f m/s]" <wind> (AT_Dachflache, gWind, gmeter)
DateTime        AT_Dachflache_Wind_MaxDay_Time  "Dach Wind Max Max Zeit" <wind> (AT_Dachflache)

In the log you can see my Maximumsince5 rule reporting log entries (still the wrong ones …) and your rule’s results: Interestingly, your rule also doesn’t keep the maximum value. I chose to trigger the rule on every update of my wind meter whereas you chose 1MinutePeak in your item. This is the only obvious difference I can spot. Here’s the log:

2022-04-04 22:52:00.904 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 14.045833333333336 m/s at 2022-04-04T22:51+02:00[Europe/Berlin]
2022-04-04 22:52:00.895 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_Max5' changed from 13.175666666666663 to 14.045833333333336
2022-04-04 22:52:01.703 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:01.669 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 5.04
2022-04-04 22:52:07.554 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 5.04 to 7.5600000000000005
2022-04-04 22:52:07.608 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:11.719 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:11.695 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 7.5600000000000005 to 13.68
2022-04-04 22:52:14.283 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:14.261 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 13.68 to 9.35
2022-04-04 22:52:15.751 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 22.68 m/s at 10:52:15 PM
2022-04-04 22:52:15.719 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.35 to 22.68
2022-04-04 22:52:15.735 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 17.699125 to 22.68
2022-04-04 22:52:15.753 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-04T12:16:00.000+0200 to 2022-04-04T22:52:15.747+0200
2022-04-04 22:52:16.962 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 26.64 m/s at 10:52:16 PM
2022-04-04 22:52:16.932 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 22.68 to 26.64
2022-04-04 22:52:16.950 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 22.68 to 26.64
2022-04-04 22:52:16.964 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-04T22:52:15.747+0200 to 2022-04-04T22:52:16.958+0200
2022-04-04 22:52:18.545 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 26.64 to 20.16
2022-04-04 22:52:18.574 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 20.16 m/s at 10:52:18 PM
2022-04-04 22:52:18.563 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 26.64 to 20.16
2022-04-04 22:52:18.576 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-04T22:52:16.958+0200 to 2022-04-04T22:52:18.570+0200
2022-04-04 22:52:21.334 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:21.303 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 20.16 to 12.6
2022-04-04 22:52:21.318 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 20.16 to 17.699125
2022-04-04 22:52:21.336 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-04T22:52:18.570+0200 to 2022-04-04T12:16:00.000+0200
2022-04-04 22:52:25.342 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:25.317 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.6 to 9.72
2022-04-04 22:52:27.788 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:27.761 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.72 to 14.4
2022-04-04 22:52:30.686 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:30.655 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 14.4 to 12.24
2022-04-04 22:52:34.422 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 17.699125 m/s at 12:16:00 PM
2022-04-04 22:52:34.397 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.24 to 10.08

I checked for the default persistence setup. It is set to rrd4j. Apparently, the rule still doesn’t work as desired.

Thils is @BrianS 's rule in my setup. Hopefully I did not make a typo …

rule "Calculate today's max wind gust"
when
    Item AT_Dachflache_Wind changed
then 
	// formatter is needed in the conversion of Java time (ZoneDateTime) to DateTime.
	// See the following link for discussion on time conversion. https://community.openhab.org/t/datetime-conversion-openhab-3-x/107197/16. 
    // See in particular post from Bartkummel in Jan21
	
	val formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

	AT_Dachflache_Wind_MaxDay.postUpdate(AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).state)
	
	// get time of max gust. This is returned in Java time (ZoneDateTime variable)
	var time_of_max_gust = AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).timestamp
	
	// convert to string using above formatter anmd then convert to DateTime variable using DateTimeType 
	AT_Dachflache_Wind_MaxDay_Time.postUpdate(DateTimeType.valueOf(time_of_max_gust.format(formatter)))
	
	logInfo("org.openhab","Today's max wind gust is " + AT_Dachflache_Wind_MaxDay.state + " m/s at " + AT_Dachflache_Wind_MaxDay_Time.state.format("%1$tr"))
end

Can you please advise how to specify the persistence service as second argument? I have searched the community but I could only find information on how to do that in a sitemap for charts. How would I implement it in a rule?
Thank you.

Unfortunately I made a mistake with something I told you…I am using the JDBC Persistence MariaDB; not RRD4J. Sorry, I always get the JDBC and RRD4J persistence mixed up. In any case, the strategy is the same…this is my JDBC persistence file:

Strategies {
       default = everyUpdate
}

Items {
       Sensors*    : strategy = everyChange, restoreOnStartup
}

My anemometer is in the Sensors group.

I like the JDBC persistence because it sends the data to a MariaDB database (MySQL compatible) which I am very familiar with.

My question for you is this: what data is actually being captured in the database? Have you compared this to what your rule is selecting/outputting?

Examples in the docs

e.g.
Temperature.averageSince(now.minusMinutes(5), "influxdb")

3 Likes

Thank you for your suggestions.

I already installed MariaDB using this guide: https://community.openhab.org/t/how-to-setup-mysql-persistence-in-a-few-minutes-step-by-step/100965

In addons.cfg I changed the following value:

persistence = mysql,rrd4j,jdbc

My jdbc.persist looks like this:

// persistence strategies have a name and a definition and are referred to in the "Items" section
 
 
Strategies {
   everyMinute	: "0 * * * * ?"
   everyHour    : "0 0 * * * ?"
   every5Minutes : "0 0/5 * 1/1 * ? *"
   every15Minutes : "0 0/15 * 1/1 * ? *"
   every30Minutes : "0 0/30 * 1/1 * ? *"
   everyDay     : "0 0 0 * * ?"
   default = everyChange
}

Items {

   AT_Dachflache* : strategy = every30Minutes // These are all weather station measurements
   AT_Dachflache_Wind_* : strategy = everyChange // This is just for the wind measurements
}

And my wind.rules has my previous rule attempt and the adapted @rossko57 rule. I pointed the persistence to “jdbc” in both rules:

rule "Wind Maximum 5 Minuten"

when
	//Time cron "0 0/5 * * * ?"
	Time cron "0 * * * * ?" //for debug
then
//	var Number Wind_Max5 = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc") as Number
//	AT_Dachflache_Wind_Max5.postUpdate(Wind_Max5) // assumes values are still in rrd4j
	AT_Dachflache_Wind_Max5.postUpdate(AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc")) // assumes values are still in rrd4j
    logInfo ("Wind Maximumsince5","Maximum Wind was: " + AT_Dachflache_Wind_Max5.state + " m/s at " + AT_Dachflache_Wind.maximumSince(now.minusMinutes(5)).timestamp)    
/*
//	logDebug("consumptionAvg", "Cons average {}", ConsumptionAvg.state)
	PowerAverage = SolarPowerTot.averageSince(now.minusMinutes(5)) as Number
	SolarPowerAvg.postUpdate(PowerAverage) // assumes values are still in rrd4j
//	logDebug("SolarAvg", "Solar average {}",SolarPowerAvg.state)
*/
end

rule "Calculate today's max wind gust"
when
    Item AT_Dachflache_Wind changed
then 
	// formatter is needed in the conversion of Java time (ZoneDateTime) to DateTime.
	// See the following link for discussion on time conversion. https://community.openhab.org/t/datetime-conversion-openhab-3-x/107197/16. 
    // See in particular post from Bartkummel in Jan21
	
	val formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

	AT_Dachflache_Wind_MaxDay.postUpdate(AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).state, "jdbc")
	
	// get time of max gust. This is returned in Java time (ZoneDateTime variable)
	var time_of_max_gust = AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).timestamp
	
	// convert to string using above formatter anmd then convert to DateTime variable using DateTimeType 
	AT_Dachflache_Wind_MaxDay_Time.postUpdate(DateTimeType.valueOf(time_of_max_gust.format(formatter)))
	
	logInfo("org.openhab","Today's max wind gust is " + AT_Dachflache_Wind_MaxDay.state + " m/s at " + AT_Dachflache_Wind_MaxDay_Time.state.format("%1$tr"))
end

VS Code reports the following problems for my rule:

Type mismatch: cannot convert from HistoricItem to String
The method or field state is undefined for the type ZonedDateTime

and for @rossko57 's rule:

Invalid number of arguments. The method postUpdate(Item, State) is not applicable for the arguments (NumberItem,State,String)

This is my log:

2022-04-05 12:55:09.544 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 10.08 m/s at 12:40:00 AM
2022-04-05 12:55:09.511 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.84 to 10.08
2022-04-05 12:55:09.524 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 8.740625 to 10.08
2022-04-05 12:55:09.547 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-05T00:40:00.000+0200 to 2022-04-05T12:55:09.539+0200
2022-04-05 12:55:12.486 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 12.6 m/s at 12:55:12 PM
2022-04-05 12:55:12.457 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 10.08 to 12.6
2022-04-05 12:55:12.470 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 10.08 to 12.6
2022-04-05 12:55:12.487 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-05T12:55:09.539+0200 to 2022-04-05T12:55:12.479+0200
2022-04-05 12:55:16.630 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 9.0 m/s at 12:55:16 PM
2022-04-05 12:55:16.604 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.6 to 9.0
2022-04-05 12:55:16.619 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 12.6 to 9.0
2022-04-05 12:55:16.631 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-05T12:55:12.479+0200 to 2022-04-05T12:55:16.625+0200
2022-04-05 12:55:25.055 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 8.740625 m/s at 12:40:00 AM
2022-04-05 12:55:25.007 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.0 to 6.84
2022-04-05 12:55:25.021 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 9.0 to 8.740625
2022-04-05 12:55:25.046 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-05T12:55:16.625+0200 to 2022-04-05T00:40:00.000+0200
2022-04-05 12:55:26.373 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 8.740625 m/s at 12:40:00 AM
2022-04-05 12:55:26.349 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.84 to 0.0
2022-04-05 12:56:01.004 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'wind-2' failed: 'state' is not a member of 'java.time.ZonedDateTime'; line 29, column 69, length 25 in wind
2022-04-05 12:56:05.737 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 8.740625 m/s at 12:40:00 AM
2022-04-05 12:56:05.701 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 4.32

The rules seem to work somehow but not as desired. Rosko57’s Max Windgust rule logs an update to a high gust but it then falls back to a value from 12:40 a.m. And my rule seems to be stuck at the problems from the beginning. Did I do something wrong with the persistence definition?
I also tried to work with a variable in the rule instead of the straigh postUpdate:

	var Number Wind_Max5 = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc") as Number
	AT_Dachflache_Wind_Max5.postUpdate(Wind_Max5)

Unfortunately, this doesn’t fix the problem either. Any more ideas? Sorry, I don’t want to drive anyone mad in case this appears like some newbie nonsense to you.

This will not do what you expect.
postUpdate() is asynchronous; the instruction is sent on to openHAB event bus and passed to interested bindings, rules, UI etc, and also updating the actual Item state. Takes a few milliseconds.
Meantime, your rule does not stop and wait. If you retrieve Item state in the following lines, you will likely get the old state still.
Items are not simple variables.

That’s fine, you already know what you sent to postUpdate - use that directly.

var x = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc")
AT_Dachflache_Wind_Max5.postUpdate(x)
logInfo("test", "posted " + x.toString)

It’s going to help your analysis not to try to do everything on one line anyway.

The next pitfall is what the persistence method returns. Clue -

See

maximumSince words -

Gets the maximum value of the State of a persisted Item since a certain point in time (returns HistoricItem)

What you get back is an object - the max record AND a timestamp. That code snippet will fail because x is a complex object. You’d be interested in x.state

1 Like

Thank you @rossko57

I tried to pick up your advise like this:

rule "Wind Max 5 Test"
	when
    Item AT_Dachflache_Wind changed
	then
	var wmax = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc") as Number
	AT_Dachflache_Wind_Max5.postUpdate(wmax)
	logInfo ("Wind test", "Wind posted " + wmax.toString)
end

I added “as Number” to the var line because otherwise VS Code said:

Type mismatch: cannot convert from HistoricItem to String

However, there is no log output at all.
I guess this is the corresponding error:

2022-04-05 15:36:59.876 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'wind-2' failed: Could not cast org.openhab.core.persistence.extensions.PersistenceExtensions$1@17c96a1 to java.lang.Number; line 25, column 13, length 70 in wind

Yes, I went on to add about how to handle what maximumSince() returns, which is not what you think either. You’d be interested in wmax.state

1 Like

Thank you @rossko57. But for some reasone I can’t find the reason why it’s not the real maximum wind value which is stored. Clueless …

Tell us more. What is “it” i.e. what does your rule look like now. What result do you see, what result do you expect?

1 Like

Thank you @rossko57, unfortunately storing the correct values still doesn’t seem to work. To illustrate what I mean here is a log part which shows entries for your “Maximum wind gust of the day” rule:

2022-04-06 14:27:11.493 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 8.28 to 12.24
2022-04-06 14:27:11.530 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 11.933541666666667 to 12.24
2022-04-06 14:27:11.541 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-06T14:08:00.000+0200 to 2022-04-06T14:27:11.510+0200
2022-04-06 14:27:15.210 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 11.933541666666667 m/s at 02:08:00 PM
2022-04-06 14:27:15.170 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.24 to 10.44
2022-04-06 14:27:15.191 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay' changed from 12.24 to 11.933541666666667
2022-04-06 14:27:15.211 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_MaxDay_Time' changed from 2022-04-06T14:27:11.510+0200 to 2022-04-06T14:08:00.000+0200
2022-04-06 14:27:20.666 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 11.933541666666667 m/s at 02:08:00 PM

As you can see the max. value jumps to 12.24 m/s at 14:27:11. At 14:27:15, it goes down to 11.93 m/s. That doesn’t make sense because the previously recorded value was higher. So there still seems to be an issue with persistence. Tbh, I can’t figure out how to look into the table of my MariaDB file. Maybe the values don’t get stored correctly in the database.

This is your rule for the and mine for the previous five minutes:

rule "Wind Maximum 5 Minuten"

when
	//Time cron "0 0/5 * * * ?"
	Time cron "0 * * * * ?" //for debug
then
	var Number Wind_Max5 = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc") as Number
	AT_Dachflache_Wind_Max5.postUpdate(Wind_Max5)
    logInfo ("Wind Maximumsince5","Maximum Wind was: " + AT_Dachflache_Wind_Max5.state + " m/s at " + AT_Dachflache_Wind.maximumSince(now.minusMinutes(5)).timestamp)    
end

rule "Calculate today's max wind gust"
when
    Item AT_Dachflache_Wind changed
then 
	// formatter is needed in the conversion of Java time (ZoneDateTime) to DateTime.
	// See the following link for discussion on time conversion. https://community.openhab.org/t/datetime-conversion-openhab-3-x/107197/16. 
    // See in particular post from Bartkummel in Jan21
	
	val formatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ")

	AT_Dachflache_Wind_MaxDay.postUpdate(AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).state, "jdbc")
	
	// get time of max gust. This is returned in Java time (ZoneDateTime variable)
	var time_of_max_gust = AT_Dachflache_Wind.maximumSince(now.with(LocalTime.of(0,0,0,0))).timestamp
	
	// convert to string using above formatter anmd then convert to DateTime variable using DateTimeType 
	AT_Dachflache_Wind_MaxDay_Time.postUpdate(DateTimeType.valueOf(time_of_max_gust.format(formatter)))
	
	logInfo("org.openhab","Today's max wind gust is " + AT_Dachflache_Wind_MaxDay.state + " m/s at " + AT_Dachflache_Wind_MaxDay_Time.state.format("%1$tr"))
end

My intention is that the item AT_Dachflache_Wind_Max5 shows the highest wind speed of the past 5 minutes. And AT_Dachflache_Wind_MaxDay shows the highest recorded wind speed of the current day.

This is the log error I receive for my 5 minute rule:

2022-04-06 14:37:00.247 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'wind-2' failed: Could not cast org.openhab.core.persistence.extensions.PersistenceExtensions$1@165e572 to java.lang.Number; line 28, column 25, length 70 in wind

I don’t have a “Maximum wind gust of the day” rule, you do. We can try to help you with it.

Yes. As discussed, maximumSince() returns a complex object - which cannot be cast as a Number.
You’d want to look at its state component.

Seeing as you are investigating weirdness, you might find it useful to get the timestamp component as well - which record did it retrieve for you?

Maybe

var Wind_Max5 = AT_Dachflache_Wind.maximumSince(now.minusMinutes(5), "jdbc")
// this is NOT a number, you cannot beat it into a number shape
// you have to look at its parts
logInfo ("Wind Maximumsince5","Maximum Wind was: " + Wind_Max5.state.toString)    
logInfo ("Wind Maximumsince5","Maximum Wind occurred at " + Wind_Max5.getTimestamp.toString)    

It has not sunk in about postUpdate() being asynchronous.
Fetching the state of this Item will get the “old” state, because the postUpdate() in the preceding line has almost certainly not completed yet.

1 Like

Thank you. I will try to fetch the data you suggested.
And I mixed things up with the maximum wind gust rule. It wasn’t provided by you but by @BrianS in an earlier posting. Sorry about that.

When looking into problems with this kind of thing, break it up so you can see the moving parts. Something is not what you expected, find out what. Is that time what you think? Is that whole gubbins something suitable to postUpdate to an Item?

var tt = now.with(LocalTime.of(0,0,0,0)
logInfo("diag", "Using datetime " + tt.toString)
var xx = AT_Dachflache_Wind.maximumSince(tt, "jdbc")
logInfo("diag", "got value " + xx.state.toString)
logInfo("diag", "from " + xx.timestamp.toString)
AT_Dachflache_Wind_MaxDay.postUpdate(xx.state)

I tried this suggestion and here are the log results:

ince5] - Maximum Wind was: 10.44
2022-04-06 18:29:03.086 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind occurred at 2022-04-06T18:29:03.085624+02:00[Europe/Berlin]
2022-04-06 18:30:59.611 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 6.12
2022-04-06 18:30:59.620 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind occurred at 2022-04-06T18:30:59.619834+02:00[Europe/Berlin]
2022-04-06 18:32:00.031 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 8.28
2022-04-06 18:32:00.035 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T18:32:00.033958+02:00[Europe/Berlin]
2022-04-06 18:33:00.029 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 6.12
2022-04-06 18:33:00.033 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T18:33:00.032004+02:00[Europe/Berlin]

Apparently, not the highest but the last recorded value is used by the Max5 variable.

How do you know, what recorded data is available for the preceding 5 minutes? Any at all?

This is a bit of a handicap, I’ve no idea how you use that either.
Let’s get openHAB to have a rough look.

var zz = AT_Dachflache_Wind.historicState(now.minusMinutes(5), "jdbc")
// this is NOT a number either
logInfo ("Wind Maximumsince5","5 mins ago was: " + zz.state.toString)    
logInfo ("Wind Maximumsince5","record from  " + zz.getTimestamp.toString)    

It is unlikely there is any record at exactly five minutes ago to the millisecond - what historicState() should do is search backward for the last valid record before that (on the assumption it remained valid until)

You can query the DB through the API Explorer. The persistence: GET /persistence/items/{itemname} endpoint will let you create a query for all entries between two supplied date times (or a date time to now).

1 Like

Thank you for pointing to the API Explorer, @rlkoshak. I can see that values have been persisted for the item. However because I guess time values are Epoch, I cannot tell when the data was recorded.

Interesting behaviour for the historicState. It keeps showing the data for a value that was recorded at 9 p.m. No changes over time:

2022-04-06 21:11:00.673 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 0.0
2022-04-06 21:11:00.677 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T21:11:00.676203+02:00[Europe/Berlin]
2022-04-06 21:11:00.686 [INFO ] [core.model.script.Wind Maximumsince5] - Wind 5 mins ago was: 0.0
2022-04-06 21:11:00.690 [INFO ] [core.model.script.Wind Maximumsince5] - Wind record from  2022-04-06T21:00:00.277+02:00[Europe/Berlin]
2022-04-06 21:12:00.669 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 0.0
2022-04-06 21:12:00.672 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T21:12:00.671677+02:00[Europe/Berlin]
2022-04-06 21:12:00.680 [INFO ] [core.model.script.Wind Maximumsince5] - Wind 5 mins ago was: 0.0
2022-04-06 21:12:00.683 [INFO ] [core.model.script.Wind Maximumsince5] - Wind record from  2022-04-06T21:00:00.277+02:00[Europe/Berlin]
2022-04-06 21:12:54.143 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 11.933541666666667 m/s at 02:08:00 PM
2022-04-06 21:12:54.029 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 7.92
2022-04-06 21:13:00.124 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 11.933541666666667 m/s at 02:08:00 PM
2022-04-06 21:13:00.098 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 7.92 to 6.12
2022-04-06 21:13:00.667 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 6.12
2022-04-06 21:13:00.670 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T21:13:00.669890+02:00[Europe/Berlin]
2022-04-06 21:13:00.679 [INFO ] [core.model.script.Wind Maximumsince5] - Wind 5 mins ago was: 0.0
2022-04-06 21:13:00.681 [INFO ] [core.model.script.Wind Maximumsince5] - Wind record from  2022-04-06T21:00:00.277+02:00[Europe/Berlin]
2022-04-06 21:13:05.266 [INFO ] [penhab.core.model.script.org.openhab] - Today's max wind gust is 11.933541666666667 m/s at 02:08:00 PM
2022-04-06 21:13:05.245 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.12 to 0.0
2022-04-06 21:14:00.668 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 0.0
2022-04-06 21:14:00.671 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T21:14:00.670668+02:00[Europe/Berlin]
2022-04-06 21:14:00.679 [INFO ] [core.model.script.Wind Maximumsince5] - Wind 5 mins ago was: 0.0
2022-04-06 21:14:00.681 [INFO ] [core.model.script.Wind Maximumsince5] - Wind record from  2022-04-06T21:00:00.277+02:00[Europe/Berlin]
2022-04-06 21:15:00.684 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 was: 0.0
2022-04-06 21:15:00.690 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind5 occurred at 2022-04-06T21:15:00.688795+02:00[Europe/Berlin]
2022-04-06 21:15:00.703 [INFO ] [core.model.script.Wind Maximumsince5] - Wind 5 mins ago was: 0.0
2022-04-06 21:15:00.707 [INFO ] [core.model.script.Wind Maximumsince5] - Wind record from  2022-04-06T21:00:00.277+02:00[Europe/Berlin]