I am getting errors when starting up Openhab 2 (I am on 2.3 stable) in function and procedure definitions, like:
2018-10-14 20:53:11.151 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Change Light_FF_GuestBedroom_Rear status if power reported is changing': The name 'updateLightsStatus' cannot be
resolved to an item or type; line 896, column 3, length 18
‘updateLightsStatus’ is a lambda function:
val Procedure2<SwitchItem,NumberItem> updateLightsStatus = [
SwitchItem theItem,
NumberItem theItem_Power |
logInfo("FILE","--> Evaluating the power update for "+theItem.name)
if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL) ) {
logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
theItem.postUpdate(ON)
} else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
theItem.postUpdate(OFF)
}
]
rule "Change Light_FF_LivingRoom_Rear status if power reported is changing"
when Item Light_FF_LivingRoom_Rear_Power changed then
updateLightsStatus.apply(Light_FF_LivingRoom_Rear,Light_FF_LivingRoom_Rear_Power)
end
from import org.eclipse.xtext.xbase.lib.Procedures.Procedure2. As a matter of fact, I am getting this error for all such lambda functions in my rules file.
import org.eclipse.xtext.xbase.lib.Functions
val Procedure2<SwitchItem,NumberItem> updateLightsStatus = [ SwitchItem theItem, NumberItem theItem_Power |
logInfo("FILE","--> Evaluating the power update for "+theItem.name)
if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL)
logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
theItem.postUpdate(ON)
} else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
theItem.postUpdate(OFF)
}
]
rule "Change Light_FF_LivingRoom_Rear status if power reported is changing"
when
Item Light_FF_LivingRoom_Rear_Power changed
then
updateLightsStatus.apply(Light_FF_LivingRoom_Rear,Light_FF_LivingRoom_Rear_Power)
end
The import 'org.eclipse.xtext.xbase.lib.Functions' is never used.
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
The field Tmp_HomeRules.offsetToTime refers to the missing type Object
...
The imports seem to be ok, but both functions and procedures are not resolved when loading to figure out the return types. I didn’t have that problem in OH2.1, but now in OH2.3. I noticed some updates where needed to rules between the 2 versions…
val updateLightStatus = [ SwitchItem theItem, NumberItem theItem_Power |
logInfo("FILE","--> Evaluating the power update for "+theItem.name)
if ( (theItem_Power.state as DecimalType).intValue >= 1 && (theItem.state == OFF || theItem.state == NULL) ) {
logInfo("FILE","--> Power is on, change "+theItem.name+" from OFF to ON.")
theItem.postUpdate(ON)
} else if ( (theItem_Power.state as DecimalType).intValue < 1 && (theItem.state == ON) ) {
logInfo("FILE","--> Power is off, change "+theItem.name+" from ON to OFF.")
theItem.postUpdate(OFF)
}
]
That will create a Procedure2 with the proper types for the arguments.
If you want a Functions$FunctionX, make sure the last line executed returns something and specify the type.
You don’t need the import.
You need to import Procedures to create Procedures$Procedure2. Functions only works for Functions$FunctionX
There are things that have always been a problem with Rules that the Rules parser used to let go. In 2.3 they made the syntax checking more strict so that we can get more meaningful errors so a lot of things that have always been on the bad side of acceptable are not full blown syntax errors.
I really hate to contradict you but in the rules you provided for the Islamic Prayer Times you use both a function and a procedure2 and a procedure5 and the only import is Functions. And it works!!
Are you sure because it makes no sense that it works, unless they started to include Pricedures$ProcedureX by default, in which case I bet it will continue to work without any imports what so ever.
Functions and Procedures are separate classes. You can use Procedures if you only import Functions because they are not the same thing. Just like I can’t use ArrayList when I only import HashMap.
So the only way it can work is if Procedures is already imported by default. And if Procedures is imported then Functions almost certainly is too.
still gives errors at system startup. If just refreshing rules on a running system, there are no errors. The startup errors seem not to be 100% reproducible, as if they depend on the startup status somehow. System runs fine though.