# MQTT to UI - Data loss or data stored issue?

**URL:** <https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538>\
**Category:** Dashboard\
**Created:** [2 July 2023 09:44 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538 "2023-07-02T09:44:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 09:44 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/1 "2023-07-02T09:44:19Z")

</div>

Hello,

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:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8a9dc8ecc58ea5ea1ac09473cc09d1f7f1a6aac0.png)

The function code is very simple such that :

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/5/554f5cc81e2a865de78b0a92fa8d1c0e4b1ef304.png)

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,

E

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 July 2023 10:04 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/2 "2023-07-02T10:04:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 July 2023 10:08 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/3 "2023-07-02T10:08:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 July 2023 10:21 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/4 "2023-07-02T10:21:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 10:55 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/5 "2023-07-02T10:55:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 10:56 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/6 "2023-07-02T10:56:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 10:57 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/7 "2023-07-02T10:57:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 July 2023 11:05 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/8 "2023-07-02T11:05:57Z")

</div>

> [@edwardr](#):
>
> surely when I then pull more current data through mqtt then it will update the retained value with the newest value

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.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 July 2023 11:08 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/9 "2023-07-02T11:08:28Z")

</div>

> [@edwardr](#):
>
> the older value takes priority and the new value ignored?

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.

---

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 11:10 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/10 "2023-07-02T11:10:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![edwardr](https://avatars.discourse-cdn.com/v4/letter/e/e0b2c6/32.png) [@edwardr](https://discourse.nodered.org/u/edwardr)\
**Post date:** [2 July 2023 11:11 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/11 "2023-07-02T11:11:56Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [2 July 2023 11:12 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/12 "2023-07-02T11:12:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 August 2023 11:13 UTC](https://discourse.nodered.org/t/mqtt-to-ui-data-loss-or-data-stored-issue/79538/13 "2023-08-01T11:13:13Z")

</div>

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