logo elektroda
logo elektroda
X
logo elektroda

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

p.kaczmarek2 1776 2
ADVERTISEMENT
  • 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

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 11834 posts with rating 9932, helped 566 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #3 21073842
    p.kaczmarek2
    Moderator Smart Home
    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 at 12:48 AM:
    
    {
    "dev": {
    "ids": [
    "multifunction_alarm_OpenBK7231N_E1552B06"
    ],
    "name": "multifunction_alarm_obkE1552B06",
    "sw": "1215_merge_dd32e251ff32",
    "mf": "Beken Corporation",
    "mdl": "BK7231N",
    "cu": "http://192.168.11.110/index"
    },
    "name": "2",
    "~": "obkE1552B06",
    "avty_t": "~/connected",
    "uniq_id": "multifunction_alarm_OpenBK7231N_E1552B06_sensor_2",
    "qos": 1,
    "dev_cla": "frequency",
    "unit_of_meas": "Hz",
    "stat_t": "~/2/get",
    "stat_cla": "measurement",
    "val_tpl": "{{ '%0.3f'|format(float(value)*0.1) }}"
    }
    

    What if we could store that in LittleFS and publish with command?
    Yes, I am working on publishFile...
    Helpful post? Buy me a coffee.
ADVERTISEMENT