JSScripting use MAP transformation

Hi,

I used the MAP transformation in rules DSL files and I would like to use the same in JS scripts. I tried “transform”, “Transformations.transform” and some other variations, but had no success in getting it working without a JS error. Is there a way to use transformations in ECMAScript UI scripts?

Thanks,
Martin

Try the blockly transform block and check out the generated code and you’ll see how it is done in ECMAscript.

That was a really good idea, thanks for the hint! I got it working now.

For completeness, this is the relevant code:

var transformation = Java.type('org.openhab.core.transform.actions.Transformation');
console.log(transformation.transform('MAP', 'xy.map', 'valueToBeTransformed'));

There is actually a little bit better approach that you can use in JS Scripting (ECMAScript 2021). The helper library that comes with the add-on makes these sorts of things a little easier. But this is one place where I think it was missed. Transformation isn’t there in actions. I’ll open PR to add it. When it’s added you’d just need the following.

console.log(actions.Transformation.transform('MAP', 'xy.map', 'valueToBeTransformed');

Thanks for this hint! I saw the comments in your issue and tried it - and it is already working fine! I’m just wondering as I thought I tried something similar, but maybe I tried Transformations as plural or so.
Anyhow, this is the nicest solution, thanks!