Multi-Option Custom Items

Are there any plans to introduce custom items with more than 2 states?

I’d like to be able to define controls that can have multiple states (a simple dropdown to select the values would be nice). A good example would be a custom control to control the heating mode (ON, OFF, AUTO, BEDTIME, etc).

Can this be done already?

Thanks in advance.

Yes, with a rule and a string item

string myCustomItem
...

rule "4 states"
when
    Item myCustomItem changed
then
    switch(myCustomItem.state.toString) {
        case "ON":
            Do something...
            break;
        case "OFF":
            Do something ...
            break;

         ...
    }
end

For anyone else wondering about this…

Items file:

String HTEST "Test switch" <switch>

Sitemap Entry:

Selection item=HTEST label="TEST" mappings=[0="Auto", 1="On", 2="Off"]

works with Number items and switches, too:

Number scene "current scene"

and

Switch item=scene icon="colorwheel" mappings=[1="early",2="day",3="late",4="night"]

Can we have the same item, with mappings, defined as read-only?

It may ‘work’ with those item types, however only the ‘String’ item type shows the currently selected item text next to the item in the sitemap (if you click ‘Auto’ for example, ‘Auto’ shows next to the item). This is preferable to me as I like to know what the state of the ‘option’ is.

I would use a Number item for scenes though, I think this may a be useful feature for selecting scenes, as a workaround for openhabs refusal to add scenes natively, and I wouldn’t care about knowing the state since I would be using the options as buttons effectively.

If anyone is using string items for options in sitemaps, be aware you have to compare for the string literal of the state in your rules markup (ie. check for state==“0” instead of state==0). It can be confusing because openhab reports them as if they were actual numbers in the log. openhab, should, in my opinion, surround string states with quotes.

You can use transformations to map a number into a string when displaying:

Switch item=scene label="scene [MAP(scene.map):%s]" icon="colorwheel" mappings=[1="early",2="day",3="late",4="night"]

and in /etc/openhab2/transform/scene.map put

1=early
2=day
3=late
4=night