Sonoff RF Bridge Node Red Demuxer w/ RF Learning



I implemented the demux strategy using node-red.
Features

  • Home Assistant Discovery
  • Sensor Learning for motion and door
  • File-based Yaml configuration w/ autosave and autoload on node-red restart.

Here is the main demux code that I am using. It’s a little messy right now

var result = {}
const regex = /(.{5})([7BEF])$/;

const sensor_topics = msg.sensors;

const states = {
    '7': 'close',
    'E': 'open',
    'F': 'battery',
    'B': 'tamper'
};

var data = msg.payload.RfReceived.Data;
var sensor
if (regex.test(data)) {
    var matches = regex.exec(data);
    sensor = sensor_topics[matches[1]];
} else {
    sensor = sensor_topics[data]
}
if (typeof sensor !== "undefined") {
    if (sensor.class == 'door' && matches.length == 3 ) {
        var state = states[matches[2]];
        switch (state) {
            case 'battery':
                result = {
                    topic: `${sensor.topic}/battery`,
                    payload: 1,
                    retain: false
                };
                break;
            case 'tamper':
                result = {
                    topic: `${sensor.topic}/tamper`,
                    payload: 1,
                    retain: false
                };
                break;
            default:
                result = {
                    topic: `${sensor.topic}/state`,
                    payload: states[matches[2]],
                    retain: sensor.retain
                    
                };
        }
        result.error = null
    } else {
        result = {
            topic: `${sensor.topic}/state`,
            payload: 1,
            retain: false
        };
        result.error = null
    } 
} else {
    result.error = { 
        message: `Unknown Sensor ${data}`,
        code: data
    }
}
return result;
1 Like

very nice, can You share whole flow?