MQTT2: How to publish a topic with retain?

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