Unable to use "now" in Rules

I have problems using “now”. I am using 1.8.3 installed on a Ubuntu 16.04 LTS VM (downloaded the ZIP, not with apt-get). I’m a beginner, but everything else I have tried in openHab is working. I am aware of the Designer problems with “now”, but in my case the rules also don’t work. Adding all kinds of imports didn’t help.

Error message in designer:

Couldn't resolve reference to JvmIdentifiableElement 'now'. Couldn't resolve reference to JvmIdentifiableElement 'plusMinutes'

Error in openhab.log:

2016-09-16 14:12:49.052 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Initialize Lights and Shutters': Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DateTimeType

So I made a very simple rule, see below. Then the error changed to:

2016-09-16 15:31:47.490 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Initialize Lights and Shutters': cannot invoke method public java.lang.String org.joda.time.base.AbstractDateTime.toString() on null

What’s the problem?

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer

import org.joda.time.*
import java.util.date

/* On startup check time for shutters and light */
rule "Test rule"
	when
		System started
	then
		val DateTime soon = now.plusMinutes(1)
		logInfo("Testing", soon.toString)
end

Your errors are unrelated.

The error in Designer can be ignored. Something got broken in the latest release of the Designer such that it no longer recognizes “now”. But openHAB still does to its a false error in Designer.

The second error listed means you are taking the state of an Item which does not yet have a value (i.e. UnDefType) and trying to cast it to a DateTimeType. Your Item is uninitialized. You can avoid this by first checking that your Item isn’t undefined (i.e. MyItem.state != UNDEFINED) before trying to cast it.

The third error is not coming from your “Test rule” at all. It is coming from a Rule named “Initialize Lights and Shutters”. Assuming this is just a copy and paste error, try adding in a brief sleep in your rule before setting soon and logging it. It might be the case where your rule is running before now has been initialized. The error clearly indicates that it recognizes now as a joda DateTime but it thinks it is null.

Thank you very much for your help. Yes, that was only a copy and paste error. I am now ignoring the Designer problem. I tried around with sleeps and noticed, that the following action works:

val DateTime soon = now.plusMinutes(1)
logInfo("Testing", soon.toString)

However, if I comment that and activate my original version, it doesn’t work:

val DateTime jetzt = now.plusMinutes(1)
logInfo("Testing", jetzt.toString)

The following error occurs:

2016-09-17 21:41:11.661 [ERROR] [m.r.internal.engine.RuleEngine] - Error during the execution of startup rule 'Test rule': cannot invoke method public java.lang.String org.joda.time.base.AbstractDateTime.toString() on null

Do I have a strange caching issue?

That is quite odd indeed. By my eye, except for renaming “soon” to “jetzt” the two are identical.

Instead of “activating your original version” try editing the working “soon” version to be what you want it to be and see if that makes a difference. There might be an errant hidden character or the like in your original version that is mucking things up.

I admit, I’m grasping at straws.

Editing the working test rule to a “real” rule has worked until now, thanks a lot! Even with “jetzt” :smiley: I guess there might be some whitespace in the not working rule, maybe from copying the rule from the wiki or some online example.

It is amazing and a little frightening how often just retyping out what you copied from somewhere works to fix this sort of error. Glad you got it to work.