OH3 how to use map transform in ECMA Rules

I’m moving my DSL rules into openHAB3 and I’d like to also move to EMCAscript while I’m at it.
first of all: there’s no GUI way to add map Transformations in openHAB3 yet, correct? You must copy the files to /etc/openhab/transform?

so, the main question: how can I migrate that DSL line to Javascript - and what do I need to import for that?

var String name = transform("MAP","mynames.map",Userid.state.toString())
1 Like

I’m interested too, can’t find a way through.

I’ve just come across this as I’m also looking for a way to access transform in ECMAScript.

You can do it without rule if it’s just an item you want to transform:
Just go to Things -> Channel -> Select Item. At the bottom you can choose a profile, select MAP and type the file name: mynames.map (this file is inside your /etc/openhab/transform)

I’m looking for it as I need to get the string in rule - currently I have duplicated the map as an array

It is possible to create your own solution and possibly adapt it to your needs:

this.maptransform = function(filename, parameter)
  var TypeMap = Java.type("java.util.HashMap");
  var TypeReader = Java.type("java.io.BufferedReader");
  var TypeFile = Java.type("java.io.FileReader");
  var file = new TypeFile(Java.type("java.lang.System").getenv("OPENHAB_CONF") + "/transform/" + filename);
  var reader = new TypeReader(file);
  var map = new TypeMap();
  var line = new String();
  var delimiter = "=";
  while ((line = reader.readLine()) != null)
    if (line.trim().length()==0) continue;
    if (line.charAt(0)=='#') continue;
    var delimPosition = line.indexOf(delimiter);
    var key = line.substring(0, delimPosition).trim();
    var value = line.substring(delimPosition+1).trim();
    map.put(key, value);
  return map.get(parameter);

and calling:
var mapping = maptransform("config.map", "email");


@Dant Thanks a lot for this helpful snippet.

I’m migration all my rules from DSL to JS and wondering that a function like this isn’t available out of the box.
@rlkoshak Rich, Can you give perhaps some background information?

It is available out of the box. See Transformation (openHAB Core 3.1.0-SNAPSHOT API)

var Transformation = Java.type("org.openhab.core.transform.actions.Transformation");

Transformation.transform("MAP", "mynames.map", Userid.state.toString());

All the actions are available.

All of the other actions are available at org.openhab.core.model.script.actions (openHAB Core 3.1.0-SNAPSHOT API)

Interaction with Persistence is through PersistenceExtensions (openHAB Core 3.1.0-SNAPSHOT API)


Awesome! Thanks a lot @rlkoshak!
Wasn’t aware of it because it’s not mentioned in the docs Transformations | openHAB :frowning:

Thanks a lot!
Finally the solution I needed.
And what’s more now I think I am able to find other soultions myself using your documentation links.

Best Regards,