Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions configuration/jsr223.md
Original file line number Diff line number Diff line change
Expand Up @@ -534,9 +534,11 @@ Both caches implement the `org.openhab.core.automation.module.script.rulesupport

### `TriggerType` Objects (all JSR223 languages)

The following trigger types are defined by openHAB (custom triggers can also be defined) and take the specified configuration parameters.
The following trigger types are defined by openHAB (custom triggers can also be defined) and take the specified parameters in the constructor of [`org.openhab.core.config.core.Configuration`](https://www.openhab.org/javadoc/latest/org/openhab/core/config/core/configuration).
The value of the map in each configuration must be of type String, Number or Boolean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is that limitation put in place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is in ConfigUtil.normalizeType(). It accepts as value also a Collection, but I could not get it running with Groovy/JSR223. The collection ["A", "B"] was converted to String and then this string had to be the watched state in the trigger.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to pass in the configuration map values an item, a collection (ArrayList), a state and a command. The demonstration below is for UnDef, which is a state and cannot be a command, but with command the result is the same.

The fact that as map value item, state and command are rejected with the message „ of configuration value!“ means that ConfigUtil.normalizeType() fails validating, as „ of configuration value!“ appears only in ConfigUtil.normalizeType().

I think it would be more comfortable if states, commands, items and collections can be passed, and then the Configuration does .getName()/.toString() on them - if no better shortcuts exist. Also passing a collection would be nice, let’s say state: ['A', 'B'] creates two triggers with the same id and state:'A' respectively state:'B', or if the collection is after itemName: then many triggers, where only the item is changed.

import org.openhab.core.automation.Action
import org.openhab.core.config.core.Configuration
import org.openhab.core.automation.module.script.rulesupport.shared.simple.SimpleRule
import org.openhab.core.automation.util.TriggerBuilder

scriptExtension.importPreset('provider')
scriptExtension.importPreset('RuleSupport')

m = new org.openhab.core.library.items.StringItem("v")
itemRegistry.add(m)

automationManager.addRule(new SimpleRule() {
  {
    name = "Demonstration for /openhab-docs/pull/2578"
    description = "Pass as configuration map value string, item, collection, state and command"
    uid = "gh-docs-2576"
    triggers = [
    /*
     * fails: key cannot be an item, it must be string
     * TriggerBuilder.create().withId('a').withTypeUID('core.ItemStateChangeTrigger')
     *       .withConfiguration(new Configuration([itemName: m])).build()
     * [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script '/etc/openhab/automation/jsr223/a.groovy': javax.script.ScriptException: java.lang.IllegalArgumentException: Invalid type '{org.openhab.core.library.items.StringItem}' of configuration value!
     * “of configuration value!” appears only in the source code of ConfigUtil.normalizeType()
     */

     // next line has collecion as value, but means state must be the string "[A, B]"
     TriggerBuilder.create().withId('a').withTypeUID('core.ItemStateChangeTrigger')
          .withConfiguration(new Configuration([itemName: 'v', state: ['A', 'B']])).build(),

     /* fails: key cannot be a state or a command
      * TriggerBuilder.create().withId('a').withTypeUID('core.ItemStateChangeTrigger')
      *       .withConfiguration(new Configuration([itemName: 'v', state: UNDEF])).build()
      * ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script '/etc/openhab/automation/jsr223/a.groovy': javax.script.ScriptException: java.lang.IllegalArgumentException: Invalid type '{org.openhab.core.types.UnDefType}' of configuration value!
      */
    ];
  }
  Object execute(Action a, Map<String, Object> inputs) {
    org.slf4j.LoggerFactory.getLogger('C').error('Executed! item is ' + m.state)
  }
});

org.slf4j.LoggerFactory.getLogger('TYPE').error('TYPE IS ' + ['A', 'B'].getClass().toString())
Thread.sleep(2000)
events.postUpdate('v', 'A') // does not fire
Thread.sleep(3000)
events.postUpdate('v', '[A, B]') // does fire

which prints only

2025-10-27 14:33:43.665 [INFO ] [ort.loader.AbstractScriptFileWatcher] - (Re-)Loading script '/etc/openhab/automation/jsr223/a.groovy'
2025-10-27 14:33:43.981 [ERROR] [TYPE                                ] - TYPE IS class java.util.ArrayList
2025-10-27 14:33:49.006 [ERROR] [C                                   ] - Executed! item is [A, B]

The configuration can be passed to [`TriggerBuilder`](https://www.openhab.org/javadoc/latest/org/openhab/core/automation/util/triggerbuilder).`withConfiguration()`.
All parameters are Strings.
Read the JSR223 language specific documentation for examples of using these `TriggerType` objects.
Read the JSR223 language specific documentation for examples of using these [`TriggerType`](https://www.openhab.org/javadoc/latest/org/openhab/core/automation/type/triggertype) objects.

::: details timer.DateTimeTrigger

Expand Down