Temperature rule producing "Unknown variable or command '>' " error

Dear all,

First of all I would like to thank all of you for the great support this forum gave me during the installation of OpenHap (Raspberry Pi3)

If I may ask a question (as I am really lost and after having checked on the web as well as in the forum I cannot find any solution), I tried to write a rule to close the RollerShutters if the outside temperature is over X.

The rule is the following :

rule "Volets Chaleur"

	when 
		Item Temperature_Ext received update
	then
		if (Temperature_Ext > 20)
				if (cFboxMacOnline2.state==ON || cFboxMacOnline3.state==ON)
					Central_Vol.sendCommand(DOWN)			
end 

Each time that the Temperature_Ext is updated, I can see following error in the log file.

2019-05-01 16:21:56.812 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Volets Chaleur': Unknown variable or command '>'; line 6, column 7, length 20

I tried replacing Temperature_Ext with Temperature_Ext.state, added a () after the .state, nothing seems to work.

May I kindly ask for your assistance ?

Thanks a lot !

Sam.

try:
if ((Temperature_Ext.state as Number) > 20)

Thanks a lot for your answer and solution.

In fact I just found another solution (yours is probably also working) which is the following :

rule "Volets Chaleur"

	when 
		Item Temperature_Ext received update
	then
		var TempVol = Temperature_Ext.state as Number
		if (TempVol > 20)
				if (cFboxMacOnline2.state==ON || cFboxMacOnline3.state==ON)
					Central_Vol.sendCommand(DOWN)
			
end

Honestly, I really have problems understanding the different types of “values” and comparing them but I am here to learn !

Once again thanks for the time you spent for me !

Sam.

:slight_smile:
Temperature_Ext is an Item, therefor an object, not a value
Temperature_Ext.state is the status of this Item, and it’s a status, not an ordinary value.
Temperature_Ext.state as Number is the status of this Item, explicitly casted to the Number Type. openHAB will in most cases do that silently :wink:

Sometimes, even casting is not enough and you have to enforce a different special type:

(Temperature_Ext.state as Number).intValue

for example will give explicitly an Integer.

Please be aware that sometimes a Number Item status is not of Type Number at all. If casting explicit to Type Number, that will lead in a null pointer exception, so to be save, before casting to Number, try if there is a Number to be casted:

if(Temperature_Ext.state instanceof Number)
1 Like

Thanks a lot for your clear and precise explanation Mr Hartmann !

Concerning my rule, I think I still have a lot to amend as the rollershutter are closing each time Temperature_Ext receives an update … I think I am missing some {} somewhere.

Best regards.

Sam.

When you use if() without { } , rules interpreter guesses that maybe you meant just one line to be controlled by the if().

Personally, I use { } every time, so that I know what’s going on.

if ( condition ) {
    // do stuff
}

if ( condition) {
   if (other condition) {
     //do stuff
   }
   // other stuff
}

Thanks for the tip, I will try that.

Have a nice evening !

Good Morning.

I tried and amend my rule.


rule "Volets Chaleur"

	when 
		Item Temperature_Ext received update
	then
		logInfo("Temperature: ", Temperature_Ext.state.toString())
		logInfo("Presence1: ",cFboxMacOnline2.state.toString())
		logInfo("Presence1: ",cFboxMacOnline3.state.toString())
		
		var TempVol = Temperature_Ext.state as Number
		if (TempVol > 20 && cFboxMacOnline2.state==OFF && cFboxMacOnline3.state==OFF)
					{Central_Vol.sendCommand(DOWN)}
		
			
end

The “values” of the checked items are the following :

2019-05-03 06:11:37.394 [INFO ] [smarthome.model.script.Temperature: ] - 3.5 °C
2019-05-03 06:11:37.404 [INFO ] [e.smarthome.model.script.Presence1: ] - OFF
2019-05-03 06:11:37.413 [INFO ] [e.smarthome.model.script.Presence1: ] - OFF

If I am correctly reading/understanding my rule (but I really doubt), it should trigger the event only if the 3 conditions are fullfiled but the temperature is well below 20°; however the rollershutter received a “down” command.

I am (again) a little bit lost … if you have 2 minutes to spend (or loose), may I kindly ask you to take a look at my rule and tell me what you would do ?

Thanks once again and have a nice day (and Week-end).

Sam.

It seems Temperature_Ext is a QuantityType item, which sadly makes comparisons even more difficult in rules.

Here is a thread talking about it, but basically you need to change your comparison from

if (TempVol > 20 &&...

To

if (TempVol > 20|°C &&...

Edit: you might need to remove the as Number statement as well. I haven’t personally worked with QuantityType items, so not sure how it should be done best.

Thanks a lot for taking the time to answer.
I will try that and keep you informed !

If you want to trigger when the temperature is BELOW 20 you need to the “>” into “<”! Initially it was the other way around!

No, I want to tigger when the temperature reaches (or is >) 20°; sorry if I was not clear.

Sorry for my late answer.

It is working; I just have to write a rule to open the rollershutter when the temp goes down under 20 (or anything else).
Thanks once again for your kind assistance.

Have a nice week-end !