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?
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.
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 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 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
Is there already a solution available?
I already complained about the same stuff a few days earlier than you did
(OH 2.4 with MQTT - retained messages)