Tcp binding: multiple items from single IP/port

Hi all,

I have a controller which is sending out tcp packets to my openhab server on IP:port combination, from these tcp packets i’m creating items in openhab using the following item config:

String item1 “[%s]” {tcp="<[‘REGEX(item1 =([ABC]))’]"}
String item2 “[%s]” {tcp="<[
:‘REGEX(item2 =([ABC]))’]"}

The tcp packet looks like item1=A or item1=B or…
This works fine but as all the items use the same tcp channel ( an update on item2 will trigger item1 as well. The regex doesn’t match so item gets updated to ‘null’ (problem!)

I’m wondering if there is any way to avoid this update to ‘null’ on items that don’t match the regex expression? Basically I only wan’t the item to recieve an update when the regex is not ‘null’. unfortunately changing the port for each item is not an option. I wan’t to avoid this faulty updates as the other items lose their state due to this behavior (I have dozens of these items…).

Any ideas are more then welcome!

You will have to have only one Item bound to the socket and then have a Rule that parses out the message data and updates/commands item1, item2, et al as appropriate.

This is also how I do it.
I have these items:

String Tex_UDP_Receive	"Receive UDP String [%s]"	<settings>  { udp="<[*:'REGEX((.*))']" }			
String Tex_UDP_Send		"Send UDP String"	<settings>  { udp=">['REGEX((.*))']" }			

As you can see, one item is for incoming UDP messages, the other one is for sending them.

And then I have many (many) virtual items (not bound to a channel), and also some rules (if “Tex_UDP_Receive” contains X then do Y).

Good luck!

1 Like

Makes sense! Thank you for sharing!

Thanks for sharing as well, this is a great solution for me to incorporate my openhab into my RTI system since I cant tap into the MQTT bus.

I was mislead by Dries post, though the idea of virtual items helped a lot. I’m reving this old topic, so others are not mislead by the same route.

“>[]” means openhab connects to remote port
“<[]” means other server connects to openhab port

Both of the above opens a tcp or udp, and thus both sends and receives data in a single binding. So in the example where MichVw wants to have the remote end connect to openhab server just one item to rule them all is needed:

String item2 “[%s]” {tcp="<[]"}

Port ‘*’ does not work, and for this global binding we probably don’t want any transformation, but instead just do that in rules, hence the omitted leading command: and the empty transformation at the end.

I’ve updated the documentation to make it more clear:

Hey Rich,
I have a similar setup, where a udp server sends multiple strings (2 strings per room in a house), each string contains some information. I have created an item which receives massages from the udp server. So how to separate them in the rule and update the appropriate item accordingly.

Trigger a rule when the Item changes and write the lines of code to parse out the values from the String received. Then postUpdate or sendCommand those values to the appropriate Items.

Thanks for the reply rich!!!
But how to use consist in rule. I tried String.Consist but it is not working.
here is my setup of rules.

Rule " Rako_1"
   Item Rako_Scene Received update
  if (string.consist(43 03 00 09 F4))
     postUpdate(Rako_Room_9_Welcome, ON)


String Rako_Scene {udp="<['REGEX((.*))']"}
String Rako_Udp_Send {udp="<[ 0X5102"}
Switch Rako_Room_9_Welcome  "Room_9_Welcome"

What I am I missing here?

There is no such thing a “consist”. I don’t even know where you came up with that.

If you want to see if the String Rako_Scene received as an update equals that String:

if(Rako_Scene.state.toString == "43 03 00 09 F4"){

If you want to see if the update contains that text among other stuff

if(Rako_Scene.state.toString.contains("43 03 00 09 F4")){

And the Rule trigger needs to be

Item Rako_Scene received update

Notice the lower case “r”.