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
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.
What has mosquitto got to do with this question? If the value is coming from MQTT then feed that straight into the text node.
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.
NB Do not tick this box in the input field config or you will have created an infinite loop!
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
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
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
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.
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.