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;