[SOLVED] Error in rules (Cannot reference the field 'NULL' before it is defined)

rules
openhab2
Tags: #<Tag:0x00007f014d3c6e78> #<Tag:0x00007f014d3c6ba8>

(Pavel Eremin) #1

Here is my rule

import org.eclipse.xtext.xbase.lib.Functions
import org.joda.time.DateTime

var Number washmachineStartDetected = 0
var DateTime washmachineFinishedAt = NULL
var DateTime washmachineStartedAt = NULL
val org.eclipse.xtext.xbase.lib.Functions$Function1<DateTime, Boolean> washmachineNotify = [
	DateTime washmachineFinishedAt |
		var DateTimeType dt = new DateTimeType(washmachineFinishedAt.toString())
		var String message = String.format("Стирка закончена в %s", dt.format("%1$tH:%1$tM"))
		sendBroadcastNotification(message)
		true
]

rule "WashmachineNotifications"
when
	Time cron "0 0 * * * ?"
then
	if (washmachineSendNotification.state == ON && washmachineFinishedAt != NULL && washmachineFinishedAt.plusMinutes(50).isBefore(now)) {
		washmachineNotify.apply(washmachineFinishedAt)
	}
end

rule "WashmachineOpen"
when
	Item washmachineOpen received update OPEN
then
	washmachineSendNotification.postUpdate(OFF)
end


rule "WashmachineStatus"
when
	Item washmachineCurrent changed
	//Item washmachineCurrent received update
then
	logInfo("washmachineCurrent", washmachineCurrent.toString())

	if (washmachineCurrent.state >= 0.25) {
		washmachineStartDetected = washmachineStartDetected + 1

		//if (washmachineStartDetected == 3) {
		if (washmachineStartDetected == 1) {
			washmachineStartedAt = new DateTime()
			washmachineFinishedAt = NULL
		}
	}
	else if (washmachineCurrent.state == 0) {
		washmachineStartDetected = 0;

		if (washmachineStartedAt != NULL && washmachineFinishedAt == NULL) {
			washmachineStartedAt = NULL
			washmachineFinishedAt = new DateTime()
			washmachineSendNotification.postUpdate(ON)

			washmachineNotify.apply(washmachineFinishedAt)
		}
	}
end

and I got this error

10:40:31.949 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'washmachine.rules', using it anyway:
Cannot reference the field 'NULL' before it is defined
Cannot reference the field 'NULL' before it is defined

What it mean? And how to fix it?


(Alpoy) #2

what happens if you change the NULL references to null (lowercase)?


(Pavel Eremin) #3

null helped me :+1: thank you. But it’s interesting thing :hushed:


(Antares2001) #4

Great @lfs_alp5

That finally solved a problem I’ve been facing a long time. :crazy_face:


(Alpoy) #5

:slight_smile:


(Rich Koshak) #6

NULL != null

NULL is a special State that an Item can have to indicate it is uninitialized.

null is a programming construct that means no value.

The two are distinct be and not interchangeable.


Is there a difference between null and NULL?