Import org.joda.time.* does not work

hi,

somehow I can’t make it work to use joda time in my rule. Even this simple rule does not work at all:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.joda.time.format.*
import java.lang.String

rule "Check FBH in heating period"
when
Time cron "0/10 * * * * ?"
then
var DateTime dt = new DateTime()
var String monthName = dt.monthOfYear().getAsText()
logInfo( “FILE”, monthName)

end

In the log I get:
2016-03-08 08:37:51.412 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Check FBH in heating period
java.lang.RuntimeException: The name ‘.getAsText()’ cannot be resolved to an item or type.

Any idea what is wrong?

Looking at the Joda DateTime’s JavaDoc, monthOfYear returns an int and int has not getAsText method.

The docs for Joda indicate what you may mean to do is:

var String monthName = dt.month.getAsText()

Strange I got this example from a joda tutorial. However edited with your line…
var DateTime dt = new DateTime()
var String monthName = dt.month.getAsText()
logInfo( “FILE”, monthName)

… but it does not work either. Error:
2016-03-09 08:23:20.268 [ERROR] [.o.m.r.i.engine.ExecuteRuleJob] - Error during the execution of rule Check FBH in heating period
java.lang.RuntimeException: The name ‘.month’ cannot be resolved to an item or type.

SInce also this line: var LocalTime now = LocalTime.now()
gives an error I belive there is something wrong with the import.
I’m on rasbian wheezy with openhab 1.8

Not know what you were looking at I can’t comment. But the link above comes straight from Joda and says:

DateTime dt = new DateTime(); // current time
int month = dt.getMonth(); // gets the current month
int month = dt.month().get(); // alternative way to get value
String monthStr = dt.month().getAsText(); // gets the month name

Depends on the error but I doubt it is a problem with the import as there would be no DateTime at all if the import is wrong.

I highly recommend using Designer which will tell you if you have syntax errors or lacking imports.

It would seem that the month method is not exposed in the Rules DSL so we can’t get at the month name using getAsText. Perhaps it is using an older version of Joda.

You can try to get the month name as follows:

var String monthName = String::format("%1$tB", dt)

I think that String::format works with Joda time. If not just use Java’s built in Date object.

I have this now in my rules file:

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
import org.joda.time.format.*
import java.lang.String

rule "test"
	when
		Time cron "0/10 * * * * ?"
	then
		
	var DateTime dt = new DateTime() // current time
	var int month = dt.getMonth() // gets the current month
	var String monthStr = dt.month().getAsText() // gets the month name
	logInfo( "FILE", monthStr)
end

Designer also tells me:

Couldn’t resolve reference to JvmIdentifiableElement ‘getMonth’.
Couldn’t resolve reference to JvmIdentifiableElement ‘month’.
Couldn’t resolve reference to JvmIdentifiableElement ‘getAsText’.

It is not about getting the name of the month, it is more about in general getting the Joda things to work in my rules. Since it is easier and more elegant than the Java build in Date object.

You can force the issue by using:

var org.joda.time.DateTime dt = ...

However, I don’t know what version of Joda is in OH 1, whether the OH 1 Rules engine changes things around, etc.

One thought, if dt.month is static you would need to access it using the “::” operator (e.g. dt::month).

Beyond that I don’t know what else to offer for why the .month method is not showing up.

After some time of frustration I wanted to start again on this antried without joda. But it also does not work well.
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import java.text.SimpleDateFormat
import java.util.Calendar
import java.lang.String

rule "test"
	when
		Time cron "0/10 * * * * ?"
	then
		
	
	var Calendar cal = Calendar.getInstance()
	var SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss")
	logInfo( "FILE", "Time: " + sdf.format(cal.getTime()))
end

Designer tells Multiple markers at this line

  • Couldn’t resolve reference to JvmIdentifiableElement ‘getInstance’.
  • Couldn’t resolve reference to JvmIdentifiableElement ‘Calendar’.

And also in the log:
java.lang.RuntimeException: The name ‘Calendar’ cannot be resolved to an item or type.

I don’t get it. The most simple time thing just does not work. Any hint?

I don’t know whether or not this is your problem, but I’ve had problems like yours because of duplicate class names. So when there is a Calendar class in the OH API it might use that one in your program, not the Java one. The only solution I can see in that case (as written not sure if this really is your problem but it sounds like) is to use the fully-qualified names as @rlkoshak mentioned.

