Get mqtt topic data on demand?

I ended up doing something similar but I used settings.js (located in ~./.nodered) instead.
This allowed me to get the serial but also manipulate the full topic texts that I needed.
Since my node-red runs in systemd, it was becoming increasing difficult (at least to me, with my low knowledge of Linux) to create these different environment variables.

Here's my code that I inserted just before the module.exports line.

//get serial number of RPi from cpuinfo file
exec('cat /proc/cpuinfo | grep Serial | cut -d \' \' -f 2', (err, stdout, stderr) => {
  if (err) {
    //some err occurred
    console.error(err);
  } else {
    // need to use slice to remove the newline character at the end of the returned text
    process.env.SERIAL_NUMBER = stdout.slice(0,-1);
    process.env.TOPIC_CONFIGURATION = "/configuration/" + stdout.slice(0,-1);
    process.env.TOPIC_COMMANDS = "/commands/" + stdout.slice(0,-1);
  }
});
// end of setting environment variables

Not the prettiest, but worked like a charm.

2 Likes

I do similar things in settings.js including capturing the PID for the running Node-RED instance so that I can kill it from within Node-RED itself if needed.

My alternate installer also shows you how to use a file to set environment variables when starting Node-RED from systemd if that is of any interest to anyone. It makes it easy to do things like change the port number without having to edit the systemd startup script. All you have to do is change the local file and then systemctl restart node-red.

1 Like

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