ECMA 11 ir. not defined

  • Platform information:
    • Hardware: _Macmini I3, 16 GB
    • OS: Linux Mint Mate 20.3
    • Java Runtime Environment: Zulu 11 jdk
    • openHAB version: 3.2.0 Release Build
  • Issue of the topic: Script execution of rule with UID ‘f47124c721’ failed: org.graalvm.polyglot.PolyglotException: ReferenceError: “ir” is not defined
  • Please post configurations (if applicable):
    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Hello, I’m having some trouble with an ECMA 11 script.
I got the following error:

Script execution of rule with UID 'f47124c721' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "ir" is not defined
//variables
var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.rule." +ctx.ruleUID);
var ScriptExecution = Java.type("org.openhab.core.model.script.actions.ScriptExecution");
var TimerExpired = function(){logger.info("All lights ON; Timer expired!")};
var ZonedDateTime = Java.type("java.time.ZonedDateTime");
var Minutes = 0;
var Cloudiness = ir.getItem("openweathermap:onecall:d5bb69232e:local:current#cloudiness");
var Now = ZonedDateTime.now();
var SunSet = ir.getItem("AstroSun_EndTime_30MIN").state;
var myTimer = null;

logger.info ("Variables passed")

Above the first part of the script. This one used to work in my previous installation of OpenHAB 3 1.0. After I did do an new installation the script generates an failure in this script.
Question is: "What did I do wrong.?

Any help would be appreciated.

What are you expecting variable ir to be?

I assume you are referring to the JS Scripting add-on version of ECMAScript. Note, you’d need to install this add-on yourself (it’s not the default) and you’d need to change the script type from “application/javascript” to “application/javascript;version=ECMAScript-2021” on the code tab.

If you’ve not done these things then you are using ECMAScript 5.1 Nashorn and there is something else going wrong.

Indeed ir is not automatically injected into your rule for you like in Nashorn ECMAScript 5.1. It’s a wholly different environment and while you can write code that is compatible with both, you are better off if you use the openhab-js library which comes with the add-on and is installed and imported into your rules by default.

When doing that you will find everything you need to know at the JS Scripting add-on’s readme: JavaScript Scripting - Automation | openHAB

Your code should look something like:

var logger = log.getLogger(ruleUID);
var TimerExpired = () => {logger.info("All lights ON; Timer expired!")};
var Minutes = 0;
var Cloudiness = items.getItem('Name_of_item'); // what you have is a Thing UID, not an Item name
var Now = time.ZonedDateTime.now();
var SunSet = items.getItem('AstroSun_EndTime_30Min').state; // note this will be a String representation of the state, not a DateTimeType
// If you want a ZonedDateTime version of this Item's state use
// var SunSet = items.getItem('AstroSun_EndTime_30Min').rawState.toZonedDateTime();
var myTimer = null;

logger.info('Variables passed');

actions.ScriptExecution.createTimer(...

@Rich,
Thanks for your response.
That pointed me in the right direction.
ECMA script 11 is already installed as is of course openhab-js library.
That means that I have to start learning this language. Takes some time but that is all.

I assume you are referring to the JS Scripting add-on version of ECMAScript. Note, you’d need to install this add-on yourself (it’s not the default) and you’d need to change the script type from “application/javascript” to “application/javascript;version=ECMAScript-2021” on the code tab.

But what do you mean by you’d need to change the script type from “application/javascript” to “application/javascript;version=ECMAScript-2021” on the code tab?*
Where do I find the code tab? I’m a newbee to this scripting language.

Rosko57,
thanks for your response. I was looking in a totaly wrong direction. @rlkoshak was putting me in the right direction. So I have to start from the beginning with a new language.

Rich,
thanks for the last message for pointing me to the code tab.