Openhab-js: create group item with groupFunction

Dear all,

I try to create a group item with a groupFunction using the openhab-js library (in a file based script):

items.replaceItem({type: 'Group', giBaseType: 'Switch', groupFunction: 'and', name: room.name + '_PresenceDetected')

Unfortunately, that does not work:

[ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/openhab/conf/automation/js/PresenceMgr.js': org.graalvm.polyglot.PolyglotException: TypeError: invokeMember (withGroupFunction) on org.openhab.core.internal.items.ItemBuilderImpl@de57658 failed due to: Cannot convert 'and'(language: Java, type: java.lang.String) to Java type 'org.openhab.core.items.GroupFunction': Unsupported target type.

Defining the groupFunction as “and(on,off)” does not work either. What am I missing here?

Many thanks.

C.

In the docs it’s always big letters, AND ?

Hi, thanks but that does not work either:

TypeError: invokeMember (withGroupFunction) on org.openhab.core.internal.items.ItemBuilderImpl@3407c1ee failed due to: Cannot convert 'AND'(language: Java, type: java.lang.String) to Java type 'org.openhab.core.items.GroupFunction': Unsupported target type.
TypeError: invokeMember (withGroupFunction) on org.openhab.core.internal.items.ItemBuilderImpl@376eda3e failed due to: Cannot convert 'AND(ON:OFF)'(language: Java, type: java.lang.String) to Java type 'org.openhab.core.items.GroupFunction': Unsupported target type.

I am a bit lost since the docs do not provide much information on the usage of the groupFunction.

Best,
C.

Aggregation function AND does have parameters, have you tried giving a complete function? Like “AND(ON,OFF)”

The openhab-js readMe says that in the itemConfig object the groupFunction should be specified as a string type. However, the error you are getting seems to indicate that addItem expects this to be an actual group function type, not a string. This appears to be consistent with the io help file.

It may be worth dropping an issue on the library gitHub page to point out the discrepancy.

1 Like

Tried this one already as well:

TypeError: invokeMember (withGroupFunction) on org.openhab.core.internal.items.ItemBuilderImpl@376eda3e failed due to: Cannot convert 'AND(ON:OFF)'(language: Java, type: java.lang.String) to Java type 'org.openhab.core.items.GroupFunction': Unsupported target type.

'AND(ON:OFF)' is still a string. To work with any of the OH specific types, you have to pull in the appropriate Java class first. So, for an AND group function class you need:

var GroupAndFunc = Java.type('org.openhab.core.library.types.ArithmeticGroupFunction.And');

Then you’ll need to create a new instance of this GroupAndFunc class, but according to the javadocs:

Constructor
And​(@Nullable State activeValue, @Nullable State passiveValue)

the constructor requires states as the input parameters. So you’ll also need access to the OH ON and OFF state types, not just 'ON' and 'OFF' strings. Fortunately, this is made fairly easy, because ON and OFF are available through the runtime object so you can get those directly:

var { ON, OFF } = require('@runtime');

Now you’re ready to set up the item configuration object and create the item, which would look something like this:

var groupConfig = {
  type: 'Group',
  name: 'gFuncExample',
  label: 'Group With Example Function',
  giBaseType: 'Switch',
  groupFunction: new GroupAndFunc(ON,OFF)
}
newItem = items.addItem(groupConfig);
1 Like

You made my day! Thank you very much.

Please do still file an issue though. How to file an Issue

One of the goals of openhab-js is to make it so you never have to deal directly with the raw openHAB Java Objects. It really should accept a String for the groupFunction instead of requiring you to Java.type or pull in the function and the Java State Objects.

1 Like