Unable to use QuantityType in Script, what am I not getting?

So I’ve done some RTFMing and gave up, need some help.

Why does the code below:

var QuantityType = (this.QuantityType === undefined) ? Java.type("org.openhab.core.types.QuantityType") : this.QuantityType;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


var awayTemp = 16.5|°C;
logger.info('Quantity Type Test:');

Result in this log below. Isn’t awayTemp supposed to become a QuantityType like expressed in this page https://www.openhab.org/docs/configuration/rules-dsl.html#scripts ?

2021-02-01 19:53:25.966 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'f54a140f0f' failed: <eval>:9:20 Expected an operand but found error
var awayTemp = 16.5|°C;
                    ^ in <eval> at line number 9 at column number 20

But isn’t this javascript, not DSL?

var awayTemp = new QuantityType("16.5 °C");

Yes, it was javascript. That worked, thanks.

I’m not sure I understand what DSL is, to be honest. I had RTFM the site looking for the syntax of how to initiate a QuantityType and didn’t find that syntax all in one String. I thought I was reading about javascript syntax.

Please, would you tell me the URL where I should find the documentation/wiki/etc for javascript scripting in OpenHab 3?


DSL is a “Domain Specific Language”, in this case the “native” rules language developed for openHAB version 1 and 2. It’s based on Xtend which in turn is based on Java.
Because of that history, a great many examples, most of the older forum postings, and most of the docs refer to it.
Because of that history, it continues to be available in OH3, though not really viewed as primary rules language.

javascript is javascript, a lot of general resource across the web.

In the openHAB environment, you’ll also see reference to JSR223 rules which generally means javascript.
For this, you could do worse than start at -

which was written in the context of using javascript rules in OH2 (hence “new rules engine” references) but is still sound for OH3.

A minor quibble. Rules DSL and Xtend are not really based on Java any more than JavaScript and Jython are based on Java. In truth it’s a unique language that actually bears little resemblance to Java beyond it’s resemblance to any language descended from C.

Having said that, all of the rules languages have to deal with Java. They are all running on Java and all the stuff used to interact with openHAB itself (Items, Actions, etc.) are in fact written in Java. So, while we do have to deal with Java stuff, the fundamental structure of the all of the rules languages are not Java.

I only point this out because it can become misleading for users to think they should go out and learn a bunch of Java programming and expect all of that to work in openHAB rules.

Just to add some detail, in specific the JavaScript supported by openHAB is Nashorn, the name of the embedded JavaScript language interpreter that comes with Java. This version of JavaScript implements ECMAScript 5.1. The reason why this is important is as you go out to the web for resources, you’ll find a whole lot of stuff that is only available in version 6 or later so you have to be careful.

JSR223 is an old name for the mechanism built into Java that allows these other languages to run on top of the Java Virtual Machine as part of a Java program (i.e. openHAB). So in OH 2.5 and older that means JSR223 applies to all the non-Rules DSL languages.

In OH 3, there is only one rules engine which uses the successor to JSR223 (I think, can’t remember what it’s called and it doesn’t really matter). So for all intents and purposes, all the supported languages are JSR223 now.