Using environment variable during another environment variable definiton in flow properties - issue

Dear all NodeRed enthusiastics,
I notified I am not able to use environment variable in another environment variable definition in flow properties similar to example shown above. I don't know why, but the first variable (writen in format ${ID} in next variable definition) is empty string, despite ID variable was defined in special line above definition as string ...
You can see the pictures of successful and unsuccessful environment variables definitions ...
1] when I am NOT using ${ID} and ${TOPIC}, TOPIC = "/vamet1x02/", TOPIC_C0 = "/vamet1x02/c0/", TOPIC_C1 = "/vamet1x02/c1/", TOPIC_C2 = "/vamet1x02/c2/" as expected :slight_smile:
2] when I am uising ${ID} and ${TOPIC}, TOPIC = "/vamet1x/", TOPIC_C0 = "c0/", TOPIC_C1 = "c1/", TOPIC_C2 = "c2/" :frowning:
Would you be so kind and could you advise me, what I am doing wrong, please?
Thx in advance. Regards, Josef.
Works
DoesntWork

If I understand correctly then possibly all you need in the subflow properties is to provide the ID. Then inside the subflow build the TOPIC strings from the id and fetch the values from the environment variables.

Dear Colin,
thank you very much for your response.
Yes, exactly. All what I need is to enter ID (as string) and I would like to have all appropriate flow nodes properties / values after that derived from this environment variable.
BTW, "TOPIC_0x" are used for example in "mqtt in" nodes for "Topic" property.
Regards, Josef.

I meant remove the subflow properties except for ID and build the other values inside the subflow from the ID. So, for example, in a Function node

msg.topic = "/vamet1x" + env.get("ID") + "/"

or using template string literals

msg.topic = `/vamet1x${env.get("ID")}/`

Dear Colin,
thanks again for quick and valuable advise, I am using already this approach :wink:.
Unfortunately, it is not easy to do in case of mqtt in node, as there is no input option (except mqtt itself) and therefore setting up the parameters by code is impossible (or I am not experienced enough).
And what is frustrating for me ... as description of using environment variables on link Using environment variables : Node-RED says, it should be possible in TypedInput widget (like flow properties are) ... and there is indicator it is / was possible: Using subflow environment variable to set mqtt topic - #7 by tinamore ... knolleary used this and tinamore confirmed it is / was working ...
Therefore, is it relating to version of my NodeRed? Or was the attitude of knolleary solution misleading?
Hmmm ... still unclear for me.
Regards, Josef.

FWIW – I haven't been able to use one environment variable from within another. I tried to duplicate the example that @jopl posted from @knolleary, but that didn't work either. The output for TOPIC1 is "stat//power2" instead of the expected "stat/ROOM1/power2". Here's the simplified test flow:

[
    {
        "id": "34ee1d1f3ef8a9d2",
        "type": "tab",
        "label": "env test",
        "disabled": false,
        "info": "",
        "env": [
            {
                "name": "ROOM",
                "value": "ROOM1",
                "type": "str"
            },
            {
                "name": "TOPIC1",
                "value": "stat/${ROOM}/power2",
                "type": "env"
            }
        ],
        "_mcu": {
            "mcu": false
        }
    },
    {
        "id": "9a997e58a5bca39d",
        "type": "inject",
        "z": "34ee1d1f3ef8a9d2",
        "name": "",
        "props": [
            {
                "p": "ROOM",
                "v": "ROOM",
                "vt": "env"
            },
            {
                "p": "TOPIC1",
                "v": "TOPIC1",
                "vt": "env"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "_mcu": {
            "mcu": false
        },
        "x": 290,
        "y": 180,
        "wires": [
            [
                "003785f01356c9da"
            ]
        ]
    },
    {
        "id": "003785f01356c9da",
        "type": "debug",
        "z": "34ee1d1f3ef8a9d2",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "_mcu": {
            "mcu": false
        },
        "x": 490,
        "y": 180,
        "wires": []
    }
]

This simple example works for me.

[{"id":"78e2f572.71f74c","type":"subflow","name":"room_pir","info":"","category":"","in":[{"x":137,"y":179.0000057220459,"wires":[{"id":"d715137.ff4d2f"}]}],"out":[],"env":[{"name":"ROOM","type":"str","value":""},{"name":"SENSOR_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_sensor"},{"name":"LIGHTS_SET_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_lights/set"},{"name":"LIGHTS_GET_TOPIC","type":"env","value":"zigbee2mqtt/$(ROOM)_lights/get"}]},{"id":"d715137.ff4d2f","type":"mqtt out","z":"78e2f572.71f74c","name":"publish to ${LIGHTS_SET_TOPIC}","topic":"${LIGHTS_SET_TOPIC}","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"adf2dac3.b20fe8","x":400,"y":180,"wires":[]},{"id":"adf2dac3.b20fe8","type":"mqtt-broker","name":"localhost","broker":"localhost","port":"1883","clientid":"","autoConnect":true,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"tigger/LWT","birthQos":"0","birthPayload":"Online","birthMsg":{},"closeTopic":"tigger/LWT","closeQos":"0","closePayload":"Offline","closeMsg":{},"willTopic":"tigger.LWT","willQos":"0","willPayload":"Offline","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"1bd984a7.b656db","type":"subflow:78e2f572.71f74c","z":"b08cb6bc45dd4866","name":"kitchen_pir","env":[{"name":"ROOM","value":"kitchen","type":"str"},{"name":"SENSOR_TOPIC","value":"test/${ROOM}_sensor","type":"env"},{"name":"LIGHTS_SET_TOPIC","value":"test/${ROOM}_lights/set","type":"env"},{"name":"LIGHTS_GET_TOPIC","value":"test/${ROOM}_lights/get","type":"env"},{"name":"flexdash_grid","value":"","type":"str"}],"x":410,"y":280,"wires":[]},{"id":"2e51ab08b72022a2","type":"inject","z":"b08cb6bc45dd4866","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":280,"wires":[["1bd984a7.b656db"]]},{"id":"6dfce52f05474ff2","type":"mqtt in","z":"b08cb6bc45dd4866","name":"","topic":"test/kitchen_lights/set","qos":"1","datatype":"auto-detect","broker":"adf2dac3.b20fe8","nl":false,"rap":true,"rh":0,"inputs":0,"x":180,"y":380,"wires":[["e54004041812fc03"]]},{"id":"e54004041812fc03","type":"debug","z":"b08cb6bc45dd4866","name":"debug 34","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":420,"y":380,"wires":[]}]

Dear Colin,
thank you again.
As I can see, your definition using environment variables chaining is working. Have you any idea, where I did a mistake?
Regards, Josef.

Can you post a simple failing example, similar to the one I posted?

In particular, how have you configured the MQTT node?

Dear Colin,
I am so sorry for delay, I reinstalled everything for starting from "clean" environment ... no change.
Sending the example :-).
Thank you very much for your help.
Regards, Josef.
flow_example.json (2.4 KB)

I thought this was about subflow properties as in the post you linked to and the example I posted.

Edit: For the future please post flows inline as described in this canned message.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote ```)

``` 
   code goes here 
```

See this post for more details - How to share code or flow json

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