More generic getState action

Multiple topics exist on people trying to access Items through the itemname defined as a String

For changing the state of an item these exist:

sendCommand(String itemName, String commandString): Sends the given command to the specified Item to the event bus.

postUpdate(String itemName, String commandString): Sends the given status update to the specified Item to the event bus

For reading the state of an item there do exist ways like

import org.eclipse.smarthome.model.script.ScriptServiceUtil

val index = 5
val testItem = ScriptServiceUtil.getItemRegistry.getItem(“Virtual_Switch_1”)

or when Item is part of a group

var ItemTarget = ItemGrou[.members.findFirst[ j | j.name == ItemToFind ]

But does there exist (if not, why) a command similar as sendCommand&postUpdate like

getState(String itemName, String commandString)

(I know getState exists but this only works on Items not with String as input)

Best regards,

Bart

I assume you’re in Rules DSL?

I think all you need to do to access the state is ItemName.state. There’s no need to “get” the Item at all, all Items are automagically injected into the script context.

You don’t always know the name of the Item. Sometimes you must construct it or it’s passed to you somehow. If all you have is the name of the Item as a String, you can’t use "itemName".state. You have to use the name to pull the Item from the registry.

In all the other languages this is much easier because both the item registry is part of the presets but also because the Items are imported not individually like in MainUI but in a structure.

So for JS one can use items["MyLights_"+someVar].state.

You can’t do that in Rules DSL. You have to pull the Item by name from the Item registry (which is the second example in the OP).

The answer is no. The why is it’s possible to post an update to an Item or send a command to an Item without the Item Object itself. That’s what those postUpdate(itemName, state) and the equivalent sendCommand Rule Actions do. They put the update or command straight on the event bus without needing to involve the Item at all.

But to get the state of an Item one must get that from the Item Object. I imagine it’s theoretically possible to do it with a Rule action too, but there are so many other things one may what to do with the Item a better approach would be to make the ItemRegistry available and/or provide some sort of structure to retrieve an Item’s Object given it’s name.

Again, this is all from the Rules DSL perspective. The other languages already have this covered.

This got a lot easier in 5.2.0, you can simply use Items.get(String itemName).state (assuming that you know that the Item exists so that it won’t return null).

I’m not really sure that there’s that much difference in “difficulty”/convenience between that and the array access offered by JS? You can of course also do Items.get("MyLights_"+someVar).state if you wish to.

I forgot Items got added as part of the DSL improvements.