Understanding the MQTT support, modeling/design question

I have a PLC that controls the basics of my home (lights on/off). Now I added an MQTT client inside my plc so it can send the actions of my physical switches to an MQTT topic.
I have a topic per physical switch and the action on the button is sent to the MQTT topic. I have 3 actions. SINGLE, DOUBLE, and LONG. reflecting how the button is pressed.

When I look into the MQTT support in OpenHab, I see that it is rather event-based and not command-based. you can send commands to the broker, but not really receive them?
I’m now thinking of modeling the broker by defining 3 switches per topic and each is activated based on the state change that matches the switch.
I think I will be able to get this to work, but is this really the way to model this?

thank you

Thing topic living_1_6 “Living Room contact 1, button 6” {
Type switch : single “single” [ stateTopic="/klskmp/beneden/living/1/6" ]
Type switch : double “double” [ stateTopic="/klskmp/beneden/living/1/6"]
Type switch : long “long” [ stateTopic="/klskmp/beneden/living/1/6"]

MQTT is a messaging protocol. A message is sent, openHAB receives it and does something in response. That’s pretty much the nature of MQTT in general. On top of that, openHAB is also very much event driven.

But at that point I lose you. A command is definitely a type of event and openHAB very much supports commands as a type of event. The MQTT binding can process messages received from a topic as an update or as a command. There is a toggle in the Generic MQTT Thing to treat incoming messages as a command (in the UI you may need to check “show advanced” and it will appear as “Is Command”.

It’s as good as any other way. It’s probably not how I would do it though. Instead I’d have one topic and the message published would be “single”, “double”, or “long”. In the config for the MQTT Thing that subscribes to that you may need to transform those messages to an appropriate openHAB command or update. How to do that will largely depend on what type the Channel is.

A Text Channel may make sense and you can keep the original messages as they are. You could use a Number Item but all that would do is obfuscate what’s going on. If these three events correspond with something specific (e.g. single == INCREASE, double == DECREASE, long == STOP) you might link it to something like a Dimmer Item.

1 Like

the stateTopic is the topic /klskmp/beneden/living/1/6 you receive the state payload on

so in your example you will just get errors in log.

Create a channel

Type string : C1B6      [ stateTopic ="/klskmp/beneden/living/1/6"]

This channel will change to single, double or long