logo elektroda
logo elektroda
X
logo elektroda

How to publish MQTT string/JSON to control other devices (OBK, Tasmota, Zigbee) from OpenBeken?

p.kaczmarek2  2 2298 Cool? (+2)
📢 Listen (AI):

TL;DR

  • OpenBeken MQTT scripting can publish strings or JSON to other devices in the MQTT network, including OBK relays and Zigbee2MQTT endpoints.
  • The core command is `publish [TopicName] [Payload] [bRawMode]`, and `bRawMode` controls whether the OBK topic prefix is stripped.
  • A relay on `Uticnica2` is controlled with `publish Uticnica2/1/set 0 1`, where `1` is the channel index and payload `0`/`1` sets off/on.
  • A Zigbee2MQTT TS0012 switch accepts escaped JSON, for example `publish zigbee2mqtt/test2/set ... {"state_left":"TOGGLE"} ...`, proving OBK can handle quoted payloads.
  • The published commands changed target device states directly, avoiding Home Assistant automations and showing OBK can drive other MQTT devices itself.
Generated by the language model.
Screenshot of WinTest_3145CAFF command line tool with MQTT command.
OpenBeken provides a scriptable set of MQTT-related commands that can be used to publish data not only directly to Home Assistant, but also to other devices in the MQTT network. This way you can for example control a Zigbee switch directly from OBK, without writing any automations in HA. In this tutorial I will show you how you can do this in few simple steps.

Basic requirements
This tutorial assumes that you already have OBK device and that your device is connected to MQTT. I am using Home Assistant for this purpose.


OBK publish command
OBK provides multiple commands for publishing data, but most important is just publish command.

publish [TopicName] [Payload] [bRawMode]

First argument is MQTT topic name, second is payload, third is optional, used to strip prepended OBK device name to the topic name.
The payload can include escaped quotation marks, I will show an example soon.
The bRawMode disabled prepending OBK prefix, let's see how it looks in both cases:
Command tool interface with an example MQTT command.
Screenshot of the command input tool for OpenBeken.
Can you spot the difference? Yes, one command has "WindowsOBK/" and "/get" added.
For more information on commands, see: https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/commands.md

Controlling OBK device from OBK
Let's say that your target device is called "Uticnica2". It's OBK device, so it follows naming here:
https://github.com/openshwprojects/OpenBK7231T_App/blob/main/docs/mqttTopics.md
It's important to remember that short name is used for MQTT:
Screenshot of the device name change interface in OpenBeken.
So, the publish to control relay will use name Uticnica2/1/set.
1 there stands for channel index.
Then, payload can be either 0 or 1, to set it on or off.
So the full command is:

publish Uticnica2/1/set 0 1

Let's run it:
OpenBeken thermostat tool panel with MQTT command
The following command should change the state of target device accordingly.
Screenshot of the OpenBeken device interface named Test_uticnica_2 showing measurement data and status.

Controlling Zigbee device from OBK
Now let's try something more seemingly complicated. Let's control a Zigbee device from OBK.
First, let's look up the topics of Zigbee2mqtt device I have:
https://www.zigbee2mqtt.io/devices/TS0012.html
As you can see, there is a clear instruction how to control it, but can OBK do it?
Excerpt from documentation on controlling a switch using MQTT.
Let's try executing:

publish zigbee2mqtt/test2/set " {\"state_left\" : \"TOGGLE\"}" 1

Screenshot showing the command input tool in WinTest_3145CAFF.
The state of Zigbee device was changed. It works correctly.

Summary
As you can see, it's very easy to publish data via OBK scripting command. The method I have presented above is working even for JSON commands with the quotation marks, all thanks to the possiblity of escaping the quotes. With this mechanism, you can create advanced automations without involving the HA scenes system itself. I hope you'll find it useful.
PS: In the next topic we will check if it's possible to publish a file from LittleFS, we will also look into variables expansion

About Author
p.kaczmarek2
p.kaczmarek2 wrote 14439 posts with rating 12405 , helped 650 times. Been with us since 2014 year.

Comments

DeDaMrAz 08 May 2024 01:25

