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.
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 !
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.