Does an inline MAP service exists without using a map file

I try to convert the states of a switch from ON to online and OFF to offline but does not want to have an additional map file for that. Is it possible to use the MAP service inline in the items stateformat section? Something like this - but this does not work:

Switch raspberry "Raspberry state [MAP(ON=online, OFF=offline):%s]" {...}

As an alternative I thought about using an inline state transformation script with an ternary operator like this - but this does not work either:

Switch raspberry "Raspberry state [SCRIPT:graaljs(| input.equals(\"ON\") ? \"online\" : \"offline\")]" {...}

The information is coming from the network binding with the “online” (ping) channel which is a switch and not a contact (I don’t understand why because it’s more a read only thing…). I want to show it in a HABPANEL just as the text online or offline instead of the switch states ON and OFF.

update: using OH 4.1.1

[JS(|{"ON":"online","OFF":"offline"}[input]):%s]

But yeah, I like the MAP syntax better, although it’s not currently supported

Mhh, I’ve changed my item definition like this:

Switch networkState "network status [JS(|{\"ON\":\"online\",\"OFF\":\"offline\"}[input]):%s]" {channel="network:pingdevice:mydevice:online"}

But this results in the following error:

2024-01-15 18:34:24.094 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: SyntaxError: <eval>:1:5 Expected ; but found :
{"ON":"online","OFF":"offline"}[input]
     ^

        at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
....
2024-01-15 18:34:24.103 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state 'ON' on item 'networkState' with pattern 'JS(|{"ON":"online","OFF":"offline"}[input]):%s': Failed to execute script.

Do I still need to use JS in OH 4.1.x? I’ve read about a change in OH 4.x to use SCRIPT and specifying graaljs instead of using JS which using the old Nashorn engine - but I don’t know.

In the olden days (3.x): JS
For a brief period (3.x): JSSCRIPT
Since 4.x: SCRIPTJS

See Script Transformations

I don’t use JS and I’m not familiar with its syntax (I had to google but I might be looking for the wrong thing).

If you have JRuby this is the syntax

Switch networkState "network status [RB(|{'ON'=>'online','OFF'=>'offline'}[input]):%s]" {channel="network:pingdevice:mydevice:online"}

With JS you could probably use a ternary syntax like this:

Switch networkState "network status [JS(|input=='ON'?'online':'offline'):%s]" {channel="network:pingdevice:mydevice:online"}

Thanks a lot - this works!

A question about your comments your 2nd last post about version 3.x vs 4.x and JS vs. SCRIPT (the documentation about inline script state transformation is relatively short and I was not able to find examples which helped).
Does this mean that JS (for OH >=4.x) now automatically using graaljs as the default JavaScript implementation (no additional script addon is required and the old Nashorn is not longer used)? Does this also implies that the word SCRIPT is not longer needed/available as a transformation keyword?

You cannot use SCRIPT anymore. It was removed in 4.x and replaced with direct names of the corresponding scripting language names, so instead of SCRIPT:JS you’d just use JS just like how it used to be in 3.x. Except now you can also use other scripting languages:

JS => graalvm js ecma2021 or whatever version it is now
NASHORNJS => ECMA5.1
RB => Ruby
DSL => Rules DSL
GROOVY => Groovy
Any future automation script will be automatically supported. It’s based on the script’s file extension, uppercased.

Check out the link I posted above.

Thanks a lot for clarification !

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.