ECMA11 OH4 Stringoperations not available for event.itemName

Hi everyone,

Iam using the docker version of OH4.0.1 in my raspberry pi 4.

In a newly written ECMA11 rule I want to react on the triggering event and extract information from the item name as follows:

if(event.itemName.toLowerCase().includes("livingroom"))
   eventPlace = "livingroom";

According to the error log neither “toLowerCase” nor “includes” is available as method in this case.
Isn’t “event.itemName” supposed to be a string ?

However, I tried it more explicit:

var strEventName = "";
strEventName= new String(event.itemName);

if(strEventName.toLowerCase().includes("livingroom"))
   eventPlace = "livingroom";

This one does not throw any errors… but “strEventName” is always undefined… Though “event.itemName” isn’t.

Any ideas where Iam going wrong ?

Thanks for your help !

This is a tricky point and there is an issue open for this but no solution as of yet.

In managed rules (i.e. defined through the UI), event is the raw Java event Object. That means event.triggeringItem, for example, is the Java openHAB Item Object, not the JavaScript Item Object. Similarly, event.itemName is a Java String, not a JavaScript String.

So you either need to call the Java methods or convert it to a JS String.

I think your attempt to convert it to a JS string failed because you need to use the factory method to convert something that isn’t a JS String into a JS String.

var strEventName = String(event.itemName);

Thx for the fast response and good to know that there is an open issue going on for this.
It is quite irritating that the editor even suggests those methods to you if they actually do not work.

However using the factory method to convert event.itemName to a string did do the trick !

That is really something…

Even for standard comparisons I have to go that way:

Not working:

if(event.oldItemState == "ON")

Neither working:

if(event.oldItemState == 'ON')

Working:

if(String(event.oldItemState) == "ON")

So that’s a situation where event.oldItemState is the Java State Object, not a String. Treating the state of an Item as a String by default is something unique to JS Scripting.

So the following would work:

var { OnOffType } = require('@runtime');

if(event.oldItemState == OnOffType.ON) {

That works because the State of a SwitchItem is an OnOffType. And that’s how you’d do it in Rules DSL only you don’t have to import OnOffType. But the openhab-js library, in an effort to normalize as much as possible to JavaScript, presents the state as a String by default.

But, as we’ve discussed, openhab-js doesn’t have power over event yet so it’s all Java.