How to create an "enum"?

It is a simple question but I could not find a tutorial how to create an own “enum” item. I would like to have an item with “day” “night” “outside” “vacation” etc…

Could someone explain me how to do that?

Thanks

What exactly do you mean or what do you intend to do with it?

For example, I have created two switch items, one for present / vacation and one for summer / winter, so that different actions are executed depending on the position in the respective rules.

But you could also create a string item and then fill it accordingly.

The short answer is you cannot. What you can do is use a String Item as @DrScr3w describes.

I mean I would like to have an “item” or something different, which I can set to different, custom “values” like:

“IN”
“OUT”
“VACATION”
“NIGHT”
etc.

this “Item” (or something else) I would like to use in rules afterwards. Something like "If “VACATION” then do this or that…

Is this possible?

You can create an item with the type String and fill it and query it in rules and let things happen accordingly. Please remember that the check (==) is case sensitive.


DSL rule example:

if (ItemOneMode.state == 'night') {
  logInfo("OneMode","Turn lights off")
} else if(ItemOneMode.state == 'vacation'){
  logInfo("OneMode","do other tings")
}

If using OH4 you can use javascript and case statements:

//use below if the item changes
//var rfinput = items.getItem(event.itemName).state;

//use below to test
var rfinput = "IN"

switch(rfinput) {
case "IN":
console.log('IN was selected');
break;

    case "OUT":
console.log('OUT was selected');
break;
    
case "VACATION":
console.log('VACATION was selected');
break;

//if nothing found do below    
default:  
    console.log("Nothing found in case statements" + rfinput)
}


Again, no this is not possible. But you can use Strings as demonstrated by @DrScr3w and @ubeaut. But you cannot restric those Strings like an enum.

But if you use a sitemap to select the values, you could use the mapping to specify the values and write them to the string item.

Selection mappings=[night=Night, in=Present, out=absent, vacation=Vacation] label="Select OneMode" item=ItemOneMOde

I don’t know whether this is also possible in a widget in the MainUI, but I think it is.

  1. create string item
  2. create sitemap with selection and corresponding mapping
  3. create a rule that checks the string and executes it accordingly

this will help you?

Hi .
As many folks have said you can create a string item then use metadata to add state descriptions something like this

value: " "
config:
  options: IN=IN,OUT=OUT,VACATION=VACATION,VACATION=NIGHT
  step: "1"
  min: "0"
  max: "3"

you can even then create a Default list widget to populate the string item
and rules then when item state changes like

`configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: testmestringitem
    type: core.ItemCommandTrigger
conditions: []
actions:
  - id: "2"
    configuration: {}
    type: core.RunRuleAction``

to trigger what ever if condition you want to happen when that string item is populated with your selected value





Maybe that will help get you going ?

Thanks for all replies. That was exactly the answer to my question! :slight_smile:

This widget is also very helpful

Just a question: What I do not fully understand how you “create a Default list widget”. Do you mean you make your own widget? If so, could you please send me the full code of such widget?

Thanks

when you click on add metadata you see options for


various default widget types you click on the kind you want and fill in the info as you need and it becomes a default widget for that item.

and afterwards to place the widget on a page? Which of the standard library you select (cell, slider, etc…) to see the new string item?

after you create your default widget you go to page and add from model then place check mark in the non semantic box and select your item click pick 1 and it will appear on the page in the selected place you chose




Hope this helps.

Thanks for the explination - that worked perfectly!!