ECMAscript 2021: Quantity is not defined

  • Platform information:
    Raspberry Pi 4 & OS, OH 3.4.1, ECMAScript 2021

Simple question I hope, I have the following line in my script

var threshold = Quantity(‘20 °C’);

The log says

2023-02-05 10:11:23.415 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘2dabdc1660’ failed: org.graalvm.polyglot.PolyglotException: ReferenceError: “Quantity” is not defined. The documentation reference is here

I have a feeling I was supposed to install something that I haven’t, or maybe some further configuration is needed. I have the JavaScript scripting library installed, and console logging, sendmail, etc are working.

I have tried including require(‘openhab’) and require(‘@runtime’). Neither of these fail but nor do they fix anything.

You are looking at features on the bleeding edge of development. I think you need to have the very latest library versions for this:

Thanks. That wasn’t obvious to me. Does this mean I need to use DSL instead of Javascript to compare a temperature to a value?

This is all I’m trying to do right now.

var tempitem = items.getItem('zwavedevice1b10bf339cnode8_Sensor_temperature');
var threshold = Quantity('20 °C');

console.info('Family Room Temperature now: ' + tempitem.state);
if (tempitem.state > threshold) console.info('Family Room Temperature is more than 20 Celsius');

Yes, correct. Actually the last release (3.2.4) already included the Quantity API, but in an „unfinished“ version. However to use the final API you currently need the latest dev version directly from GitHub, but I am planning to release a new version to npm today or tomorrow (I just want to do some final pre-release tests on my production system).

No. You just need to wait a bit until I release a new version, I will inform you then.
You then need to install the new version:

And your script is actually not correct, you cannot compare the state (that is string) with a Quantity (which is an object) using any of the JavaScript comparison operators.

Your code needs to look something like this:

var tempitem = items.getItem('zwavedevice1b10bf339cnode8_Sensor_temperature');
var threshold = Quantity('20 °C');

console.info('Family Room Temperature now: ' + tempitem.state);
if (tempitem.quantityState.greaterThan(threshold)) console.info('Family Room Temperature is more than 20 Celsius');

The comparison in this code works as following:

  • use the quantityState property of the Item: gets the state as a Quantity
  • compares using the greaterThan(value) method of the Quantity, where value can be another Quantity or a Quantity-compatible string

If you need more help, feel free to ask for it. Also have a look at the Quantity docs: GitHub - openhab/openhab-js: openHAB JavaScript Library for JavaScript Scripting Automation.

Florian. I really appreciate the amazing promptness and quality of your response. Thank you. I installed OpenHab with apt, so I will use the manual install.

Thanks for the clarification on using quantities. I’ll give it a go. The script will end up emailing me if the temperature drops below a threshold, indicating a problem with the furnace.

Meanwhile I’ve just done a 2 character slice of the state to compare in integer degrees, and that works for now. I’ll update it when the new version is released.

I’m looking forward to doing a lot more with scripting and I’m glad it’s now in a language I have a little familiarity with, although I’m much more adept with C & Python. I had a lots of scripts in OH2 that did all this, but let it all slide during the 3 years of mostly staying at home.

Thanks again,
Chris

1 Like

Thank you for your kind words :slight_smile:

I have just released version 4.0.0 of our openHAB JavaScript library, which included the finished, new Quantity API. The CHANGELOG is (as usual) available directly in the repo: openhab-js/CHANGELOG.md at v4.0.0 · openhab/openhab-js · GitHub. Please note that there are breaking changes, but they are unlikely to affect you since you are a scripting starter.

1 Like

I have just created a release post: openHAB JavaScript library releases.

Installed and the script is working. Thanks again.

Chris

1 Like