To add an example to this: publish <short name>/<channel number>/set <VALUE 0 or 1> 1 publish Uticnica2/1/set 0 1 [Read more]

p.kaczmarek2 08 May 2024 01:29

Next idea - as some of the readers may know, typical Home Assistant Discovery looks somewhat like this: Message 23 received on homeassistant/sensor/multifunction_alarm_OpenBK7231N_E1552B06_sensor_2/config... [Read more]

FAQ

TL;DR: With 3 parameters in publish, OpenBeken can send plain strings or escaped JSON to other MQTT devices, and "it works correctly" for direct OBK-to-OBK and Zigbee2MQTT control. This FAQ helps OpenBeken users send MQTT commands without building Home Assistant automations first. [#21073836]

Why it matters: Direct MQTT publishing lets one OpenBeken device control another OBK, Zigbee2MQTT, or similar target faster and with fewer moving parts. [#21073836]

Metoda Format tematu Ładunek Efekt
publish bez raw mode OBK dopisuje własny prefiks urządzenia Tekst lub JSON Dobre do tematów lokalnych OBK
publish z bRawMode=1 Wysyła dokładnie wskazany temat Tekst lub JSON Dobre do sterowania innymi urządzeniami MQTT
Sterowanie innym OBK Uticnica2/1/set 0 lub 1 Wyłącza lub włącza kanał przekaźnika
Sterowanie Zigbee2MQTT zigbee2mqtt/test2/set JSON z escape Zmienia stan urządzenia Zigbee

Key insight: Najważniejsze jest poprawne użycie krótkiej nazwy MQTT oraz bRawMode=1. Bez tego OpenBeken może zmienić temat przez dodanie własnego prefiksu i komenda nie trafi do celu. [#21073836]

Quick Facts

  • Składnia polecenia ma 3 argumenty: publish [TopicName] [Payload] [bRawMode]; trzeci parametr jest opcjonalny i steruje surowym wysłaniem tematu. [#21073836]
  • Dla przekaźnika OBK przykład używa tematu Uticnica2/1/set, gdzie 1 oznacza indeks kanału. [#21073836]
  • Do przełączania kanału przekaźnika używa się wartości 0 lub 1 jako ładunku MQTT. [#21073840]
  • Przykład Home Assistant Discovery zawiera m.in. qos: 1, jednostkę Hz, temat stanu ~/2/get i przelicznik *`0.1** wval_tpl`. [#21073842]

How do I use the OpenBeken publish command to send an MQTT message to another device on the network?

Use publish [TopicName] [Payload] [bRawMode]. For direct delivery to another device, set bRawMode to 1 so OpenBeken sends the exact topic you typed. For example, publish Uticnica2/1/set 0 1 sends 0 to channel 1 of the target device. 1. Pick the target topic. 2. Set the payload. 3. Add 1 for raw mode. [#21073836]

What does the bRawMode parameter do in OpenBeken's publish command, and how does it change the MQTT topic that gets sent?

bRawMode stops OpenBeken from prepending its own MQTT prefix to the topic. When raw mode is off, the firmware adds its device prefix, such as WindowsOBK/, and also appends OBK-style path parts like /get in the shown example. When bRawMode=1, the broker receives the topic exactly as you entered it. [#21073836]

How can I control one OpenBeken device from another using a topic like Uticnica2/1/set?

Publish directly to the target OBK relay topic with raw mode enabled. The thread’s working example is publish Uticnica2/1/set 0 1, where Uticnica2 is the target short name, 1 is the channel index, and 0 turns that channel off. The same structure works for on or off control of another OpenBeken device. [#21073836]

Which device name does OpenBeken use in MQTT topics: the full name or the short name?

OpenBeken uses the short name in MQTT topics. The thread explicitly notes that the short name is important for MQTT, and the relay example uses Uticnica2/1/set rather than a longer display-style name. If you use the wrong name form, the target topic will not match the device subscription. [#21073836]

What payload values should I publish to switch an OpenBeken relay channel on or off over MQTT?

Publish 0 or 1. In the thread’s relay example, those are the two valid payload values for channel control, and the sample command publish Uticnica2/1/set 0 1 shows 0 used to switch the relay off. The added forum example repeats the same rule for a generic <channel number>/set topic. [#21073840]

How do I publish a JSON payload with escaped quotation marks from OpenBeken script commands?

Wrap the JSON so the quotation marks are escaped inside the payload string. The working example publishes to zigbee2mqtt/test2/set with an escaped JSON body containing state_left and TOGGLE, then adds raw mode 1. OpenBeken supports escaped quotation marks in payload, which makes JSON publishing possible from script commands. [#21073836]

What is Zigbee2MQTT, and how does it expose MQTT topics for controlling Zigbee devices?

"Zigbee2MQTT" is MQTT integration software that bridges Zigbee devices to MQTT topics, exposing device-specific command and state topics for control and monitoring. In the thread, the Zigbee2MQTT device example uses the topic zigbee2mqtt/test2/set, showing that the Zigbee device can be controlled by publishing JSON to a documented MQTT path. [#21073836]

How can I control a Zigbee2MQTT device such as TS0012 from OpenBeken using a publish command?

Send the Zigbee2MQTT command topic and an escaped JSON payload with raw mode enabled. The shown command is publish zigbee2mqtt/test2/set " {\"state_left\" : \"TOGGLE\"}" 1, which targets a TS0012-style example and toggles the left state. The author confirms the result: "The state of Zigbee device was changed. It works correctly." [#21073836]

Why does OpenBeken prepend its own device prefix to MQTT topics unless raw mode is enabled?

OpenBeken does that to keep its normal MQTT messaging aligned with its own device topic structure. The thread shows a non-raw example where OBK adds WindowsOBK/ and /get, which is useful for native OBK topics but wrong for external targets. Raw mode exists so you can bypass that behavior and hit exact third-party topics. [#21073836]

What is Home Assistant MQTT Discovery, and what information is typically included in its config payload?

Home Assistant MQTT Discovery is a JSON config message that describes a device, entity, topics, and metadata for automatic setup. The example payload includes device IDs, name, firmware string 1215_merge_dd32e251ff32, manufacturer Beken Corporation, model BK7231N, config URL, qos: 1, unit Hz, state topic ~/2/get, and a value template multiplying by 0.1. [#21073842]

OpenBeken publish vs Home Assistant automations: which approach is better for directly controlling MQTT devices?

Direct publish is better when you want one device to control another without adding Home Assistant automations. The thread’s main point is that OBK can control another OBK or a Zigbee2MQTT device directly over MQTT, including JSON payloads, so you can build local logic first. Home Assistant remains useful, but it is not required for these direct command paths. [#21073836]

What are the most common mistakes when publishing from OpenBeken to OBK, Tasmota, or Zigbee2MQTT topics?

The most common mistakes are wrong topic format, wrong device name, missing raw mode, and bad JSON escaping. The thread shows that OBK uses the short name for MQTT and that bRawMode=1 matters when targeting external topics. If you omit raw mode or escape quotes incorrectly, the broker may receive a changed topic or invalid JSON, and the target device will not react. [#21073836]

How should I format MQTT topics and channel numbers when sending commands from OpenBeken to another relay device?

Format the topic as <short name>/<channel number>/set. The concrete example is Uticnica2/1/set, where Uticnica2 is the short MQTT name and 1 is the relay channel index. Then publish 0 or 1 as the payload to switch that channel off or on. This format is the clearest pattern shown for OBK relay control. [#21073840]

What's the best way to test whether an OpenBeken publish command actually reached the target MQTT device?

Run a simple on/off command and verify the target device changes state immediately. The thread tests this with publish Uticnica2/1/set 0 1 and then confirms the target state changed accordingly. For a Zigbee path, the author also executed a JSON TOGGLE command and observed the Zigbee device state change, which proves the message reached the target topic. [#21073836]

How could publishFile with LittleFS be used in OpenBeken to send large Home Assistant discovery JSON payloads?

It could store a long discovery JSON in LittleFS and publish that file instead of typing the full payload into a command line. The thread shows a large Home Assistant Discovery example with nested fields, qos: 1, Hz, and val_tpl, then states that work is in progress on publishFile. That would fit large, reusable config payloads much better than inline escaped JSON. [#21073842]
Generated by the language model.
%}