State display from stateDescription in DSL rule

Hello,
In a DSL rule I want to display the state of an item using the values defined in its stateDescription. Does anybody know how to achieve that?

Any hint is welcome
Hermann

I don’t think you can. You’d have to do the formatting yourself. Without more information (e.g. what type of Items, what does the stateDescription do, etc) I can’t really give you specifics.

The item is the state of my UPS, type String.

The stateDescription has the following options:

OFF=Off line
OL=On line
OB=On battery
LB=Low battery
RB=Replace battery
OVER=-OVER-
TRIM=-TRIM-
BOOST=-BOOST-
CAL=-CAL-
BYPASS=Battery bypass active or no battery installed
NULL=-NULL-

What does it mean to “do the formatting yourself”? Is it possible using DSL code without knowing the map?

Possibly. But it’s not a one line thing.

Each Item has a `getStateDescription() method. GenericItem (openHAB Core 5.1.0-SNAPSHOT API)

That returns StateDescription (openHAB Core 5.1.0-SNAPSHOT API) which has a getOptions() method that returns a List<StateOption>. A StateOption StateOption (openHAB Core 5.1.0-SNAPSHOT API) has a getLabel() and getValue() method.

So you would need to:

  1. pull the state description
  2. get the options
  3. loop through the options to find the one that matches the current state

It would be something like:

val options = MyUpsItem.stateDescription.options
val currOption = options.findFirst[ opt | opt.label == MyUpsItem.state.toString() ].value

Of course this includes no error checking and will throw an exception if there is a state that isn’t in the options (e.g. NULL or UNDEF).

Thanks a lot, it works.
The only thing I had to change is to switch label and value, because I want to get the label from the value: