Send a bunch of data from node-red to home-assistant, what is the best way

Hello.

I’m using Node-RED to parse some Modbus data using JavaScript functions, because it’s the only sane and reliable way that I found. The data is coming from my solar inverter, and the data is about the generated data, battery status, and that stuff.

Now that I have the data in the right shape, I want to export that data to Home Assistant so I can use it on automations and I can see it in the Energy dashboard.

I am using this great node for now:

But that requires to create an entity and connect a node for each property. Probably it is not that much, but I was wondering if there is a better way of doing this ?

This is how it currently looks with two nodes:

Also, I’m wondering if creating a separate entity for each value is the right thing to do. I’m doing it like that because that’s the only way that I have to specify the kind of data that I’m providing: voltage, current, watts, etc.

Thanks in advance

Well for the energy dash, you’ll want individual sensors. In other situations You can add as many attributes as you want.

I saw I can add attributes, but they do not allow to set the unit or the type, which is kind of key for the energy dashboard to pick those up.

Thank you for your help in any case.
Is there any way to create them “in bulk” or “on demand” ? Currently I have to add them one by one manually

I found a way to do it programatically. This is the code of a function node in nodered, which then you attach that node to HA API from the node-red-contrib-home-assistant helpers.

This is just a portion of it, the for loop is iterating a list of data coming from another node and matching it against a local list of interesting values, that is irrelevant for the scope of this, so I just put the loop where I create the entities and send the state to home-assistant

const serverId = "noderedserver";

// Iterate over the parsed values
for (const [key, value] of Object.entries(inputData)) {

    // Only process if this key is in our "valuable list"
    if (sensorDefinitions[key]) {
        const def = sensorDefinitions[key];

        // Convert camelCase to snake_case (e.g. "criticalLoadsCurrentL1" -> "critical_loads_current_l1")
        const snakeCaseKey = key.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase();

        // Create the node_id
        const nodeId = "solar_" + snakeCaseKey;

        // 1. Send Discovery/Config Message
        const discoveryMsg = {
            payload: {
                data: {
                    type: "nodered/discovery",
                    server_id: serverId,
                    node_id: nodeId,
                    component: "sensor",
                    config: {
                        name: def.name,
                        device_class: def.device_class,
                        unit_of_measurement: def.unit_of_measurement,
                        state_class: "measurement"
                    }
                }
            }
        };
        node.send(discoveryMsg);

        // 2. Send State Message
        const stateMsg = {
            payload: {
                data: {
                    type: "nodered/entity",
                    server_id: serverId,
                    node_id: nodeId,
                    state: value
                }
            }
        };
        node.send(stateMsg);
    }
}

node.done();
return null;