How to send command to mqtt broker

I am new with MQTT and wanted to test it with a network Binding.
I installed a mqtt broker and connected it to openhab.
I dont understand how can synchronize the channel with an MQTT topic.
I added a thing which show me if my smartphone is online.
network.items
Switch Smartphone { channel=“network:pingdevice:devicename:online” }
Switch SmartphoneMQTT {mqtt="<[Test123:test:state:default],>[Test123:test:command:*]"}

Now I get the right status in my basic UI. But nothing is send to my topic test. I tested it with MQTT.fx.



You are trying to use the MQTT 1 syntax for your Item but you are using the MQTT 2 binding. It looks like you’ve created a Generic MQTT Thing so all you need to do is Link the Channel to your Item. https://www.openhab.org/docs/configuration/items.html#_2-x-binding-configuration

I’m on a phone can can’t read the screenshots so I can’t comment on whether you have the MQTT Thing configured properly.

I do not understand how you would be able to test MQTT with the Network binding?
In order to use MQTT just use the MQTT binding.

From what do you get the “right status”? Without an item correctly linked to a MQTT thing you can’t be receiving any status from a MQTT thing.
Are you sure about the state-and commandtopic selected in PaperUI? Did you check with MQTTfx? You can subscribe to topics with MQTTFx and send messages as well, that way you can test if you have the correct settings for state- and commandtopic!

I just want to send a message to a topic when the state changes. So when my smartphone gets offline. I want to send a message OFF to a topic . And when my device is online, I want to send a message ON to a MQTT topic. How can I do this? I have correctly used the network binding without MQTT. I linked the item to the MQTT Item. But nothing is send to the mqtt topic.

IMHO you need to use a rule for that like:

rule "MyMQTTRule
when
    Item Smartphone changed
then
    val mqttActions = getActions("mqtt", "mqtt:broker:MQTTBrokerTest")
     mqttActions.publishMQTT("MyTopic", Smartphone.state)
    
end

Please insert your desired Topic instead of “MyTopic”. Could not test ATM, so I’m not sure if the part “Smartphone.state” is correct.

1 Like

@opus
ok thank you. I will try it. So I must do it for each device? Is there no easier way?
The reason for this is that I want to build my own android app which communicates over MQTT to my openhab.

Have a look here:


You should be able to adapt it to your needs, so you would only need one rule per direction. The second rule publish all, publishes all state changes of items in a group.

Why don’t you use the REST API for that?

@opus When I use REST API, I have to poll the state of an item. When I use MQTT I can use a connection which send me an event when the topic get a new message. But when you have a workaround, I am open for advices.

IMHO the openHAB UIs are communicating with openHAB via REST.

You don’t poll with the REST API. Just like you send an MQTT message when you get a new value, you have what ever is generating the value make the REST call to update our commands the Item