Serial Binding for periodic requests


I have a meter reader which is sending data via serial port device. So I guess the serial binding would be great.

But, there are different wanted strings in the stream (something like “xxxSOMEDATA_HERExxxayyyyuuOTHER_DATA_HEREoooxxx” and so on), so that I think I can’t use the regex. If I understand correctly, I could use several String items with serial binding and the different regexes, but I guess this would cause several serial connections?

Also as far as I remember (currently I have a perl script from FHEM reading these values), the meter is sending data to serial constantly. But I would like to only read and log these data every 5 minutes. So I guess I could save some cpu power if I only connect every 5 mins to the serial port, read the data and disconnect again.

Is there already a suitable binding for that usecase? Should I write my own binding (I’m a java developer, although not familiar with openHAB dev) or use a shell script to read the data and report back to openHAB (but I would like to have as much as possible integrated in openHAB to keep the system small and maintainable).

According to the wiki page you can populate multiple Items from one serial binding with the 1.8 version of the serial binding. That will let you pull SOMEDATA_HERE into one Item and OTHER_DATA_HEAR into another Item without needing multiple connections, if I read it correctly. So as long as you upgrade to the 1.8 binding (downloaded from the nightly builds) that won’t be a problem.

The current serial binding does not support this but you could handle the logging of the data every five minutes with a cron triggered rule and call to the Item’s persist method (assumes persistence is set up and the Items in question are not configured to save every change). The binding does not support connecting to the serial port periodically. That is really an aberrant use case for serial connections so I’m not surprised it isn’t supports.

I would not worry about the CPU usage. Even on something like a RasPi 2 the CPU use would be negligible.

I am a very strong advocate for only writing a new binding as a last resort. There are over 120 officially supported bindings (based on a count from the wiki) already and in my opinion forking and writing more bindings to support one little use case here or another one there hurts the OH community more than helps it.

This is a very common approach actually. It is particularly useful for environments with remote sensors and actuators (i.e. sensors and actuators not directly wired into the openHAB server). For sensors,the most common approach, though not the sole approach, is to have a script or program that reads the data and publishes the updates over MQTT. This requires setting up an MQTT broker (mosquitto is very popular), using an MQTT client library (paho is incredibly easy to use and is available in lots of languages), and the MQTT Binding.

Personally I would recommend this approach as it will allow you to position your OH server beyond wire distance from your meter should you decide later being that close is unworkable. It also sets you up for expansion later.

Another approach could be a shell script which gets called every five minutes using the Exec binding which calls a shell script (or Python or PHP or your scripting language of choice) which connects, grabs the data, and returns the data. This would follow your desired use case a little more closely.

Is there an example somewhere? I can’t find that. Only that the regex is mentioned for 1.8 (I’m still on official 1.7.1, but afaik 1.8 is around the corner, so I wouldn’t have a problem switching), not a code example on how to get multiple items from one reading.

I have a HP ProLiant Gen8 running, so I guess it’s really not a problem. :smile: But I think it is unnecessary to read and regex the input stream all the time, although I only need it every 5 mins.

Actually the meter reader is the only thing which is not directly supported by openHAB. To have another script with cron and so on is another layer, another bug source, another thing to monitor…

But in this case this might be the easiest way, maybe I could also reuse my working perl code to read the data and let the script post the data to the rest api of openHAB or something like this.

Thank you for your help.

REGEX is supported for the older binding as well. What 1.8 provides is the ability to have more than one Item populated from the same socket without having multiple connections to the same socket.

All you need to do is grab the 1.8 version of the serial binding from the nightly builds and replace the 1.7.1 version of the binding in your current install. You don’t need to upgrade your whole install to 1.8 to use the newer binding.

I don’t think you need to do anything different or special. Just define your Items and bindings as is shows on the wiki page but have a different REGEX clause for each Item to extract the data you want for each item. So it would be something like:

String SomeData { serial="/dev/ttyS0,REGEX(xxx(SOMEDATA_HERE pattern)xxx" }
String OtherData { serial="/dev/ttyS0,REGEX(uuu(OTHER_DATA_HERE pattern)ooo" }

The binding will handle populating both Items from the same socket stream.

In my opinion my time is worth more than the CPU’s time. So unless there are other constraints I’d rather have the CPU needlessly busy and throw away unneeded data then going out of my way to avoid a couple percent load on the CPU.

Still seems easier than writing a new binding. :wink:


I want to give serial binding a try. But as docs and code read, the databits and parity are hardcoded. But I need 7 databits, 1 stopbit and parity even… so currently I get only gibberish.

Maybe I’ll add that to the binding and could contribute to a project the first time. :smile:

Before diving in, make sure it hasn’t already been implemented in 1.8 or whether there is an open issue for that.