# Update Text Input Fields from global variable

**URL:** <https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709>\
**Category:** Dashboard\
**Created:** [14 December 2023 10:23 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709 "2023-12-14T10:23:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JazzJackR](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JazzJackR](https://discourse.nodered.org/u/JazzJackR)\
**Post date:** [14 December 2023 10:23 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/1 "2023-12-14T10:23:34Z")

</div>

Hi everyone,

I want to prefill a text input field on node red start. When I modify the text input field, the value is already stored as global variable which is saved to the filesystem.

Now I want that when I restart the mosquitto broker or node red, that node red restores the global variable to the specified text input field.

How can I archive this?

Thanks in advance

Best Regards

---

<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:** [14 December 2023 11:00 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/2 "2023-12-14T11:00:04Z")

</div>

Welcome to the forum @JazzJackR

You can configure an Inject node to fire on startup, feed that into a change node which sets msg.payload to the global value, and feed that into the text node.

> [@JazzJackR](#):
>
> when I restart the mosquitto broker

What has mosquitto got to do with this question? If the value is coming from MQTT then feed that straight into the text 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:** [14 December 2023 11:14 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/3 "2023-12-14T11:14:53Z")

</div>

You could use the retained message option in Mosquitto too, getting rid of the global variable.

When Mosquitto starts up or when Node-red subscribes (on startup) to a topic, the last retained value is re-sent.

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

NB Do not tick this box in the input field config or you will have created an infinite loop!  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/7/d767d7a0dcb4dc2987c863f8f2f36d667c7c82e7.png)

---

<div class="post-metadata">

**Author:** ![JazzJackR](https://avatars.discourse-cdn.com/v4/letter/j/c37758/32.png) [@JazzJackR](https://discourse.nodered.org/u/JazzJackR)\
**Post date:** [14 December 2023 12:22 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/4 "2023-12-14T12:22:30Z")

</div>

Thanks @jbudd and @Colin

I thought when I restart mosquitto that all received messages are gone. This was the main reason for saving the values separately as global variables

---

<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:** [14 December 2023 12:32 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/5 "2023-12-14T12:32:17Z")

</div>

> [@JazzJackR](#):
>
> I thought when I restart mosquitto that all received messages are gone.

Retained messages are kept over a restart or power cycle and the last values automatically injected on connection, so either a reboot, mosquitto restart or node-red restart will all result in the latest value being received. Much more convenient than using context. This tutorial is excellent and will explain in more detail if you need it. [MQTT Essentials - All Core Concepts Explained](https://www.hivemq.com/mqtt-essentials/)

---

<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:** [14 December 2023 13:43 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/6 "2023-12-14T13:43:55Z")

</div>

Just to clarify, with mosquitto, whether to save retained messages over a restart or power down is actually configurable via the mosquitto config file. The default for Pi OS, and Ubuntu (and Debian I assume) is, in /etc/mosquitto/mosquitto.conf

```auto
persistence true
persistence_location /var/lib/mosquitto/

```

which tells mosquitto to save retained values, and where to put the files. I believe that the default install on Windows also has persistence true.

---

<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:** [28 December 2023 13:44 UTC](https://discourse.nodered.org/t/update-text-input-fields-from-global-variable/83709/7 "2023-12-28T13:44:51Z")

</div>

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