Item Format Problem

Hi All,

Sorry to keep bombarding you with questions.

I’m struggling with formatting of Items. I have a few items that receive a either a ‘ON’ ‘OF’ or a two digit number.

I’ve set all items as String but openhab doesn’t like that. It things the ON is a OnOffType and it thinks the two digit numbers are a percentage.

What’s strange is the items seems to go in ok and when I look at the event.log it seems ok but I can’t work with these items in rules or use them to trigger a rule.

Is there a way to just tell OpenHab to get over itself and except when as strings.

I’m then converting the numbers to integers in the rules so I don’t mind if they are stored as strings.


It would help if you posted your rules and items definitions. I’ve not encountered this before and it may be in how you are trying ti use it,

That being said, if you truly have an items that only takes “ON” or “OFF” it should probably be defined as a Switch. Likewise your two digit number item should be defined as a Number.

What is key here is how are you trying to use them as a trigger? Since they are defined as Strings I don’t think they ever receive commands so a trigger like “received command” would never occur. You would have to use “received update”.

What do you mean by “work with these items in the rules?” You can’t reference them? You can;t cast them? Unexpected behavior?


Yes good point on the code. Below is some examples of my code.

The items in question looks like this.

  String setPos1 "Target Set Position 1 [%.1f C]" <temperature> (MainSpace) {mqtt="<[mymosquitto:timer/SA:state:REGEX(aSASETPOS(..).)]"}

This item can receive three values on mosquitto


These values are regex’d to extract


These are updating in my event.log. As in it says setPos1 updated to ON for example.

However in the debug log I am seeing these logs for aSASETPOS16-

Received update of a not accepted type (PercentType) for item setPos1


Received update of a not accepted type (OnOffType) for item setPos1

What I’m wondering is why openhab can’t just log these things as the text they are.

Also as mentioned I can’t use the command

	Item setPos2 received update

Finally my point on using these items in rules I’m doing something like this but again this doesn’t work.

rule "Set Position 1"
	Item setPos1 received update
	val setPos = setPos1.state.toString		
	if setPos1.state.toString=='ON' {
           //turn something on
	} else if setPos2.state.toString=='OF'{
               //turn something off
	} else {
                val target1 = (setPos1.state as DecimalType).intValue
	    //use the number value

Does that help explain a bit better?

Maybe it will work if you move the RegEx stuff out of the Item and do it in the rule. I wonder if the mqtt binding or the stuff behind the scenes has special cases for ON and OFF. Not sure why it would be using a PercentType. If you do the RegEx in the rule then the value of setPos1 won’t be anything like something inside openHAB so it might pass it through.

You can simply do something like:

if setPos1.state.toString.endsWith("ON-") {
    // turn on something
else if setPos1.state.toString.endsWith("OFF"){
    // turn something off
else {
    val Number target1 = new Integer(setPos1.state.substringAfter(setPos1.state.length-2)) // may be off by one in the length-2, you'll need to verify

It may not be quite as clean but it might get you to where you are going.

Also, if you are planning on putting setPos1 on your sitemap, I would probably separate the temperature out into another Item because every time you get a aSASETPOSON or aSASETPOSOFF it will write over your last temperature number. In fact, that very well may be the problem depending on how you are displaying this item on your sitemap.

This makes sense since the debug log is indicating that it can’t assign the value to setPos1 so setPos1 is never updated so nothing triggers the rule.

What is generating that error by the way, the MQTT binding or something in openHAB core?

I’ve not done a whole lot with regular expressions in the Items files yet; I’ve had full control over all of my messages thus far so there hasn’t been a need.

Good luck!


Hi Rich, this is looking like it’s going to work well from my initial tests.


Can you help with how to Persist these items with a transformation so I only get ON in the database as opposed to aSASETPOSON-

Thanks for you help.

This is where @bob_dickenson’s proxy approach might be useful. Create a second setpos1 item which is a switch instead of a string. Once you parse the regex post the state to the second item and make sure it gets persisted. In your sitemap and rules use the second item.

Hi Rich,

Been playing around with your script.

The first part using the .endsWith works well but I can’t get the Integer bit to work.

I’ve tried.

var setPos = setPos1.state.toString	
var setTemp = setPos.substring(9,11)

This works but the value is a string and I can’t convert it.

(setTemp as DecimalType).intValue

Doesn’t work. Finally I tried.

setTemp = setPos.doubleValue

This is hard!

Also, is there anywhere I can look as references rather than keep asking on these forums. Is there any good guides to Xtend etc.


tl;dr: Look at the the Xtend documentation, become familiar with Java and Joda for time based stuff, and use Designer with the <ctrl><space> combo.

This is a tough problem to answer unfortunately.

So the rules engine is based on XBbase but the docs refer to Xtend which is also based on Xbase. However, the Xtend documentation primarily focuses how you can get at Java from Xtend and how Xtend is different from Java. But they make the assumption that you already know Java.

Those who come to openHAB without knowing Java are therefore at an immediate disadvantage because the default way to get past a thorny issue where you don’t know how to make the language do what you want is to revert and do it using the Java library.

Unfortunately I am one of those familiar with Java so my advice here may be incomplete. But here is the process I follow when I need to figure something out.

  1. In Designer I use the <ctrl><space> combo all the time. When you start typing something in Designer and hit that key combo a menu will come up showing you all the valid completions for what you have typed. So if you type a class name (e.g. setPos) and ‘.’ then press <ctrl><space> it will show you all of the methods that exist on that object you can call. There is no documentation there like you would have with Java but just having the name, arguments, and return type is often enough to figure out what you need to call.

  2. I look at the examples to see if someone has posted an example that does something similar. Look to the bottom right hand side of any wiki page.

  3. Look at the Xtend documentation if it is something that seems more structural to the language (e.g. declare a hashmap, how to write a while loop, etc) and look to Java libraries for handling things like conversions (e.g. parse a String to an int, complex string manipulations, regular expressions, etc). If all else fails, you can usually find an example on Stackexchange or something showing you how to do something in Java which you can use in your rules when you get stuck.

  4. Try stuff. Xtend is actually really good about automatically parsing, converting, and transforming objects for you behind the scenes. Sometimes it can figure things out.

  5. Sometimes I will need to look at the documentation in the actual openHAB source code to figure out what is going on. This is exceptionally rare and usually occurs when I don’t know what to import so I can access something.

  6. For time based operations I’ve run into some cases where the Joda documentation is a must and others where I had to look up the Java way of doing things. The two overlap but not completely as far as I can tell. Examples are a godsend.

More than a lot of languages, I’ve found writing rules for openHAB to requires a lot of trial and error and learning by example as opposed to going through a few well defined tutorials to get the basics and going from there.

Maybe someone needs to write a “Learn openHAB the Hard Way”. :slight_smile:


Hi Rich,

Many thanks for this information. I’ll have good look through it all.