Migrate JodaTime to Java LocalDateTime in OH3 (withTimeAtStartOfDay)

I‘m using openHABian with a influxDB for persistence of my temperature sensors. In OH2.5 I created a rule to get the min and max values of the day. Attached the rule:

rule "Set daily max and min temperature für Außen"
when
	Item Outside_Garten_Temperature changed or
	Time cron "0 0 0 * * ?" or
	System started 
	
then
		
	var Number Min = -99
	var Number Max = -99
	var String tmp
	var SimpleDateFormat df = new SimpleDateFormat( "HH:mm" ) 
 	
 	if (Outside_Garten_Temperature.state instanceof DecimalType) {
		Min = (Outside_Garten_Temperature.minimumSince(now.withTimeAtStartOfDay, "influxdb").state as DecimalType)
		tmp = (Math::round(Min.floatValue*10.0)/10.0) + " °C (" + df.format(Outside_Garten_Temperature.minimumSince(now.withTimeAtStartOfDay, "influxdb").timestamp) + " Uhr)"
		postUpdate(Outside_Garten_Temperature_Min, tmp)
		
		Max = Outside_Garten_Temperature.maximumSince(now.withTimeAtStartOfDay, "influxdb").state as DecimalType
		df = new SimpleDateFormat( "HH:mm" ) 
		tmp = (Math::round(Max.floatValue*10.0)/10.0) + " °C (" + df.format(Outside_Garten_Temperature.maximumSince(now.withTimeAtStartOfDay, "influxdb").timestamp) + " Uhr)"
		postUpdate(Outside_Garten_Temperature_Max, tmp)
	}

I tried to migrate the command

now.withTimeAtStartOfDay

to the new JavaTime

now.withHour(0).withMinute(0).withSecond(0)

But it does not work… I checked also the forum but I do not find anything that helped me, e.g. https://community.openhab.org/t/oh3-can-i-we-already-ask-questions-wrt-oh3/107091/4 or https://community.openhab.org/t/datetime-conversion-openhab-2-x/54266/86.
I do not understand how to use the new command that the rule also works in OH3

Can maybe someone help me how to migrate the rule to OH3?

Many thanks in advance.

I am using this for now with start of day

now.with(LocalTime.of(0, 0, 0, 0))

1 Like

thanks for your answer. I tried to show the date via the logInfo() command.

logInfo(“Time”, ZonedDateTime.now.with(LocalTime.of(0,0,0,0)))

I also imported the java.time

import java.time

But I get the following error message:

Script execution of rule with UID ‘rule’ failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object) on instance: null in rule

And when you just do this
logInfo(“Time”, now.with(LocalTime.of(0,0,0,0)).toString())

After a lot of hours testing and reading I found the solution and the rule works again :slight_smile:

rule "Set daily max and min temperature für Außen"
when
	Item Outside_Garten_Temperature changed or
	Time cron "0 0 0 * * ?" or
	System started 
	
then
		
	var Number Min_value = -99
	var Number Max_value = -99
	var String tmp
	val time_formatter = java.time.format.DateTimeFormatter.ofPattern("HH:mm")
 	
 	if (Outside_Garten_Temperature.state instanceof DecimalType) {
		
		Min_value = (Outside_Garten_Temperature.minimumSince(now.with(LocalTime.of(0,0,0,0)), "influxdb").state as DecimalType)
		var Min_time = Outside_Garten_Temperature.minimumSince(now.with(LocalTime.of(0,0,0,0)), "influxdb").timestamp
		tmp = (Math::round(Min_value.floatValue*10.0)/10.0).toString() + "°C (" + Min_time.format(time_formatter) + " Uhr)"
		postUpdate(Outside_Garten_Temperature_Min, tmp)
		
		Max_value = (Outside_Garten_Temperature.maximumSince(now.with(LocalTime.of(0,0,0,0)), "influxdb").state as DecimalType)
		var Max_time = Outside_Garten_Temperature.maximumSince(now.with(LocalTime.of(0,0,0,0)), "influxdb").timestamp
		tmp = (Math::round(Max_value.floatValue*10.0)/10.0).toString() + "°C (" + Max_time.format(time_formatter) + " Uhr)"
		postUpdate(Outside_Garten_Temperature_Max, tmp)
	}

end

Hi,
I have a similar problem with my old OH2.5 rule in OH3 which calculates the power consumption.
OH2.5 old rule which worked in OH2.5:

rule "Stromzaehler EZD Bezug Heute Wh" 
when
Item EZD_Wirken_A_plus_Wh received update
then
EZD_Bezug_Heute_Wh.postUpdate (((EZD_Wirken_A_plus_Wh.deltaSince(now.withTimeAtStartOfDay, "influxdb")).floatValue)/1000)
end

The problem is here:
deltaSince(now.withTimeAtStartOfDay

So I tried to replace this with:
deltaSince(now.with(LocalTime.of(0,0,0,0))
OH3 rule which gives an error:

rule "Stromzaehler EZD Bezug Heute Wh" 
when
Item EZD_Wirken_A_plus_Wh received update
then
EZD_Bezug_Heute_Wh.postUpdate (((EZD_Wirken_A_plus_Wh.deltaSince(now.with(LocalTime.of(0,0,0,0)), "influxdb")).floatValue)/1000)
end

Unfortunately it does not work. The log does not show any error but my visual code marks this as an error:

Can you please help me to get this working?

This thread is about OH3, where datetimes are handled differently to OH2.5, so you wouldn’t expect the OH3 methods to work in OH2.5

The OH2.5 method is shown in post #1
now.withTimeAtStartOfDay

Sorry, maybe I was unclear. The OH3 rule does not work for me. OH2.5 is fine.

The complaints from VSCode come from validating your “OH3” code against OH2 stntax.

Have you got imports at the head of your rules file?

At the top of the rule page I have

var Timer stopMotionTimer = null
val EchoVolume = 20

This should be for the Amazon Echo.

Those shouldn’t mess anything up. VSCode is convinced that ‘now’ is a DateTime type. You might try a “fresh” now object
ZonedDateTime.now.with(LocalTime.of(0,0,0,0))
etc.

Same problem again.
openhab.log gives no error but VSCode does:

Not the same, it doesn’t recognize ZonedDateTime.

Have you got two OH instances running, and VSCode is using the wrong LSP connection?

1 Like

Well, I have 2 Raspberries.
The old one is with OH2.5, and the new one with OH3.
I added the directories of both to VSCode to have access to the Config and Logfiles.


Looks like you are on the right track. What do I need to change in VSCode than?

Look at the custom settings for the VSC openHAB extension, look for LSP server.

VSC talks to the target about the syntax it supports via LSP. End of my knowledge.

I think you’ll need to run that as two different VSC projects, there’s all sorts of non-shared stuff like Items.

2 Likes

Got it. I had to change the IP in the settings of VBCode to the new raspberry.
Now a new problem came up.
Old rule:

rule "Mika_is_home"
when
Item cFboxMacOnlineM changed from CLOSED to OPEN
then
var Number hour = now.getHourOfDay()
if (hour >= 7 && hour <= 21 ) {

		// Echo talks

		if (Echo_Volume.state!=UNDEF && Echo_Volume.state!=NULL) {
    		Echo_Volume.sendCommand('25')
			Thread::sleep(2000)  // 2 second wait
    		Echo_TTS.sendCommand("Wilkommen zu Hause Mika.")
			Thread::sleep(2000)  // 2 second wait
		}
		
	hour = null
	}
end

Problem is now.getHourOfDay()

How to replace this in OH3?

Hmm, I suppose you could search this forum to see if anyone encountered that before