Node red add power to MQTT switch

Hi all,
I have a few heaters around the house, they are controlled by a PLC that turns on and off relays in my cabinet. I send commands to this PLC from Node Red and Node Red is publishing the state of the heaters using MQTT. I managed to set this up in Node Red so that they are autodiscovered in home assistant, I followed a bunch of posts in here and other places some time ago, I hardly remember it anymore.
But, now I would like to add a sensor to each of these heaters so that I see power consumption as well.
Currently I have a function node for each of them were I give them node_id like
bedroom_2nd_fl_heat, name, manufacturer etc and then a change node where I set the stuff that I publish to the MQTT broker like this:

    "command_topic": "homeassistant/switch/" & $.payload.node_id & "/set",
    "state_topic": "homeassistant/switch/" & $.payload.node_id & "/state",
    "name": $,
    "icon": $.payload.icon,
    "unique_id": $.payload.node_id,
    "device": {
        "identifiers": [$.payload.node_id],
        "model": $.payload.model,
        "manufacturer": $.payload.manufacturer
    "availability_topic": "homeassistant/switch/" & $.payload.node_id & "/availability"

I also publish to the /config, which I think is needed for home assistant to discover it:

"homeassistant/switch/" & $.payload.node_id & "/config"

This works, and they pop up in home assistant as a device looking like this:


Now I want to add another entity in there called power, so that I get one device with multiple entities, like one of my ZigBee2Mqtt sensors for example:

I have read and tried to understand the Home assistant MQTT discovery and for sensors with multiple values I need to add multiple configuration topic submissions but I can’t understand how to do this.

You’re going to want the device identifiers between all the sensors and switches to be the same, but the unique_ids to be different. An easy solution would be to prefix the unique_id with their type.

"unique_id": "switch_" & node_id
"unique_id": "power_" & node_id

Here’s the function node I use for my door sensors

const [,id,type] = msg.topic.match(/security\/sensors345\/sensor\/(\w+)\/(\w+)/);
const discovered = context.get("discovered") || [];
const sensorId = `${id}_${type}`;

if(discovered.includes(sensorId)) return;

context.set("discovered", discovered);

let deviceClass = "door";
let stateOn = "ON";
let stateOff = "OFF";
switch(type) {
    case "loop1":
    case "loop2":
    case "loop3":
        deviceClass = "door";
        stateOn = "OPEN";
        stateOff = "CLOSED";
    case "tamper":
        deviceClass = "problem";
        stateOn = "TAMPER";
        stateOff = "OK";
    case "battery":
        deviceClass = "battery";
        stateOn = "LOW";
        stateOff = "OK";

const device = { 
    ids: `345_${id}`,
    name: `Sensor ${id}`,
    mf: "Honeywell",
    mdl: "5816WMWH Magnetic Contact",
    via_device: "345_hub"

const sensor = {
    name: `Sensor ${id} ${type}`,
    device_class: deviceClass,
    state_topic: `security/sensors345/sensor/${id}/${type}`,
    unique_id: `345_sensor_${sensorId}`,
    payload_on: stateOn,
    payload_off: stateOff,
    availability_topic: 'security/sensors345/rx_status',
    payload_available: "OK",
    payload_not_available: "FAILED"
msg.topic = `homeassistant/binary_sensor/345/${sensorId}/config`;
msg.payload = sensor;

return msg;
1 Like

Thank you! That seemed to do the trick.