maximumSince does not show highest value

Hi everyone,

I am trying to store the highes measurements for my wind sensor for the past 5 minutes and past hour. I have searched the community and thought I found a working solution. However, the item does not save the highest value in the given period but a lower one.

Here is the log for about two minutes:

2022-04-02 17:35:00.288 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_AvgBft' changed from 5.939923392521746 to 4.636669532899513
2022-04-02 17:35:33.915 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 6.12
2022-04-02 17:35:37.194 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.12 to 11.16
2022-04-02 17:35:41.732 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 11.16 to 8.64
2022-04-02 17:35:43.837 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 8.64 to 16.2
2022-04-02 17:35:47.084 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 16.2 to 11.16
2022-04-02 17:35:54.810 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 11.16 to 9.35
2022-04-02 17:35:57.558 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.35 to 13.68
2022-04-02 17:36:00.446 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 13.68 m/s at 2022-04-02T17:36:00.444252+02:00[Europe/Berlin]
2022-04-02 17:36:00.293 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_Max5' changed from 6.743333333333333 to 13.68
2022-04-02 17:36:03.523 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 13.68 to 10.08
2022-04-02 17:36:10.624 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 10.08 to 6.12
2022-04-02 17:36:13.643 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.12 to 12.6
2022-04-02 17:36:18.727 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.6 to 15.84
2022-04-02 17:36:25.625 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 15.84 to 8.28
2022-04-02 17:36:33.045 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 8.28 to 0.0
2022-04-02 17:36:45.780 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'localCurrentWindSpeed' changed from 8.064 km/h to 12.888 km/h
2022-04-02 17:36:49.411 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 6.48
2022-04-02 17:36:52.430 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.48 to 14.040000000000001
2022-04-02 17:36:58.458 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 14.040000000000001 to 10.44
2022-04-02 17:37:00.314 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 10.44 m/s at 2022-04-02T17:37:00.313048+02:00[Europe/Berlin]
2022-04-02 17:37:00.288 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_Max5' changed from 13.68 to 10.44
2022-04-02 17:37:07.243 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 10.44 to 5.4
2022-04-02 17:37:09.232 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 5.4 to 0.0
2022-04-02 17:37:21.937 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 6.12
2022-04-02 17:37:23.935 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.12 to 0.0
2022-04-02 17:37:41.805 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 4.67
2022-04-02 17:37:44.547 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 4.67 to 14.76
2022-04-02 17:37:50.312 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 14.76 to 11.16
2022-04-02 17:37:56.318 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 11.16 to 7.2
2022-04-02 17:37:59.913 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 7.2 to 10.8
2022-04-02 17:38:00.339 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 10.8 m/s at 2022-04-02T17:38:00.338610+02:00[Europe/Berlin]
2022-04-02 17:38:00.290 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind_Max5' changed from 10.44 to 10.8
2022-04-02 17:38:07.961 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 10.8 to 9.35
2022-04-02 17:38:19.228 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.35 to 6.12
2022-04-02 17:38:23.283 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 6.12 to 0.0
2022-04-02 17:38:40.085 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 0.0 to 7.2
2022-04-02 17:38:45.035 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 7.2 to 7.92
2022-04-02 17:38:48.038 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 7.92 to 11.88
2022-04-02 17:38:52.232 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 11.88 to 9.35
2022-04-02 17:38:55.278 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 9.35 to 12.24
2022-04-02 17:38:59.147 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'AT_Dachflache_Wind' changed from 12.24 to 9.72
2022-04-02 17:39:00.322 [INFO ] [core.model.script.Wind Maximumsince5] - Maximum Wind was: 9.72 m/s at 2022-04-02T17:39:00.321116+02:00[Europe/Berlin]

My items are

Number          AT_Dachflache_Wind              "Dach Wind [%.1f m/s]"              <wind>        (AT_Dachflache, gmeter, gWind)                        ["Wind"]            {channel="knx:device:bridge:generic:Wetterstation_Wind"}
Number          AT_Dachflache_Wind_Max5          "Dach Wind Max 5 Min [%.1f m/s]"          <wind>        (gWind)

and my rule is

rule "Wind Maximum 5 Minuten"

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

