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

@florian-h05 is the openhab-js installed in the current stable release; OpenHab v.3.4.4 or do we have to install manually?

A version of openhab-js is included in all openHAB releases since 3.2.0 (I think it was 3.2.0, but that was a bit before my time as a contributor and maintainer), openHAB 3.4.4 includes openhab-js 3.1.2.

If you want to use the Quantity API, you need at least 3.2.0 but I‘d recommend the latest version (currently 4.3.0). You need to manually install those, but the openhab-js GitHub provides instructions to do so (you can even use openHABian). Please have a look at the (few) breaking changes for openhab-js 4.0.0: openhab-js/CHANGELOG.md at v4.3.0 · openhab/openhab-js · GitHub.

thanks not how to use npm but documented command fails

npm i git+https://github.com/openhab/openhab-js.git

npm ERR! code 128
npm ERR! An unknown git error occurred
npm ERR! command git --no-replace-objects ls-remote ssh://git@github.com/openhab/openhab-js.git
npm ERR! Warning: Permanently added ‘github.com’ (ED25519) to the list of known hosts.
npm ERR! git@github.com: Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR!
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

Either use openHABian or install the latest release with npm i openhab in the $OPENHAB_CONF/automation/js folder

thanks, how do I display version
I assume I will need to clean after a openhab upgrade?

note its working now.
for other peoples reference

rules.when().item("Energy_Mains_Total_Consumption").changed().then(event => {	
	//console.log(event)
	var curr_total = Quantity(event.newState);
	console.log("new="+event.newState +" q="+curr_total.float)  // outputs  new=140.021 kWh q=140.021

}).build("test Energy Consumption", "calculate energy usage every update");

You have two options:

  1. From the automation/js folder: cat package.json to see the version number or even better package-lock.Json
  2.  console.log(utils.OPENHAB_JS_VERSION);
    

Not necessarily, but you should keep in mind that your manually installed version might not be 100% compatible with openHAB and especially code completion in the UI.