MQTT 2 OpenHAB - How to configure a simple switch?

(Max) #1

hello, I have created a rf2mqtt gateway and now I would like to integrate it to my openhab.
I installed the mqtt embedded broker, I tried to do some configuration but it does not work.
I would like to start over simply starting from the MQTT commands sent and received in order to turn on or off a switch.

The messages I receive and transmit to the switch are:
{“Value”: 3461392, “protocol”: 1, “length”: 24, “delay”: 469}
{“Value”: 3461379, “protocol”: 1, “length”: 24, “delay”: 468}

the first turns on, the second goes off.

Depending on the TOPIC the message updates the status on openhab, if published in

home / OpenMQTTGateway / commands / 433toMQTT

or it acts on the switch if published in

home / OpenMQTTGateway / commands / MQTTto433

You can help me, I read the tutorial but honestly it is not very clear to me.

Thank you

1 Like

(Rich Koshak) #2

Since you are writing the gateway yourself, you should consider implementing the Homie standard for MQTT. This will let the MQTT2 binding automatically discover your gateway and create your Things.

What wasn’t clear to you? I can help but I’d just recreate the tutorial wasting my time and getting you no closer to understanding.

At a very high level:

  1. Create a MQTT Broker Thing and configure to connect to the broker
  2. Create a Generic MQTT Thing to represent each device
  3. Create a Channel on the MQTT Thing to publish/subscribe to the desired topics
  4. Link those Channels to Items for use within OH

(1technophile) #3

Here is some examples into the doc:


(Max) #4

Hi! I succeeded!
Yesterday I finally solved the problem of the RF receiver, now I have a gateway that takes radiofrequency signals and sends them as mqtt messages with a homie format. I created the binding, I created the item, I created a channel, I created a panel in hubpannel with a Dummy widget that reads the value of the channel.
At the moment it writes the value transmitted by the radio frequency on screen.

Now I wonder, do I have to change the gateway code to create more channels, one for each sensor, or can I act through openHAB to do that?
I looked at the map file, I would say to my nose that with that I could say that the signals AAAA01, AAAA02, AAAA03 and AAAA01 are part of the 4-channel remote control called AAAA, that the code BBBB01 is the PIR in the corridor, that the senosre CCCC01 indicates the opening of the door and so on …
Am I on the right track? Can I only act on the configuration of openhab items?

Furthermore, the last value read remains written in the MQTT message in the topic on the broker, and here I believe I have to intervene with a script. I should set the event to change the code in the message and trigger a behavior, such as sending a mail or a telegram or a message of another kind, but I’ll see this after creating the ensemble in openhab.

Thanks, bye!


(Rich Koshak) #5

A more standard approach would be to do this in the gateway. Then each of your sensors would be published to a separate topic (per the Homie standard) and then each sensor will have a separate Channel in OH to represent it.

As a general Rule, I always recommend stuff like this to be implemented in the edge device, not in OH.

But OH can handle this too. But it will require you to write Rules to parse the messages and update Items based on the contents of the message. This almost always requires long switch statements and the requirement of updating Rules every time you add a new device.

That’s because whatever is sending the message has set the retain flag to true. When the retain flag is true, the message stays on the topic and when a client subscribes to the topic, they will receive the last retained message. To change this behavior, you need to change your publisher to set the retain flag to false.


(Max) #6

very clear, thank you very much!


(1technophile) #7

@maddmax1976 I would be interested in having some feedback about your implementation in OpenHAB, could you share your different definitions/configurations?


(Max) #8

Hi, I’m working right now.

Thinking about it, I’m not interested in having an object for each sensor, after all the only interest is to have a feedback of which sensor has activated.

Unfortunately, in the old alarm control unit I can not tell if a door, a window has been opened or if movement has been detected.

With the current gateway configuration, I know the code that triggers the alarm.

I left the configuration of the mqtt binding as it was.

With the rules I can have the information that is missing:

rule "Decode Sensor"
    Item RFGateway_RFSignal changed to 524745 or Item RFGateway_RFSignal changed to 5724096
   sendBroadcastNotification ("Open Entry Door!")

this is just a prototype, and it seems to work.

Once all the alarm isensors are reactivated, I will use a Switch / Case construct (or similar) to specify the broadcast message to send.


(1technophile) #9

I have updated the wiki tutorial if you want to take a look and test it don’t hesitate: