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.
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.
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.
@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.
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.
@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.
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