I wonder if the .state on the item is causing the problem. Should I try something else?
For the last hour I did the same rule but with a Wind_Max60 item and now.minusMinutes(60). I use rrd4j for persistence.

My rrd4j.persist is:

   AT_Dachflache_Wind : strategy = everyChange, restoreOnStartup
   AT_Dachflache_Wind_* : strategy = everyChange

Any ideas how to fix this? Best, Max

It’s probably not doing what you think; rrd4j is not a simple datalogger. rrd4j has strategies for archiving older data that involve averaging. There’s a clue in this value -

which does not match any actual reading - it’s an average.

I’m not sure exactly what archive method is applied by default; I think it averages whatever readings have arrived during a minute, before writing away the average for that minute.
There are other archiving strategies available.

Assuming you are on openHAB 3.x and you are using the default setup of rrd4j your items are either persisted using the default_numeric datasource ( numeric item) or the default_quantifiable datasource ( numeric item WITH dimension). In both cases the state of the item is persisted every 10 seconds for the last hour. For this timeframe NO consolidation should be used.
After 60 minutes dataconsolidation starts, the numeric database uses the LAST strategy, quantifiable the AVERAGE strategy.
In order to check where th reported value is coming from you would need to set the rrd4j persistence bundle into log: level DEBUG.
Your logged values for the maximum are always the last reported state of the item, which doesn’t make sense to me. :thinking:

Reading some old threads I would guess your persisted item is a string, that would explain the behaviour. From the docs:

NOTE: rrd4j is for storing numerical data only.

[Edit] Disregard the comment about string item as it was a missunderstanding from my side.
It might be a problem with the default persistencd service of your system.
Please change the rule to specify the used persistence service (rrd4j as second argument in the maximumSince call).

I obtain simlar data from my anemometer and I have also setup routines in OH to determine max and mins using persistance data. My persistence strategy is the same as yours.

Looking at your data it appears the maximumSince function is not finding the maxuimum value during the period but rather the wind value at the instant the function is called by the cron. Here are a few ideas:

  1. Could be a java time issue. See this post… Migrate JodaTime to Java LocalDateTime in OH3 (withTimeAtStartOfDay)

  2. Also, In my rule, it executes when the wind speed changes, not at a regular interval as per a cron

Here is my working code where it calculates the maximum wind gust for the day. It may help in your situation:

import java.time.ZonedDateTime

rule "Calculate today's max wind gust"
when
    Item Anemometer_WindGust1MinutePeak 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")

	Maximum_wind_gust_today.postUpdate(Anemometer_WindGust1MinutePeak.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 = Anemometer_WindGust1MinutePeak.maximumSince(now.with(LocalTime.of(0,0,0,0))).timestamp
	
	// convert to string using above formatter anmd then convert to DateTime variable using DateTimeType 
	Timeof_Maximum_wind_gust_today.postUpdate(DateTimeType.valueOf(time_of_max_gust.format(formatter)))
	
	//logInfo("org.openhab","Today's max wind gust is " + Maximum_wind_gust_today.state + " km/hr at " + Timeof_Maximum_wind_gust_today.state.format("%1$tr"))
end

Note: To run this code, you will need to create 2 items; Maximum_wind_gust_today (number) and Timeof_Maximum_wind_gust_today (DateTime).

Hope this helps.

@rossko57 @opus and @BrianS : Thank you all so much for your thoughts and ideas.

You are all right: It looks like my rule always picks the value at the time of the cron execution. I will try to change my rule to Brian’s way of handling the data gathering. I will keep you posted.
Best, Max

Do the rrd4j persitence not need a standard strategy of everyMinute?
Maybe this conflicts with the maximumSince operation?

This was true in openHAB 2 and before, in openHAB 3 the default datasources take care of that by sample intervals of 5 or 10 seconds. For custom setups this note was added:

**IMPORTANT:** When creating a custom datasource in the `rrd4j.cfg` file the used [sample interval](https://www.openhab.org/addons/persistence/rrd4j/#sampleinterval-sample-interval) should be 20 seconds or less in order to keep the granularity. The selection of the used strategy has no effect on the granularity.

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