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