Regards
Dieter

Hi,

sorry for not getting back. I had to restore my openhab machine.
After a bit more struggling with this I made it work :grinning:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import java.lang.String
import java.lang.Integer
import org.joda.time.*

rule "FBH in Heating"
	when
		Time cron "0/60 * * * * ?"
	then
	
	var DateTime timer1F = parse(now.getYear+"-"+now.getMonthOfYear+"-"+now.getDayOfMonth+"T"+Heating_timer1F.state
	var DateTime timer1T =  parse(now.getYear+"-"+now.getMonthOfYear+"-"+now.getDayOfMonth+"T"+Heating_timer1T.state
   "+now.getDayOfMonth+"T"+now.getHourOfDay+":"+now.getMinuteOfHour+":"+now.getSecondOfMinute)
    	if((timer1F.beforeNow && timer1T.afterNow) && Heating_Mode.state != "off"){		
    		if(HeatingFBH_InHeatTime.state==OFF){ postUpdate(HeatingFBH_InHeatTime,ON)
    			logInfo( "FILE", "Entered heating time.")	
    		}
    	} else {
    		if(HeatingFBH_InHeatTime.state==ON){ postUpdate(HeatingFBH_InHeatTime,OFF)
    			logInfo( "FILE", "Left heating time")	
    		}
    	}
    end

The turnaround for me was to get familiar with now.* and the use of parse().
I hope this might help others.

Best regards
-Biobier

1 Like

Did anyone get anywhere with this issue?

I have tried different ways of getting DateTime to register (with and without fully-qualified names) and in all of them the designer program tells me “Couldn’t resolve reference to JvmType ‘DateTime’”

I feel as if i’m going round in circles.

Using OH1 with designer 1.8.3, Java updated to latest version.

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.joda.time.*

rule "At Dusk Turn Lights On"
//Compare now to sunset time provided by Lightwave Link (lw_wifilink_dusk)
//If after dusk turn all lights to 50%
when
	Time cron "0/60 * * * * ?"
then
	var DateTime sunset = new DateTime((lw_wifilink_dusk.state as DateTimeType).calendar.timeInMillis)	
	
	if (sunset.isAfterNow())
	{
		logInfo("Test", "Sunset")
	}
	else
	{
		logInfo("Test", "Daytime")
	}
end

Any help would be greatly appreciated

Anthony

Does it run? I just added the following line to a rule and Designer had no problem with it.

var org.joda.time.DateTime sunset = new org.joda.time.DateTime(new DateTimeType().calendar.timeInMillis)

Here are a few more working examples from my rules:

val sunrise = new DateTime((Sunrise_Time.state as DateTimeType).calendar.timeInMillis)
val twilight = new DateTime((Twilight_Time.state as DateTimeType).calendar.timeInMillis)
val evening = new DateTime((Sunset_Time.state as DateTimeType).calendar.timeInMillis)

Thanks Rich

That seems to work. Although the designer still highlights them as unresolved.

Anthony

I have just reinstalled Windows on my desktop due to a hard disk crash, and now my rules file that I have been using for the last 9 months is displaying this error.

I have tried restarting designer and rebooting the pc, to make sure all paths were set, but to no effect.

Even specifying the class produces an error:

I’m looking for other suggestions to get rid of this.

Have you tried to import org.joda.time.DateTime

Yes, see in the image. Interestingly, the import doesn’t report an error, but then trying to use a DateTime, even after it has been imported specifically, does.

Did you read the rest of the thread? Designer currently has an unresolved phantom problem with ‘now’. What actually is the mystery error you see?

I’m seeing the problem with ‘now’ as well, but I think they are in the same package. Something in my new installation is causing this, but I have no idea how this scripting language resolves classes.

As @rossko57 said, there is a problem with the current builds of Designer that causes them to not recognize now and other joda classes. This is just a Designer issue. Does the rule run? If not then what is the error reported in OH?

The rules have been running fine for months, so its a designer issue.

If its a bug that’s been introduced in designer, is there a way to get a previous version that worked?

Edit:
I have just found and downloaded version 1.8.2, from bintray, which I think is the previous version (I can’t find any version info in the designer files). It is exhibiting exactly the same symptoms, so I’m not sure this is a designer bug or an installation problem.

I can’t find any older versions of designer to test.

Try looking on cloudbees. I think the error was introduced after 1.8 or 1.8.1.