Create item in rules DSL with a quantity type

I can add a new item in rules DSL with

var temporaryItem = new NumberItem("testItem")

but how do I create a “Number:Illuminance” item?

Other things I can do are

temporaryItem.label = "testItem Label" // Change label
temporaryItem.category = "slider" // Change Category ie icon
newItem.addTags("Control", "Light") // This does Semantic Class and Semantic Property as well as custom tags.
newItem.removeTag("Control") // Remove a tag
ScriptServiceUtil.getItemRegistry.add(temporaryItem) // Adds this temporary item to the database and model.
ScriptServiceUtil.getItemRegistry.remove(testItem) // Remove item


A_Group.addMember(testItem) // Add to a group
A_Group.removeMember(testItem) // Remove from a group

ScriptServiceUtil.getItemRegistry.update(newItem) // Not 100% how this works. Some changes seem to need to be 'updated' to survive a reboot.

Are there other useful things you can do with items in rules DSL?
And how do I add metadata in rules DSL? Like stateDescription and WidgetOrder and HomeKit etc

I am not sure if it answers your question about creating items, … but you can create quantity type constant values like the following…

val motionActiveTemperature     = 20.0|"°C"

See https://www.openhab.org/javadoc/latest/org/openhab/core/library/items/numberitem.

new NumberItem('Illuminance', 'testItem')

There are limitations on what can be done with Items created in this way. You’ll have to experiment to see what works and what doesn’t. For example, you can create the Item but can you later delete it? Is the Item available outside this Rule? Persistence? etc.

You can’t. There’s no access to the metadata registry available in Rules DSL. Other languages do have access though, including Blockly I believe.

Amazing! Thanks!

I think it needs to be

new NumberItem('Number:Illuminance', 'testItem')

to spit out the same log as a UI created Number:Illuminance item when checking

loginfo("log", testItem.type)

I’ll have a play with this now.

Do you know if it’s possible to change the ‘Members Base Type’ and ‘Aggregation Function’ of group items though rules?

These are all Java classes. Make use of that link to the JavaDocs above. You can search for class names (all the Item class names follow the same naming pattern). The only apparent way to set the base type and aggregation function is in the constructor. But I suspect if you create a new GroupItem and call update on the ItemRegistry it will change those to match.

As far as I can read, it seems to be possible to achieve my goals.
https://www.openhab.org/javadoc/latest/org/openhab/core/items/groupitem#%3Cinit%3E(java.lang.String,org.openhab.core.items.Item,org.openhab.core.items.GroupFunction)

GroupItem​(String name, @Nullable Item baseItem, @Nullable GroupFunction function) Creates a GroupItem with function

I’m trying

var GroupItem temporaryItem = (new GroupItem('newTestItem', 'Number:Illuminance', 'AVG'))

and other slight variations, but not having any success.

Do you know the right way to type this?

Look at the types of the arguments. It takes a String, an Item, and a GroupFunction as the three arguments. You are passing three Strings. You’ll need to instantiate an Item for the type and instantiate a GroupFunction for the function and pass those as arguments.