Support multiline strings having environment variables in change node

I am using a change node to produce a long string where several parts need to be replaced by the value of environment variables. See example here below:

This functionally works perfect but the problem is that

  • it is not very convenient to edit / review such long strings as only a small part is visible in the editor.
    • moreover the string is actually a multiline string while it is shown as a single line string in the editor which also makes it harder to edit / review.

FYI in my case the multiline string =

from(bucket: "${INFLUXDB_BUCKET}")    
|> range(start: -30d, stop: now())   
|> filter(fn: (r) =>  r._measurement == "${INFLUXDB_MEASUREMENT}" and 
                       r._field == "steps" and
                        r.wearer == "${INFLUXDB_MIBAND_OWNER}") 
|> keep(columns: ["_time"])   
|> sort(columns: ["_time"], desc: false)   
|> last(column: "_time")

Maybe the template node can be used instead but I don't know how I then should specify environment variables in the template.

I see that the idea of extending the template node so that it also supports environment variables has been raised in the past:

That would also be a good solution for this issue.

Template currently only supports the msg along with flow and global vars as you've discovered. The workaround is to use a change node before the template to add the env vars to the msg as new properties.

Alternatively, if the environment vars are actaully set prior to the launch of Node-RED, you can pass them into global vars in settings.js of course. Though that doesn't help with the handy env vars you can set in groups and sub-flows.

Another alternative would be not to use env vars but rather global or flow vars. That would work in some but probably not all situations.

However, I agree that since the use of env vars in Node-RED is steadily expanding, making them available to the template node and possibly JSON as well would be a nice addition.

1 Like

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