MQTT to UI - Data loss or data stored issue?


I`ve been coming across a recurring issue using the mqtt in nodes to UI dashboard.

I have a simple layout - topic , function - ui as shown:

The function code is very simple such that :


Now , I am finding inconsistencies within the data displayed on the UI. Certain times when restarting flows, the ui shows a data value that isn`t the one pushed through the topic. The data is a prior value , almost like a stored value internally on node red that takes priority over current mqtt data coming through.

I can only compare it to say matlab , where you have stored variables on the programme, once assigned then aren`t being reassigned. On matlab you use a clr all, to whip any prior values before running the code each time and I feel like this is something similar - any suggestions as to what is going on here?

Thank you in advance,


Use something like MQTT explorer to inspect your broker.

It is highly possible you have (something has) sent a value to your topic with the retained flag.

When a message is published with the retain flag, the broker will send this value to anyone (your node-red) upon connect.

In addition, if you set the MQTT node Output a Parsed JSON Object then it will parse the JSON and output an object for you, so you can remove the function node.

Maybe it's your browser cache making it show the previous value until an MQTT message arrives.

You might fix it by sending 0 at startup.

Yeah I was wondering if it`s something to add in the start up and end part of the function?

Thing is, when mqtt data does come through it ignores it anyways for this assumed stored value? very strange

The mqtt in is a "0" string I believe , I have to parse it to get it onto the UI?

However, surely when I then pull more current data through mqtt then it will update the retained value with the newest value? As when I do this , the older value takes priority and the new value ignored?

Only if the newer values set the retained flag. That's how MQTT works.

At this point I would just clear it out (send null + retain) to the topic OR use MQTT explorer to delete it OR just ignore the first message.

That should not happen. Add debug nodes showing what is coming out of the MQTT node. If you set the debug node to output to the console then it will also send it to the node-red log so you can see there what happens on startup, if that is where the confusion is happening.

I`ll set all mqtt out to retain false first - then report back if the issue persists!

Yes so if every message coming from that topic is retain true, then it doesn`t matter really right? as the new messages will just overwrite the old retained one? However this is not the case as a prior value is still taking priority over the new ones coming through ?

There is no point using the At startup option of the function, I believe that is only executed when a message arrives at the function.

Use an inject node set to send 0 at startup.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.