Trouble with < in a simple rule

Hello Guys.
I simply can’t get following to work:

rule "Varme Alrum"
	when 
		Item Temperature_Living_Room received update
	then
		if((Temperature_Living_Room.state as DecimalType) < (Temp_Living_SP.state as DecimalType))  {
			sendCommand(Heating_Living_Room, 100)
		}
		end

it says error under the <, ive tested also > == none of it works…

The items is defined as numbers:

Group:Number:AVG                Temperature_Living_Room 	"Gennemsnitlig Temperatur i Køkken / Alrum / Stue [%.1f °C]"    <temperature>   (Status)

Number Temp_Living_SP			"Temperatur Setpunkt Stue/Gang/Opholdsrum [%.1f °C]" 	<temperature>   (Temperature_indoor, Temperature_Living_Room, Living_Room)

What am i doing wrong?

That looks right to me.

Are you seeing an error in Designer only or do you get an exception in the logs when you run it? What is the exception?

You can try getting the primitives and doing the comparison on that. It doesn’t explain the problem but it should work.

val float livingRm = (Temperature_Living_Room.state as DecimalType).floatValue
val float livingSP = (Temperature_Living_SP.state as DecimalType).floatValue
if(livingRm < livingSP) {

I’ve only tested it in the designer, and it only “remarks” the < - everything else is ok… I’ll test it and see what happens :slight_smile:

How does OH know what datatype it is? it’s written as “Number” but i think the KNX data that comes is 9.001 meaning temperature as float - does it self detect that?

Try without so much brackets. This works for me:

.....
if (HeatPump_enable_heating.state==ON) {
            if (HeatPump_Temperature_4.state as DecimalType - HeatPump_Temperature_5.state as DecimalType > -1.5) {
                if (HeatPump_adapt_heating.state as DecimalType != 0) {
                    sendCommand(HeatPump_adapt_heating, 0)
                }
            }
.....

Finally i figured it out - seems like the Group item shouldn’t be as DecimalType:

		Item Temperature_Living_Room received update
	then
		if (Temperature_Living_Room.state < Temp_Living_SP.state as DecimalType )  {
			sendCommand(Heating_Living_Room, 100)
		}
		end

This works in the compiler :slight_smile:

Hmmmm, does it actually work when running?

This is related to [quote=“Rasmus7700, post:3, topic:13949”]
How does OH know what datatype it is?
[/quote]

It is actually really complicated. At a high level the Rules DSL is a weakly typed language. This means that the Rules engine does its best to figure out the type of the Item based on the context in which it is used. In this case it sees you are using numerical operators so it tries to convert the state to a Number before running the operation. If it is successful hurray! If not one often gets a NullPointerException, which is not often very informative.

It is interesting that Deisgner did not like the “as DecimalType” for the Group Item. As far as Designer is concerned there really is no distinction between the two in this regard. One would not expect to see an error until runtime.

I’m not really sure what is going on. I hope it works for you when it runs.