MQTT 2 OpenHAB - How to configure a simple switch?

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

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

Here is some examples into the doc:

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!

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.

very clear, thank you very much!

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

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"
When
    Item RFGateway_RFSignal changed to 524745 or Item RFGateway_RFSignal changed to 5724096
then
   sendBroadcastNotification ("Open Entry Door!")
end

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.

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

Hello @1technophile,
I am facing a similar problem as @maddmax1976. May I ask you for your help?
The steps I did so far are as follows:

  1. Installed everything according to your manual.

  2. Created the OpenMQTTGateway in OpenHAB, resulting in working setup.
    I know this because the commands I send from my Light Switch Remote Control to the 433 MHz receiver appear well in the Serial Monitor and matches the OpenHAB MQTT Config Topic

    +

     20:09:46.246 -> N: Received json : {"value":13081585,"protocol":1,"length":24,"delay":368}
    

What I know at this point is: 13081585 is the value for switching on and off the light.
So far so good. What I am struggling at now, is how to get this value from OpenHAB back to the Transmitter.
What I tried so far is as follows:

  1. Created a Generic MQTT item, following this instruction.
    image

When I press this button, I get an expected result in OpenHAB log:

2021-01-24 21:10:27.351 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'Switch1' received command ON
2021-01-24 21:10:27.370 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'Switch1' predicted to become ON
2021-01-24 21:10:27.402 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Switch1' changed from OFF to ON

But what I would expect is that the light switches on at that time.
I know this is not hardware related, as I would expect something in my serial monitor which says that the value has been transmitted. The Serial Monitor does not say anything about a transmitted value.

Something like this is what I am looking for in my Serial Monitor, but I don’t see it, what reflects that something in the chain goes wrong…

xx.xx.xx -> N: Sent json : {"value":13081585,"protocol":1,"length":24,"delay":368}

Hopefully this is clear to you. Thank in advance!

EDIT: Actually, you may be hitting this issue:

Try using a different device or browser…

Original content below, just in case:


Unless you tell openHAB to, it won’t send your value inside a JSON string, which seems to be what your device would expect.

You will need to use the formatBeforePublish option, though I don’t know what the field is called in the user interface. Once you find it, try:

{"value":%s,"protocol":1,"length":24,"delay":368}

Thanks for your help. I tested a bit more, using your suggestion and found the solution.
For others, here it is:

MQTT State Topic

home/OpenMQTTGateway_ESP8266_RF/433toMQTT

MQTT Command Topic

home/OpenMQTTGateway_ESP8266_RF/commands/MQTTto433

Custom On/Open Value

{"value":13081588,"protocol":1,"length":24,"delay":365}

Custom Off/Closed Value

{"value":13081588,"protocol":1,"length":24,"delay":365}

1 Like

Nice, if you are interested in updating the OpenHAB/OMG integration doc, do not hesitate: