logInfo in the first line of a rules file

Tags: #<Tag:0x00007efec55a2ce0> #<Tag:0x00007efec55a2a88>

using RaspiOS on master branch

I am putting this on the first line of a rules file:

logInfo(“GFH”, “Hello GFH”)

There is nothing else in the file. I get the warning:

Configuration model ‘test.rules’ is either empty or cannot be parsed correctly!

and there is nothing logged.

Inside a rule-when-then it works fine.
Is this normal?

thx

That is not valid syntax for dsl rules. The forum is not a substitute for reading the documentation.

1 Like

From your answer I assume that the solution must be written somewhere. But from a rookies point of view this is a bit hard … It’s not that the documentation on OpenHab is that exhaustive isn’t it? And the simple fact that a text file consists of imports, declarations, and rules does not exclude what I was trying to do. Or did I miss that?

Probably I am too much preloaded with other language syntax that I’am doing things wrongly here, but I am willing to read into it.

If you please give me a second chance and a hint on this issue, it gives me the following error. The logs working fine, but the avgRoomTemp reports a null exception. What do I miss here?

An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.ObjectExtensions.operator_plus(java.lang.Object,java.lang.String) on instance: null

> rule "React on TEMP_FFParents change/update"
> when
>     Item TEMP_FFParents received update or
>     Item TEMP_FFParentsSet received update or
>     Item GFH_FFParentsForceFlow received update
> then
>     logInfo("GFH", TEMP_FFParents.state.toString)
>     logInfo("GFH", TEMP_FFBath.state.toString)
>     logInfo("GFH", TEMP_FFEmil.state.toString)
>     logInfo("GFH", TEMP_FFFelix.state.toString)
>     logInfo("GFH", TEMP_FFOffice.state.toString)
>     val avgRoomTemp = (TEMP_FFParents.state + TEMP_FFBath.state + TEMP_FFEmil.state + TEMP_FFFelix.state + TEMP_FFOffice.state) / 5
>     //logInfo("GFH", "avgRoomTemp: " + avgRoomTemp.state)
>     logInfo("GFH", "avgRoomTemp: ")
> end

Your original post stated you only has one line in the rules file. Obviously that is not a valid rule. That was the basis for my original comment.

Why the > at the beginning of the rule lines? Here on the forum we only know the data you provide about your system. In rules, spacing and syntax are important. I doubt you wrote the rule as posted here.

the > is only a mistake while posting. There is no > in my rules file.

1 Like

I think I found the way to access the integer value of an item:

val tempParents = TEMP_FFParents.state as Number;
val test = tempParents.intValue * 2

but what puzzles me is that TEMP_FFParents is already a Number (as defined in the items file). Why do I need to take this (for me looking) complicated route to get to the integer value?

Why can’t I do TEMP_FFParents.intValue for example?

thx

Personally the one time I tried using logic in triggers did not work consistently. I ended up with almost duplicate rules for each trigger.

I am personally looking at moving my rules to Python 3 by using HABApp. Python 3 is better documented & more powerful.

No, it isn’t. It is an Item, of flavour Number.
Items are not variables, Items are complex objects with several properties - label, icon, name.
The interesting part is the state.
Items can have states like “Help” or CLOSED or 223.47 or 55% …
The state is not a Number or a String or a DateTime etc… it’s a state.

Because it’s a state, if you need to handle it like a number that’s what you do. If you need to handle it like a string, you do something else.

Yes. A .rules file must have at least one rule defined in it. Also there is no context at the “top” of the .rules file so I’m not sure if logInfo even exists in that context.

If you want to see a log statement when a rule is loaded, you can add a System started triggered rule that has the logInfo.

NOTE: This only applies to Rules DSL rules. It does not apply to Jython, JavaScript, or Groovy rules where it is possible to put a log statement anywhere you want in the file and have it work.

You will almost certainly be happier using one of the other rules languages. Jython is the most mature at the moment. You can get started at [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x) (this is not yet compatible with OH 3 I think). But some of your confusion over type still apply with those languages so please read on.

Actually it’s a NumberItem. It carries a state (among other data members like name, label, list of group names, if it’s a Group the list of group members, etc). The state is of type State. Different Item types carry a different subclass of State. In NumberItem’s case, it carries a State of type DecimalType which itself implements the Number interface. When you call SomeItem.state you get back a State Object. You need to cast it to a Number (or DecimalType) if you actually want to do number type stuff with it like comparisons or calculations.

Also every Item can carry a special pair of states called NULL (never initialized) and UNDEF (Item is in an unknown state, for example the binding lost the connection to the device). If you cast a Number Item’s state to Number without first checking that the state isn’t NULL or UNDEF will result in an exception.

As I said, TEMP_FFParents isn’t a Number, it’s a NumberItem. You can sometimes get away with TEMP_FFParents.state.intValue though, if there is enough context at runtime for the Rules Engine to figure out that TEMPParents.state is of type DecimalType and therefore has an intValue method.

Thx for all the explanations and the tips for using HABApp and Jython - I will look into this.

All the explanations that it’s an object with additional properties like label etc. is fine, but it is not good enough to tell why there is no .getInt or similar way of doing this.
But again thank you for the heads up!