MQTT2: How to publish a topic with retain?

I tried to search through the documentation but I did not find an answer.
I would like to publish a topic using mqtt binding 2.4, but I must specify that the topic should be retained by the broker. In this way a subscriber can access the value of the argument even if it didn’t yet subscribed the topic at the time of its publication.
What I am doing in a rule is something like this:

    val actions = getActions ("mqtt", "mqtt:broker:mybroker")
    actions.publishMQTT ("OH2/mytopic", "valuetobepublished")

where I want the topic OH2/mytopic to be published with the retain flag set.
How can I do this?

Unfortunately action message cannot be retained as far as I know.
The binding can though.

I know that I can set this way a thing:

    Thing topic mytopic "Description" {
        Type string : mytopicstring "Description2" [
            stateTopic="OH2/mytopic",
            retained=true
        ]
    }

but I don’t know haw to put in relation the above setting with

actions.publishMQTT ("OH2/mytopic", "valuetobepublished")

Link the Thing-Channel to an Item, use sendCommand to change the state of your item and it will be published.

1 Like

Hi J-N-K,
I tried what you suggested, but it didn’t work.

I made a thing:

    Thing topic mytopic "Description"  {
        Type string : mytopicstring "Description2" [
            stateTopic="OH2/mytopic",
            retained=true
        ]
    }

Then I linked via Paper UI it to the following item:

String mytestItem "My test item" { channel="mqtt:topic:mytopic:mytopicstring" }

Then i wrote a rule:

rule "TestRule"
when
     Item test changed
then
    if (test.state == ON) {
        sendCommand(mytestItem, "{ \"status\":\"Armed\", \"User\":\"John\" }")
    } else {
        sendCommand(mytestItem, "{ \"status\":\"Disarmed\", \"User\":\"John\" }")
    }
end

The item test is a switch I put on my sitemap.
When I activate the swicth test the value of the item mytestItem changes, but it is not published to mqtt.

Where I was wrong?

Is your thing online?

Yep!
It is ONLINE in Paper UI

Your thing doesn’t have a command topic

Now the thing should be corrected:

    Thing topic mytopic "Description" {
        Type string : mytopicstring "Description2" [
            stateTopic="OH2/mytopic",
            commandTopic="OH2/mytopic",
            retained=true
        ]
    }

but it still doesn’t function.
When I change the item mytestItem the log reports that the value is changed but the mqtt broker does not receive a publish.
I don’t know if this is significant but the log reports the changin in the mytestItem followed by a line saying:

18:45:57.188 [INFO ] [arthome.event.ItemStatePredictedEvent] - mytestItem predicted to become NULL

BTW: the mqtt2 is functioning because I can receive message from topics I’ve subscribed to and I can change the status of switches operated by mqtt.

I think that‘s the way I do it (except I use PaperUI for configuration). I‘ll check tomorrow if I did something different.

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?