IoT
openHAB 3.0 - update of an item via MQTT messages

openHAB 3.0 - update of an item via MQTT messages

The purpose of this tutorial is to show how to update the state of an item under openHAB 3.0 when receiving MQTT messages. For this, we will be inspired by this tutorial [OH3] Tasmota relay via MQTT (Sonoff Basic with optional DHT22) to « link » an Item to the testswitch002 topic of our MQTT broker. However, we must adapt it to our needs:

  • the MQTT broker is already configured in openHAB (cf. my previous tutorial openHAB 3.0 - how to create a switch that publishes its state via MQTT messages)
  • the Item that we are going to create will not have to order a real device (eg: via sending MQTT messages); it will only be a « virtual » Switch in the OH3 interface
  • the naming of our topics and the contents of the messages are very simplified compared to real MQTT sensors…

The configuration used is the same as in openHAB 3.0 - how to create a switch that publishes its state via MQTT messages and, here too, the various steps were entirely carried out via the openHAB3 graphic interface.


Create a Generic MQTT Thing

  • SettingsThingsBlue « + »MQTT BindingGeneric MQTT Thing
    • Label = Generic MQTT Thing 001
    • Parent Bridge = MQTT Broker (cf. previous tests here)
  • the Generic MQTT Thing will appear in your list of Things and is ONLINE

Generic MQTT Thing 001 - 1
Generic MQTT Thing 001 - 1

Generic MQTT Thing 001 - 2
Generic MQTT Thing 001 - 2


Configure openHAB3 to read the values received on the MQTT topic

  • create a Channel with the Switch type: SettingsThingsGeneric MQTT Thing 001ChannelsAdd Channel
    • Id = mqttchanneltestswitch002
    • Label = MQTT Channel Test Switch 002, On/Off Switch
    • Channel type = On/Off Switch
    • MQTT State Topic = testswitch002
    • click on Create

MQTT Channel Test Switch 002 - 1
MQTT Channel Test Switch 002 - 1

MQTT Channel Test Switch 002 - 2
MQTT Channel Test Switch 002 - 2

MQTT Channel Test Switch 002 - 3
MQTT Channel Test Switch 002 - 3

  • create an Item linked to this Channel: SettingsThingsGeneric MQTT Thing 001ChannelsAdd Points to Model
    • select the previously created Channel → MQTT Channel Test Switch 002, On/Off Switch
    • click on Add to Model
    • the Point thus created appears on our Channel

MQTT Channel Test Switch 002 Point - 1
MQTT Channel Test Switch 002 Point - 1

MQTT Channel Test Switch 002 Point - 2
MQTT Channel Test Switch 002 Point - 2

MQTT Channel Test Switch 002 Point - 3
MQTT Channel Test Switch 002 Point - 3

  • the Point we have just created appears in the list of Items; we can then use any MQTT client to send the ON or OFF messages to the testswitch002 topic → the value of this Item is updated

MQTT Channel Test Switch 002 Item - 1
MQTT Channel Test Switch 002 Item - 1

MQTT Channel Test Switch 002 Item - 2
MQTT Channel Test Switch 002 Item - 2

Notes


Conclusion

After various tests carried out from information found here and there on the Internet (sometimes relating to other versions of openHAB, sometimes using text file configuration), here is a « simple » method for openHAB3 to link the state of an item to the content of MQTT messages received on a particular topic. If you have any comments or suggestions, feel free to contact me on manuel.munier@univ-pau.fr.