Thursday, 20 October 2016

openHAB debugger

Having previously set up the infrastructure for connecting my legacy Comfort system to openHab, I've started working with openHab items, sitemaps and rules to respond to sensor status updates. In doing so, I've ben working with scripts in openHAB and have been a bit frustrated by the level of debugging available. With MQTT and Node-Red in place, I wrote a simple facility to easily trace code objects and values.


The first step was to set up an item in the items file to output strings to a particular MQTT channel;

String MQTTDebugger "[%s]" {mqtt=">[myMQTTBroker:myHome/debugger:command:*:default]"}

This sets up an item named 'MQTTDebugger' that uses my previously set up MQTT binding to send to the 'myHome/debugger' channel.

 Next, I set up an item in the items file to store debug text. (acts like a variable);

String DebugString "[%s]"

Then, I set up a 'System.rules' file which I will use to store startup scripts and these general purpose or utility scripts. Here's the rule that implements debugging;

rule "Debugger"
when
Item DebugString changed
then
var msg = DebugString.state.toString
if (msg != ""){
MQTTDebugger.sendCommand(msg)
DebugString.postUpdate("")
}
end

This listens for any change to the value of the DebugString item and, provided that it's not an empty string, sends it the myHome/debugger channel via the MQTTDebugger item. It then clears the debug string, ready for the next call.

With all this in place, it's then possible to dump debug text to MQTT using the following in any rules file;

DebugString.postUpdate ("data to trace here")

The way this works is by setting a string in the DebugString item and having the Debugger rule listen out for changes to that item. Once found, the string is sent to the MQTT channel and the DebugString item is reset.

This can be observed in Node-Red with a very simple config which posts the activity on the myHome/debugger channel to the Node-Red debug window;
In this way, I've got realtime tracing of specified openHAB values in a separate window. Works great.

No comments: