SOLVED - Rule errors need help please

Hi All,
I am trying to clean up my rules and I am using Visual Stude Code with the OH Extension.

I have a particular are that seems to throw an error, I think OH 2.2 may be executing the code correctly but cannoty be certain, in any case for the sake of cleanness and education I would really like to get to the bottom of why the rule is trowing errors.

rule "BR1 Humid.State"
when
Item BR1_Humid received update
then
	// set initial values if undefined
	if (BR1_Max_Humid.state == NULL) postUpdate(BR1_Max_Humid, Humid_Max_Init)
	if (BR1_Min_Humid.state == NULL) postUpdate(BR1_Min_Humid, Humid_Min_Init)
	// no errors above here
	if (BR1_Humid.state < BR1_Min_Humid.state  { postUpdate(BR1_Humid_State, "DRY") }
	if (BR1_Humid.state < BR1_Tgt_Humid.state && BR1_Humid.state > BR1_Min_Humid.state) { postUpdate(BR1_Humid_State, "NORMAL") }
	if (BR1_Humid.state > BR1_Tgt_Humid.state && BR1_Humid.state < BR1_Max_Humid.state) { postUpdate(BR1_Humid_State, "DAMP") }
	if (BR1_Humid.state > BR1_Max_Humid.state) { postUpdate(BR1_Humid_State, "WET") }
	// end of errors
end

The items used are defined as such:

Number	BR1_Humid	"Humidity [%.1f %%]"	<climate> (gBR1_Humid, chartpersist) { mqtt="<[MQTT:tasmota/BR1SO1/tele/SENSOR:state:JSONPATH($.SI7021.Humidity)]" }

Number BR1_Max_Humid "Max Humidity [%d %%]" (gBR1_Humid, statepersist)
Number BR1_Tgt_Humid "Tgt Humidity [%d %%]" (gBR1_Humid, statepersist)
Number BR1_Min_Humid "Min Humidity [%d %%]" (gBR1_Humid, statepersist)

Essentially I am checking a sensor reading against a set of use set thresholds.
They are all declared as Numbers, yet I get these errors in my IDE checker.

file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'Type mismatch: cannot convert from DecimalType to boolean'
at: '12,6'
source: ''
code: 'org.eclipse.xtext.xbase.validation.IssueCodes.incompatible_types'

and

file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'The method or field intVal is undefined for the type DecimalType'
at: '12,39'
source: ''
code: 'org.eclipse.xtext.diagnostics.Diagnostic.Linking'

and still on the same line:

file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'missing ')' at '{''
at: '12,93'
source: ''
code: 'org.eclipse.xtext.diagnostics.Diagnostic.Syntax'

On the other erroring lines I get just the one error for each of them:

file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'Type mismatch: cannot convert from State to Number'
at: '13,24'
source: ''
code: 'org.eclipse.xtext.xbase.validation.IssueCodes.incompatible_types'

This is driving me nuts, the error changes when I try things but no matter what I cannot get a clean sheet.

Any help or pointers appreciated.

Paul

[EDIT] Solved by Kevinhab - Thanks.

Check your code. Your parentheses and brackets are all over the place:

For example

if (BR1_Humid.state < BR1_Min_Humid.state  { postUpdate(BR1_Humid_State, "DRY") }

Should be

if (BR1_Humid.state < BR1_Min_Humid.state)  { postUpdate(BR1_Humid_State, "DRY") }

Missing ) in (BR1_Humid.state < BR1_Min_Humid.state )

You’re also updating BR1_Humid_State (a Number item) with a value of “NORMAL”. Well “NORMAL” is not a number :slight_smile:

It seems i am lucky it functions at all, or unlucky depending on your perspective.
I wll take a new view after your feedback

The BR1_Humid_State item is not a Number

if (BR1_Humid.state < BR1_Min_Humid.state { postUpdate(BR1_Humid_State, “DRY”) }
if (BR1_Humid.state < BR1_Tgt_Humid.state && BR1_Humid.state > BR1_Min_Humid.state) { postUpdate(BR1_Humid_State, “NORMAL”) }
if (BR1_Humid.state > BR1_Tgt_Humid.state && BR1_Humid.state < BR1_Max_Humid.state) { postUpdate(BR1_Humid_State, “DAMP”) }
if (BR1_Humid.state > BR1_Max_Humid.state) { postUpdate(BR1_Humid_State, “WET”) }

Your codes above is error, They should be :

if ( (BR1_Humid.state as DecimalType) < (BR1_Min_Humid.state as DecimalType) ) { postUpdate(BR1_Humid_State, “DRY”) }
if ( ( (BR1_Humid.state as DecimalType as DecimalType) < (BR1_Tgt_Humid.state as DecimalType) ) && ( (BR1_Humid.state as DecimalType) > (BR1_Min_Humid.state as DecimalType) ) ) { postUpdate(BR1_Humid_State, “NORMAL”) }
if ( ( (BR1_Humid.state as DecimalType) > (BR1_Tgt_Humid.state as DecimalType) ) && ( (BR1_Humid.state as DecimalType) < (BR1_Max_Humid.state as DecimalType) ) ) { postUpdate(BR1_Humid_State, “DAMP”) }
if ( (BR1_Humid.state as DecimalType) > (BR1_Max_Humid.state as DecimalType) ) { postUpdate(BR1_Humid_State, “WET”) }

Thanks very much, I seem to have a single error left now after changing the brackets.

It is not quite as per @kevinhab suggested I only have two closing brackets at the end prior to the channel declaration not three.

here is the current rule after the changes:

rule "BR1 Humid.State"
when
Item BR1_Humid received update
then
	// set initial values if undefined
	if (BR1_Max_Humid.state == NULL) postUpdate(BR1_Max_Humid, Humid_Max_Init)
	if (BR1_Min_Humid.state == NULL) postUpdate(BR1_Min_Humid, Humid_Min_Init)
	// no errors above here
	if ((BR1_Humid.state as DecimalType) < (BR1_Min_Humid.state as DecimalType))  { postUpdate(BR1_Humid_State, "DRY") }
	if ((BR1_Humid.state as DecimalType) < (BR1_Tgt_Humid.state as DecimalType) && (BR1_Humid.state as DecimalType) > (BR1_Min_Humid.state as DecimalType)) { postUpdate(BR1_Humid_State, "NORMAL") }
	if ((BR1_Humid.state as DecimalType) > (BR1_Tgt_Humid.state as DecimalType) && (BR1_Humid.state as DecimalType) < (BR1_Max_Humid.state as DecimalType)) { postUpdate(BR1_Humid_State, "DAMP") }
	if ((BR1_Humid.state as DecimalType) > (BR1_Max_Humid.state as DecimalType)) { postUpdate(BR1_Humid_State, "WET") }
	// end of errors
end

The remaining error is around the less than and greater than.

file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'Ambiguous binary operation.
The operator declarations
	operator_lessThan(Type, Number) in NumberExtensions and
	operator_lessThan(Number, Number) in NumberExtensions
both match.'
at: '12,39'
source: ''
code: 'org.eclipse.xtext.xbase.validation.IssueCodes.ambiguous_feature_call'
file: 'file:///p%3A/rules/humid-states.rules'
severity: 'Error'
message: 'Ambiguous binary operation.
The operator declarations
	operator_greaterThan(Type, Number) in NumberExtensions and
	operator_greaterThan(Number, Number) in NumberExtensions
both match.'
at: '13,114'
source: ''
code: 'org.eclipse.xtext.xbase.validation.IssueCodes.ambiguous_feature_call'

The error is not clear to me so not sure what is expected.

Regards

Paul

@idkpmiller,
OK, I fixed your errors with codes :

rule "BR1 Humid.State"
when
Item BR1_Humid received update
then
	// set initial values if undefined
	if (BR1_Max_Humid.state == NULL) postUpdate(BR1_Max_Humid, Humid_Max_Init)
	if (BR1_Min_Humid.state == NULL) postUpdate(BR1_Min_Humid, Humid_Min_Init)
	// no errors above here
	if ((BR1_Humid.state as DecimalType).intValue < (BR1_Min_Humid.state as DecimalType).intValue)  { postUpdate(BR1_Humid_State, "DRY") }
	if ((BR1_Humid.state as DecimalType).intValue < (BR1_Tgt_Humid.state as DecimalType).intValue && (BR1_Humid.state as DecimalType).intValue > (BR1_Min_Humid.state as DecimalType).intValue) { postUpdate(BR1_Humid_State, "NORMAL") }
	if ((BR1_Humid.state as DecimalType).intValue > (BR1_Tgt_Humid.state as DecimalType).intValue && (BR1_Humid.state as DecimalType).intValue < (BR1_Max_Humid.state as DecimalType).intValue) { postUpdate(BR1_Humid_State, "DAMP") }
	if ((BR1_Humid.state as DecimalType).intValue > (BR1_Max_Humid.state as DecimalType).intValue) { postUpdate(BR1_Humid_State, "WET") }
	// end of errors
end
if ((BR1_Humid.state as DecimalType).intValue < (BR1_Tgt_Humid.state as DecimalType).intValue && (BR1_Humid.state as DecimalType).intValue > (BR1_Min_Humid.state as DecimalType).intValue) { ...

You have
if ( X operator Y operator X operator Z )
which is a bit unwieldy, and dare I say ambiguous.
I would clarify as
if ( (X operator Y) operator (X operator Z) )
for my own sanity.

Question if I use the intValue will that only use the integer part of the value for the operator comparison?
If so how do I use the whole of the provided measurement instead (its set to one decimal place in this case as RH.

I will give your additional brackets ago to see if it eases the ambiguity to the parser.

The additional brackets did not chnage anythiong.
and although it may visually assist in simplifying it adds complexity when listening to it using screen reader. so for the time being I have opted not to add the additional brackets.

Thanks to all for the assistance here I have a clean piece of code and now ill get this implemented across the hundreds of lines I have looking similar to this for temperature and humidity

thanks

Paul

@idkpmiller, change .intValue to .floatValue