Setting flow context not working?

#1

First of all I am running 0.16.2 (running on Jessie raspberry pi)

I have a flow that has a change node where I set a property on the flow context.

This doesn't appear to be getting set (as far as I can tell). To test it I have a function right after the change node that sets msg.flow to the flow object, then a debug node which dumps the entire msg. Basically like this:

[
    {
        "id": "4a64d7ea.ffaa88",
        "type": "inject",
        "z": "2de8ef9b.ec8fe",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 136.5,
        "y": 129.5,
        "wires": [
            [
                "72e38345.25e72c"
            ]
        ]
    },
    {
        "id": "72e38345.25e72c",
        "type": "change",
        "z": "2de8ef9b.ec8fe",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "flow",
                "to": "whatever",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 317.5,
        "y": 134.5,
        "wires": [
            [
                "ae3819fa.1cb0a8"
            ]
        ]
    },
    {
        "id": "ae3819fa.1cb0a8",
        "type": "function",
        "z": "2de8ef9b.ec8fe",
        "name": "get flow",
        "func": "msg.payload = flow.payload\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 556.5,
        "y": 161.5,
        "wires": [
            [
                "c5119dd5.5fe09"
            ]
        ]
    },
    {
        "id": "c5119dd5.5fe09",
        "type": "debug",
        "z": "2de8ef9b.ec8fe",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "true",
        "x": 645.5,
        "y": 238,
        "wires": []
    }
]

Any ideas?

#2

Upgrade...current version of NR is 0.19.5

#3

Since you have that version you presumably did not use the recommended method for installing node-red. See here for how to upgrade it:
https://nodered.org/docs/hardware/raspberrypi

1 Like
#4

Hi @ScottChapman

Whilst everyone has been quick to recommend you upgrade (which is certainly worthwhile), here what's going in with your flow...

To access a flow context property from a Function node, you need to use:

msg.payload = flow.get('payload');

See this page for details - although those docs do also cover things only available on the latest version.

#5

Thanks, that was it!

my node-red was basically what was included in the distro, so I'll go ahead and upgrade.

thanks!

#6

related question...

Do I need to use a change node in my flow to actually change those values? Or is there some other way I can easily establish these flow properties (which I am basically using as configuration for the flow)?

#7

you can do it with a function node as per the link in Nicks post or with a change node

#8

Right, I assume that node needs to be linked to my other nodes in the flow.

I was just wondering if I can have a "configuration" node, that is just not linked to any other nodes, that essentially fires at startup to set up the configuration that is used by the flow.

Does that make sense? I realize I can just do what I was doing, but wanting to understand alternatives.

Thanks!

#9

There is a contrib-node for this purpose but as you can do it with two core-nodes I wouldn't install a separate contrib-node (less maintenance efforts).

#10

Take a look at https://nodered.org/docs/writing-functions#global-context

You can store global context which is available over all flows.

1 Like
#11

or you can set an inject node to fire at start - to trigger a change node to set them all as you wish.

1 Like