OpenHAB OH3.2 MQTT Packet Publish Delayed

I am running OH3 Rev 3.2 and publish sequential message-stamped string-packets to MQTT Mosquitto every 10-seconds. I have recently noticed that the packet transmissions are delayed by 1 packet. IE, when packet number x is published by OH3, packet number x-1 is instantaneously received by MQTT.fx

The OH3 terminal reports:

16:33:40.871 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'OHOB_WM_String' changed from 08/28/21 @ 04:33:20 PM >
MsgStamp:26587;
00:25;
MsgStamp:26587;
EOS< to 08/28/21 @ 04:33:30 PM >
MsgStamp:26588;
00:25;
MsgStamp:26588;
EOS<

The OH3 rule code is:

OHOB_WM_String.postUpdate(OHOB_String + "\nMsgStamp:" + HS5150_Msg_Counter_Now + ";" + "\n" + "EOS<")
var	mqttActions = getActions("mqtt","mqtt:broker:hsbroker")
mqttActions.publishMQTT("/WatchMan/OHOB_WM_String", OHOB_WM_String.state.toString)

It appears OH3 is not flushing the packet buffer until the subsequent packet buffer is published.

Here is my config:

binding = network,mqtt,wemo,exec,mail

The .items file:

String OHOB_WM_String       "Outbound String[%s]" 						{channel="mqtt:topic:hsbroker:WatchManThing:HS5150ToWM"}	// From OpenHAB to Arduino

The .things:

//	MQTT	##########################################################################################################################
//  This binding has no configuration options ( .cfg file), all configuration is done at the Thing level.
    Bridge mqtt:broker:hsbroker "hsbroker" [ host="10.0.0.9", secure=false ] {
		Thing topic WatchManThing "From WatchMan" 
		{
			Channels:
			Type string: HS5150ToWM			"OpenHAB to Arduino"	    [commandTopic="/WatchMan/OHOB_WM_String"]	// OpenHAB Send  to Arduino
			Type string: WMReplyToHS5150	"OHC_MsgStamp"              [stateTopic="/OHIB/OHM/MsgStamp"]           // Arduino Reply to OpenHAB

Mosquitto MQTT is configured with all default parameters. The only lines are:

listener 1883
allow_anonymous true

Any help would be appreciated.
Thanks in advance.

The problem lies in your rule (excellent decision to show us that up front)

OHOB_WM_String.postUpdate( xxx )
...
mqttActions.publishMQTT("/WatchMan/OHOB_WM_String", OHOB_WM_String.state.toString)

This is doomed to failure.
Items are not variables, do not treat them as such.
postUpdate() is asynchronous. It fires off the update request to the openHAB event bus, where it bumps around updating any rules, bindings or UIs that are interested, eventually updating the actual Item state. Takes a couple of milliseconds.
Your rule does not stop and wait.
Consequently, if you read the Item state back within a few lines you will almost certainly get the old previous state, from before your postUpdate takes effect.

That’s okay, you already know exactly what you posted to it, so use that directly.

var x = some calculation
myItem.postUpdate(x)
mqttActions.publishMQTT(blah, x)

Thank you so much. Your explanation makes perfect sense.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.