I have an strange problem and I thought I could share it in case someone wants to burn some neurons.
I have I flow section where I retrieve data from three modbus queries and build an unique message. For that I use a join node. In the end this json I build with the data is published to mqtt and stored in context.
The modbus is not so stable, so sometimes one part is missing and I get undefined values. So I inserted that function node:
if (!msg.payload.hasOwnProperty("power") || msg.payload.power == undefined) {
msg.payload.power = global.get("pv_sources.charger.power") || 0;
node.warn("Warning: Power undefined!");
}
if (!msg.payload.hasOwnProperty("voltage") || msg.payload.voltage == undefined)
msg.payload.voltage = global.get("pv_sources.charger.voltage") || 0;
if (!msg.payload.hasOwnProperty("energy_kwh") || msg.payload.energy_kwh == undefined)
msg.payload.energy_kwh = global.get("pv_sources.charger.energy_kwh") || 0;
if (!msg.payload.hasOwnProperty("energy_daily_kwh") || msg.payload.energy_daily_kwh == undefined)
msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0;
return msg;
The debug node sometimes registers the undefined value but the next line shows that is corrected.
After that, the whole payload is stored con context and sent to mqtt.
Now comes the strange thing... The mqtt broker is showing messages without those values that are fixed on the function.
Or
Strange, isn't it?
PS: Buffer nodes reply power, current / voltage / energy_kwh, energy_daily_kwh
Mppt node has a jsonata line that builds the object shown in the context image, and then there's a node that sends it mqtt. No more data changes.