Undefs don't want to go away

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.
imagen

After that, the whole payload is stored con context and sent to mqtt.
imagen

Now comes the strange thing... The mqtt broker is showing messages without those values that are fixed on the function.
imagen
Or
imagen

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.

Since your Undefs function fills out any missing properties, it can only be something to do with either...

  • msg.payload.energy_daily_kwh is an empty string, 0, false or something else != undefined
    • i.e. the if(msg.payload.energy_daily_kwh == undefined) fails and msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0; is not being called.
    • you can use a function to better determine this (SEE BELOW)
  • the node named mppt is doing something odd
  • there is a set by reference issue somewhere
    • these are solved by deep cloning the msg to ensure it cannot be changed by reference in another node.

Better test...

As alluded to before, 0, '' and false will fail to call your global.get() ...

image

You could change it to ...

if (!isNumeric(msg.payload.power))
    msg.payload.power = global.get("pv_sources.charger.power") || 0;

if (!isNumeric(msg.payload.voltage))
    msg.payload.voltage = global.get("pv_sources.charger.voltage") || 0;

if (!isNumeric(msg.payload.energy_kwh))
    msg.payload.energy_kwh = global.get("pv_sources.charger.energy_kwh") || 0;

if (!isNumeric(msg.payload.energy_daily_kwh))
    msg.payload.energy_daily_kwh = global.get("pv_sources.charger.energy_daily_kwh") || 0;

function isNumeric(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

return msg;