Best Practice for managing behind a serial bridge

New user. I have a serial bridge configured to control a bank of thermostats. The thermostats are each addressed through commands of the form “SN#[command]?” for queries and “SN#[command=value]” to set modes and setpoints.

Where can I find examples of best practice for configuring this? I have the serial bridge configured as a THING.

Is the best practice to make each thermostat a separate item? Something has to translate “Zone 1 South setpoint 65” to “SN2S=65” and then parse the response. I’m not clear on what the best way to create a hierarchy of items, channels and things should be, and I can’t find any examples.

Also would like to do this through the UI and a lot of the documentation is based on code rather than UI. If this is beyond what’s available in the UI would be helpful to know so I can switch to looking at code instead

Hi @awald

Welcome to openHAB. I don’t specifically do what you have to do but I hope I can give you an idea.

I would create just one Thing for each serial port. If you only have 1 then that’s ok.
The docs are all for text as it is easier to write an present.

Create a Channel for each function you want to do eg. power and temp.

Then create the items for those channels and put these in the model as equipment.

Eg if you create a switch channel for power
Create a Switch item with an onValue of “SN#[command=onsignal]”

Create a number channel and number item
In item setup parameter commandFormat to SN2S=%s
You can also set min and max so temp is in a certain range so family cant set it too low or high

Thanks. Very helpful. Taking one specific example:

Behind the serial bridge I have 15 thermostats that are addressable, and each has a bunch of commands. It sounds like what I should try is configure each 'stat as EQUIPMENT in the model. Then set up a channel and point representing each command and query.

Here are some examples of Commands and Responses:

C: SN15S? R: SN15 SH=68F “What is the current set point? / I am heating to 68F”
C: SN15T? R: SN15 T=67F “What is the current zone temp? / 67F”
C: SN15M? R: SN15 M=HEAT “What is your current mode? / Heating.”
C: SN15S=67 R: SN15 SH=67F “Adjust set point to 67F / Setpoint adjusted to 67F”

I need response REGEXs for the queries, and I need different command and response REGEX for the commands. When I create the channel and configure it as REGEX I am asked for one REGEX and not clear whether this is applied on transmit or receive. Do I need to use a MAP function instead of a REGEX since the parsing is asymmetric?

As an initial validation I’d like to Query a zone for set point and current temp and display the results in the UI. Then add a knob to adjust the set point and display the new set point.

There’s another layer of complexity I’ll deal with later - that HVAC zones have a combination of forced air and radiant systems and different thermostats control them. So I probably represent each ZONE as having multiple EQUIPMENT with each EQUIPMENT having POINTS and then RULES to manage how a zone set point turns into commands (for example commanding cooling to a zone should shut down all the radiant heat).

Is this on the right track?

In general, yes you’ll probably want separate Items representing each thermostat. This is independent of the technology involved. That’s just how the normalized Items layer in openHAB is expected to work.

You can definitely do this through the UI. Anything you can do in text files you can do in the UI when it comes to configuring Things and Channels (and in fact there are some things that can only be done through the UI).

Approaches to translate between text configs and UI configs though may differ depending on what you are working with. For Things it helps to note that every parameter you find in the .things file will exist in the UI. If you open the “Code” tab, the parameters are even named the same in both places.

Usually there are two. I don’t know the Serial binding though. There is often an “Advanced” checkbox. Click that and you’ll see all the parameters. Some bindings hide some parameters under that.

As for REGEX and MAP, it depends on the nature of the messages. Sometimes MAP is better, sometimes REGEX is better.

From looking at the docs it looks like you’d create a separate Serial Device Thing for each thermostat. And a Serial Device Thing supports both a stateTransformation (transforms incoming messages) and a commandTransformation (transforms outgoing messages). There’s also the “commandFormat” which lets you create a String for the command similar to how you would define the label for an Item. So a transform may not be needed for the outgoing messages if you can handle it all in there. For example, the “name” of the specific thremostat you this Serial Device Thing represents would go there.

To clarify a bit, each individual setting or measurement would be represented as separate Items - e.g. measured temperature, target setpoint, on/off status. These may not all be of the same type.

You don’t have to, but you might conveniently collect a set of Items representing one thermostat in a Group type Item.
If you are using semantic model (again you don’t have to) then the Items are referred to a Points and the collection as Equipment - but its really the same Group/Item structure underneath.

Obviously for your own sanity you would give your Items meaningful names - “Bedroom2_setpoint” etc. perhaps.
These do not have to relate to your Thing and channel names, which might be more about the technical addressing “Device26-parameterT” or suchlike.

Thanks. I’ve been experimenting with a String Data Point channel to verify I can send a command.

Two problems now: I’ve chosen “Command Options” and put a simple query in the Metadata field “SN15S?” The device needs a \r return character and nothing I’ve tried adds that character. I have a separate terminal process attached to the device and when I send the return character manually from the keyboard I see a response on the terminal. But OH3 doesn’t seem to get the response. Not sure what I’m doing wrong.

So the two questions are

  1. How do I add a \r to the command (metadata escape character or config?)
  2. Have I chosen the right channel mode to send a command AND get a response?
  1. Can’t help there. Seems like you should be able to add a \r to the end, but are you sure it’s not looking for \n, or even a Windows \r\n?

  2. Yes, it appears that you should be able to send and receive on the same Channel. Beyond that I can’t say. These super low level bindings are fiddly and take a good deal of effort to get working.

I put an strace on the port. When I send the string “SN15S?\r” from Metadata what appears on the port is “SN15S?\r”.

I’m sure \r is the correct EOL character for the device because when I send “enter” from the terminal, strace reports it as “\r” and I get a response from the device. Metadata seems to be adding an implicit escape to the first backslash and I haven’t found a way around that yet

Correction: when I send “SN15S?\r” what appears on the port is “SN15S?\r”

It’s not possible to use the UI to enter escaped characters. You need to configure it using a config file or try the code tab workaround mentioned at the end of this thread:

Tried what’s in the other thread using default, UTF-8 and ISO-8859-1 encoding

value: " "
options: ‘“SN15?\r”’


** \“SN15?\r\”**

Also tried setting up another point as Binary64, but not sure I have a good encoder. It’s a mystery!

Also tried editing the metadata config file in /var/lib/openhab/jsondb. Is that the right config file? The format is different from the .things file shown in the Serial Binding documentation.

Same result: some stage in the processing converts “\r” to “\r” I also tried “\u000d” which gets converted to “\u000d”.

Is there some magic I can try with regular expressions? which REGEX gets applied on send vs receive operations?

No, not if you are talking about the org.openhab.core.items.Metadata.json file. Things are stored in the org.openhab.core.thing.Thing.json file. The Metadata file is strictly Item metadata (e.g. State description, default list Item widget, etc.).

Do not edit these files while openHAB is running. At best your changes will ignored. Most likely your changes will be overwritten.

And yes, the format is very different. It’s JSON.

Probably not. If it’s ignoring your escapes for the outgoing message format, it’ll probably ignore it in an outgoing message transformation too. But it’s worth a try. But you’ll probably want to use a JS transform. I can’t see how a REGEX will help here. All REGEX can do in OH is extract part of a String. It can’t replace or construct a new String.