MQTT2: How to publish a topic with retain?

I will add another action for retained messages. … if someone creates a bug report, at least. Otherwise I forget ^^.

Cheers,
David

I don’t use the state-Topic, only the command-Topic. Then it works for me when I use postUpdate to the item.

I tried to use only commandTopic, but either I do a postUpdate or a sendCommand nothing is published to mqtt.

The behaviour however is different among the two statements.
I set up the switch test to change with a rule the item mytestItem:

rule "Test switch changed"
when
     Item test changed
then
    if (test.state == ON) {
        sendCommand(mytestItem, "testvalue1")
        //postUpdate(mytestItem, "testvalue1")
    } else {
        sendCommand(mytestItem, "testvalue2")
        //postUpdate(mytestItem, "testvalue2")
    }

Using the rule as written above (with sendCommand) the log shows the following:

12:59:53.733 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'test' received command ON
12:59:53.759 [INFO ] [smarthome.event.ItemStateChangedEvent] - test changed from OFF to ON
12:59:53.770 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'mytestItem' received command testvalue1
12:59:53.781 [INFO ] [arthome.event.ItemStatePredictedEvent] - mytestItem predicted to become testvalue1
12:59:55.646 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'test' received command OFF
12:59:55.663 [INFO ] [smarthome.event.ItemStateChangedEvent] - test changed from ON to OFF
12:59:55.677 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'mytestItem' received command testvalue2
12:59:55.687 [INFO ] [arthome.event.ItemStatePredictedEvent] - mytestItem predicted to become testvalue1

Using the rule with postUpdate the log shows:

12:57:25.657 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'test' received command ON
12:57:25.688 [INFO ] [smarthome.event.ItemStateChangedEvent] - test changed from OFF to ON
12:57:25.710 [INFO ] [smarthome.event.ItemStateChangedEvent] - mytestItem changed from testvalue2 to testvalue1
12:57:27.279 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'test' received command OFF
12:57:27.304 [INFO ] [smarthome.event.ItemStateChangedEvent] - test changed from ON to OFF
12:57:27.408 [INFO ] [smarthome.event.ItemStateChangedEvent] - mytestItem changed from testvalue1 to testvalue2

It seems that with sendCommand the value of the item is “predicted” to became always the last updated value (in this case testvalue1, from a previous test), while with postUpdate the value in OH is changed accordingly to the statement.

I will try to open one… but consider it’s my first one so if something is non correctly stated please let me know :smile:

Are you sure you linked the item to the channel?

Hi @David_Graeff , I created the bug report #4466.

As written in a previous post I defined an item with a .items file and linked it this way:

I created the bug report in the wrong place.
I should use the Eclipse SmartHome issue tracker for reporting MQTT Binding issues.
The new bug report number is #6745
Sorry :roll_eyes:

1 Like

Is there already a solution available?
I already complained about the same stuff a few days earlier than you did :slight_smile:
(OH 2.4 with MQTT - retained messages)

Yes, if you add retain=true to the thing’s channel.

It is not available with the MQTT action.

I avoid MQTT retain like the plague. What do you use it for? And how is it useful?

Example: in my summer house, there’s an ESP8266 uC that is responsible for garden watering and sensor measuring. The data is sent via MQTT to my raspberry pi where the broker “lives”. However, as it’s winter in Germany, garden watering is currently not of interest :slight_smile: So the uC wakes up every five minutes in order to publish sensor data via MQTT. But additionaly the controller subscribes to a topic openhab2/out/espDeepSleep to know, if it’s allowed to fall back asleep. In OH I’ve defined a switch that writes into this topic. So I can use my smartphone to tell the uC what to do. In summer months this is important, as I don’t want to wait up to five minutes when I need water instantly.

Ok, still don’t know why you need retain for that. But I like the idea. Looking for ideas for watering the garden…

Set up is:
GPIOs of the uC are used to trigger relais-channels that switch 24V AC to valves and another relais, that turns on/off the pump. Communication between raspi & uC is MQTT/WLAN and as the connection could break, my switch command is like ON;;;. So the uC knows, without MQTT input, when it’s time to shutdown the channels.

Back to topic:
What’s your solution? This is my config and it doesn’t work. Tried it with retained and/or qos.

Bridge mqtt:broker:MQTTretain [ host="192.168.2.49", secure=false, username="raspi", password="xxx", clientID="openHAB2retain" ] {
	Thing mqtt:topic:uc_deep_sleep "uC-Deep-Sleep" {
	Channels:
		Type switch: esp_sleep_mqtt "ESP8266-DeepSleep" [ stateTopic="openhab2/in/EspDeepSleep", commandTopic="openhab2/out/EspStandby", on="Standby;", off="NoStandby;", retained=true, qos=1 ]
	}
}

MQTT-messages produced are != retained and != qos=1 (used MQTT.fx to look up).

Did you try to set retain=true in the Bridge Thing itself?

I think I tried - already tried a couple of things.
I don’t get why this one has not been corrected yet in the binding-documentation (https://www.openhab.org/addons/bindings/mqtt/).

a) It states the switch is named retainMessages
b) Doesn’t give an example
c) States it’s part of the bridge whereas David wrote it belongs to channel-configuration

Well, I’ll give it (again) a try.

It is part of the channel configuration and is called retainMessage or retainMessages, should be documented in the mqtt.generic binding.

I don’t get it…

Hm. Ok. I’m not at my pc. The problem is that code changed within the last four weeks. If the code says it is retained than it is retained and I was wrong with retainMessage.

sorry for reviving this old topic but was this ever solved?
as in how do you convince publishMQTT() to publish with retained flagg on?

Any solution to that?

I’ve been trying all options found on the forums and even some I invented myself and I can’t figure out how to publish a message using retain flag set to true…

I tried text config, switched to Paper UI config, tried publishing using an Item, tried publishing from a rule (publishMQTT()), …

Anyone found out how to publish retain=true message using latest MQTTv2? (I’m on openhab2.5.1-2)

1 Like