Accessing custom properties within subflow in the template module

Scenario:

I make a subflow and add a custom string property labeled "text".

Inside a function, I can use env.get("prefixText") to find the property value. What I want is to use it within a template function something like this:

{{flow.text}} This is after the text.

There is some stuff that seems unclear to me in the documentation for the mustache temple node.
Thank you!

Hi @cryogenicminer - welcome to the forum!

The subflow properties are exposed as environment variables. You should be able to use {{env.text}} to access the value.

Thanks knollary! I tried to put that in directly, and I must not understand exactly still. Here is my test setup:

=> timestamp ---> Subflow 1 ---> msg.payload

Inside the subflow is:

input --> template ---> output 1

The subflow properties look like this:


And the template is setup to pull that "text" property which should read something like: "TIME 1669676644053"

But, I can not for the life of me figure out how to get it to show the contents of the property. Thank you for looking at it :slight_smile: What am I missing?

Select the node where you use the subflow, Export it to the clipboard using CTRL+E (or via the menus) and paste it here, using the </> button in the forum entry window when you paste it in. Then we will be able to better see what you are doing.

Ah, my bad. That was in the formatting section of the forum guidelines.

[
    {
        "id": "7c820e03dbb518ae",
        "type": "subflow",
        "name": "Subflow 1",
        "info": "",
        "category": "",
        "in": [
            {
                "x": 40,
                "y": 40,
                "wires": [
                    {
                        "id": "41763aa8f5bade9b"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 280,
                "y": 40,
                "wires": [
                    {
                        "id": "41763aa8f5bade9b",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "text",
                "type": "env",
                "value": "Time is"
            }
        ],
        "meta": {},
        "color": "#DDAA99"
    },
    {
        "id": "41763aa8f5bade9b",
        "type": "template",
        "z": "7c820e03dbb518ae",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{{env.text}} {{payload}}",
        "output": "str",
        "x": 160,
        "y": 40,
        "wires": [
            []
        ]
    },
    {
        "id": "e672d6322ed2b137",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": ""
    },
    {
        "id": "aae8d4d14b1494c8",
        "type": "inject",
        "z": "e672d6322ed2b137",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 160,
        "y": 40,
        "wires": [
            [
                "e811c22e2fb928d5"
            ]
        ]
    },
    {
        "id": "e811c22e2fb928d5",
        "type": "subflow:7c820e03dbb518ae",
        "z": "e672d6322ed2b137",
        "name": "",
        "env": [
            {
                "name": "text",
                "value": "TIME",
                "type": "str"
            }
        ],
        "x": 320,
        "y": 40,
        "wires": [
            [
                "5521b6e122c9cedf"
            ]
        ]
    },
    {
        "id": "5521b6e122c9cedf",
        "type": "debug",
        "z": "e672d6322ed2b137",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 490,
        "y": 40,
        "wires": []
    }
]

That works for me

What do you see when you run it?

What versions of node-red (see the bottom of the dropdown menu) and nodejs (run command node -v, assuming you are not using Docker) to find out.

Huh... I just copied what I published here on the forum and re-imported it. Ran it and does not pull the env value example: 1669751683715

Node-red: 2.0.5
Node: v14.20.1

Oh, and yes, I am not using docker.

Can you upgrade to node red 3?

Support for env in the template node was added in Node-RED 3.0

If you aren't able to upgrade, an alternative is to use a Change node before the Template node to set a message property to the env var value, and then reference that message property in the Template.

Ohhh! ok. It's in a prod environment so I will upgrade, but it will be a while. In the meantime, I will make a cascade of Change nodes or use a Function node to do that. Thanks, you guys are the best!

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