View LQI & RSSI in ZHA

If you need LQI and RSSI as entities in Zigbee, you are doing zigbee wrong

  • A wise Zigbee developer

That said, I’m doing zigbee wrong. I wrote out some Node-RED flows that consumes the ZHA WS API and then creates MQTT auto discovery sensors with the LQI, RSSI, parent information, etc. Needs a little work to re-do now what zha-map is deprecated though.

var devices     = [];

msg.zha_devices.forEach(item => {
    var map = msg.zha_map_devices.devices.find(o => {
        return o.ieee === item.ieee
    });

    // Find Parent
    var parent_router = {};
    if(map.neighbours.length === 0) {
        msg.zha_map_devices.devices.forEach(parent => {
            parent.neighbours.forEach( neighbour => {
                if (neighbour.relation == "Child" && neighbour.ieee == item.ieee) {
                     parent_router = parent;
                }
            });
        });
    }

    var dev = {
        name : (item.user_given_name !== null ? item.user_given_name : item.name),
        ieee: item.ieee,
        lqi : map.lqi,
        zha_lqi: item.lqi,
        zha_rssi : item.rssi,
        available : item.available && !map.offline,
        manufacturer: item.manufacturer,
        model: item.model,
        power_source: item.power_source,
        is_router: (map.neighbours.length > 0)
    };
    
    if(!dev.is_router) {

        dev.parent_ieee         = (parent_router.ieee !== null ? parent_router.ieee : "None");
        dev.parent_manufacturer = (parent_router.manufacturer !== null ? parent_router.manufacturer : "None");
        dev.parent_model        = (parent_router.model !== null ? parent_router.model : "None");
        dev.parent_nwk          = (parent_router.nwk !== null ? parent_router.nwk : "None");
    } else {
     
        var neighbours_lqi      = 0;
        var neighbours_best_lqi = 0;
        var child_count         = 0;
        var neighbours_best     = {};
        var coordinator_direct  = false; 
        
        map.neighbours.forEach(neighbour => {
            neighbours_lqi = neighbours_lqi + neighbour.lqi;
            if(neighbour.lqi > neighbours_best_lqi ) {
                neighbours_best_lqi = neighbour.lqi;
                neighbours_best = neighbour;
            }
            
            if(neighbour.device_type == "Coordinator") {
                coordinator_direct = true;
            }
            if (neighbour.relation == "Child") {
                child_count = child_count + 1;
            }
            
        });
        
        dev.child_count         = child_count;
        dev.neighbour_count     = (map.neighbours.length > 0 ? map.neighbours.length : null);
        dev.neighbour_avg_lqi   = Math.floor(neighbours_lqi / map.neighbours.length) ;
        dev.neighbour_best_lqi  = neighbours_best_lqi;
        dev.neighbour_best      = neighbours_best;
        dev.coordinator_direct  = coordinator_direct;
    }
    
    devices.push(dev);
});

msg.payload = devices;

return msg;

and

var sensor_name      = msg.payload.name;
var ieee             = msg.payload.ieee;
var unique_id        = msg.payload.ieee.replace(/:/g, '-').toLowerCase();
var state_topic      = "zha_devices/" + unique_id + "/state";
var attributes_topic = "zha_devices/" + unique_id + "/attributes";

if (msg.payload.available) {
    state_payload = 'ON';
} else {
    state_payload = 'OFF';
}

var sensor_config = {
    payload: {
        name: "ZHA Devices - " + sensor_name,
        unique_id: unique_id,
        state_topic: state_topic,
        json_attributes_topic: attributes_topic
    },

    topic: "homeassistant/binary_sensor/" + unique_id + "/config"
};

var state = {
    payload: state_payload,
    topic: state_topic
};

var attributes = {
    payload: msg.payload,
    topic: attributes_topic
};
return [sensor_config, state, attributes];