Concatenate and string

I have an environment variable named ip-address and can assign access it using ${ip-address}.

For example I assign it to a dashboard text node label or use it in a change node to set a message field.

I use it because the rig I have moves around and it used with different networks so I change the variable and it is reflected on may nodes.

But I would like to be able to simplify matters even further.

If I set ip-address ro just the first three octets, ie 192.168.1. then I can use it to change further nodes.

For example, I have three nodes that are set to 192.168.1.11, 192.168.1.12, 192.168.1.13.

I could then assign something like ${ip-address} + "1", ${ip-address} + "2", ${ip-address} + "3" to set them accordingly.

Rather than change 3 (or more) places, then I change just one.

Unfortunately I cannot figure out the syntax, the + "1" does not work. Is it possible to do?

As per the docs:

This only works if it replaces the entire property - it cannot be used to substitute just part of the value. For example, it is not possible to use CLIENT-${HOST} .

However, if the field where you are using it is a TypedInput (where it has the $ env selection) then it is possible e.g. ${ip-address}1


The only other option is to use a change node, function node, or other node to do the string concatenation.

Example Function:

msg.ip = env.get('ip-address') + '1'
return msg

The change node appears to accept a typed input (see test1 below) but then adding suffixes, whether with no concatenate characters or a plus symbol does not work.

(Note that I am only assigning to a flow variable because it is very quick and easy to monitor the resulting values)

Try using the env type input


As Steve noted

Try using the env type input

Thank you, that works in a change node perfectly.

How can I also make it work in a standard text field that accepts typed inputs?

I tried ${ip-address} which is fine but ${ip-address}1 or even ${ip-address}${another-var} both fail.

You cannot

Instead, you would need to do it in a function or change node using JSONata

or
in a template node

{{env.ip_prefix}}1

As per the docs

Ah, I missed that!

So, I am trying another tactic, because it better suits my workflow (I get the details that way): an array of addresses, ie ["192.168.1.1","192.168.1.2","192.168.1.3"] which I assign as a JSON string

image

Trouble is trying to access the elements, ${ip-address[0]} was my guess, which is incorrect. How far off is my syntax?

You would not us env input.
You would use msg input
msg. ip-address[0]

If the json is in a env var, then first set it to a msg var.
So would require two rules.
set
msg. ìp-address
to
$ ip-address

then
set
msg. ip-address
to
msg. ip-address[0]

Or
with one rule use JSONata input
set
msg. ip-address
to
J: $env("ip-address")[0]

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