OH3 javascript ES11: using openhab-js and/or @runtime

Does anyone has an idea for a better solution?

I do have 3 roller shutter in the living room. Each one is controlled by a NumberItems that can have the values 1,2,3 or 4. The values are mapped as follows: 1 = 0%, 2=40%, 3=60% and 4=100%.
The mapped values represent the closing state of the shutter.
Additionally I do have a SwitchItem for each of these roller shutters that are triggered ON/OFF depending on the Azimuth of the sun. These switches are grouped and trigger/indicate whether the
respective shutter is in a shading position (40% or 60%) or open (0%)

I have written the following simple rules to control the shading also depending on the today forecast temperature. Everything is working fine so far.

But, what displeases me a bit is the fact that the openhab-js library does not support reading the item that has fired the event. For this I need to import the runtime I had used with the integrated “old” javascript version. Did I miss anything? Is there a way to achieve this with openhab-js.
Or is this still not yet supported?

Any suggestion for improvement is highly welcome.

Here are my rules:
A switch in the group receives command ON:

var logger = log("LRShutterShade");


function shade(locationName) {
  Object.assign(this, require('openhab'));
  let shutterItem = items.getItem("LR_Shutter_"+locationName+"_Pos");
  let shutterState = parseInt(shutterItem.state);
  let tempForcastMax = parseFloat(items.getItem('LocalWeather_ForecastToday_Maxtemperature').state);
  
  let shadePosition = (tempForcastMax > 24.0) ? 3 : 2;
  
  logger.info("shutter {} ; current Pos = {} ; new Pos = {} ; current temperature = {} °C", 
                locationName, shutterState, shadePosition, tempForcastMax);
  
  if (shutterState < shadePosition) {
    logger.info("Autoshade active for shutter {}", locationName);
    shutterItem.sendCommandIfDifferent(shadePosition);
  } else {
    logger.info("No move for shutter {} ; current Pos = {} ; current temperature = {} °C", 
                locationName, shutterState, tempForcastMax);
  }
}

Object.assign(this, require('@runtime'));

var shadeItem = itemRegistry.getItem(event.itemName);
logger.info("Atomatic {} switched ON", shadeItem.label);

var locationName = event.itemName.split("_")[2];
shade(locationName);

A switch in the group receives command OFF:

var logger = log("LRShutterDeshade");

function deshade(locationName) {
  Object.assign(this, require('openhab'));
  let shutterItem = itemRegistry.getItem("LR_Shutter_"+locationName+"_Pos");
  let shutterState = parseInt(shutterItem.state);
  
  // open shutter only if not already open or completly closed
  if (shutterState != 1 && shutterState != 4) {
    logger.info("Autoshade inactive for shutter {}", locationName);
    shutterItem.sendCommandIfDifferent(1);
  }
}

Object.assign(this, require('@runtime'));
var shadeItem = itemRegistry.getItem(event.itemName);
logger.info("Atomatic {} switched OFF", shadeItem.label);

var locationName = event.itemName.split("_")[2];
deshade(locationName);

event.itemState doesn’t work? It works for me. That will give you the raw state.

If you want the actual Item, as documented to get access to an Item use items.

items.getItem(event.itemName). Pay attention to the docs though, what you get back is a JavaScript wrapper around the Java Item Object which implements all sorts of stuff beyond what a basic Java Item offers. If you do need to get at the raw Item you can get at it using items.getItem(event.itemName).rawitem.

Thank you very much.
That indeed works for me as well. This makes the Object.assign(this, require(’@runtime’)); obsolete.
I had situations where I had to import this stuff to get access to the rawitem.
This because I had not known, that there is something like item.rawitem. And I cannot find it in the documentation either.

Note that the OH docs link to the full API documentation.

And if those fail you, the full API docs link to the actual source code.

For this I guess you meant item.rawState.
event.itemState leads to an error on my side.

var shadeItem = items.getItem(event.itemName);
logger.info("Item raw state {}", event.itemState);
logger.info("Atomatic {} switched ON", shadeItem.label);

var locationName = event.itemName.split("_")[2];
shade(locationName);

---

2022-02-07 17:18:20.538 [ERROR] [hab.automation.script.lrshuttershade] - TypeError: Cannot read property "message" from undefined

event.itemState works for me. Note it will only exists for rules that are triggered from an Item update or changed trigger. I think event might get wrapped for file based rules and it uses the Rules DSL implicit variable names (i.e. event.newState).

If you use console.log(event); it should dump all the data members to the log so you can see what’t in it.

That’s the trick! I got a command event.
Thank you Rich. That was very helpful.
Because, unlike items and item, event is not described in the documentation.