Sonoff RF Bridge. Strategies for receiving data

After a little bit of experimentation, I’ve created a Node-Red function node that duplicates the functionality of the rfbridge_demultiplexer python_script.

The entire flow consists of four nodes:

From left to right:

  1. RF Bridge is an mqtt in node subscribed to tele/RF_Bridge/RESULT.
  2. json node converts the payload from a string to an object.
  3. Demultiplexer is a function node.
  4. Publish is an mqtt out node.

The function node contains the following code:

var d = { 'A1A':['sensor1','ON','true'],
      'A2A':['sensor1','OFF','true'],
      'B1B':['sensor2','ON','false'],
      'C1C':['sensor3','ON','false']
    };

var p = msg.payload.RfReceived.Data;

if (p in d) {
  msg.topic = "home/" + d[p][0];
  msg.payload = d[p][1];
  msg.retain = d[p][2];
  msg.qos = 0;
}
else {
  msg.topic = "home/unknown";
  msg.payload = p;
  msg.retain = "false";
  msg.qos = 0;
}

return msg;

Obviously you will need to customize the function’s dictionary for your specific needs (and possibly the subscribed topic for the RF Bridge node).

Here's the flow's listing.
[
    {
        "id": "8998e275.d91a38",
        "type": "mqtt in",
        "z": "303845de.585f7a",
        "name": "RF Bridge",
        "topic": "tele/RF_Bridge/RESULT",
        "qos": "0",
        "broker": "a621bb5.8358248",
        "x": 100,
        "y": 120,
        "wires": [
            [
                "f97adc78.2a1af8"
            ]
        ]
    },
    {
        "id": "dfff2e32.4d5d38",
        "type": "function",
        "z": "303845de.585f7a",
        "name": "Demultiplexer",
        "func": "var d = { 'A1A':['sensor1','ON','true'],\n      'A2A':['sensor1','OFF','true'],\n      'B1B':['sensor2','ON','false'],\n      'C1C':['sensor3','ON','false']\n    };\n\nvar p = msg.payload.RfReceived.Data;\n\nif (p in d) {\n  msg.topic = \"home/\" + d[p][0];\n  msg.payload = d[p][1];\n  msg.retain = d[p][2];\n  msg.qos = 0;\n}\nelse {\n  msg.topic = \"home/unknown\";\n  msg.payload = p;\n  msg.retain = \"false\";\n  msg.qos = 0;\n}\n\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 460,
        "y": 120,
        "wires": [
            [
                "1716aba3.0e9de4"
            ]
        ]
    },
    {
        "id": "1716aba3.0e9de4",
        "type": "mqtt out",
        "z": "303845de.585f7a",
        "name": "Publish",
        "topic": "",
        "qos": "",
        "retain": "",
        "broker": "a621bb5.8358248",
        "x": 660,
        "y": 120,
        "wires": []
    },
    {
        "id": "f97adc78.2a1af8",
        "type": "json",
        "z": "303845de.585f7a",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 270,
        "y": 120,
        "wires": [
            [
                "dfff2e32.4d5d38"
            ]
        ]
    },
    {
        "id": "a621bb5.8358248",
        "type": "mqtt-broker",
        "z": "",
        "name": "YOUR_MQTT_BROKER",
        "broker": "YOUR_MQTT_BROKER",
        "port": "1883",
        "clientid": "YOUR_CLIENT_ID",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "willTopic": "",
        "willQos": "0",
        "willRetain": "false",
        "willPayload": "",
        "birthTopic": "",
        "birthQos": "0",
        "birthRetain": "false",
        "birthPayload": ""
    }
]
7 Likes