Global context data to create and fetch with JSONata

I would like to create some persistent global context data and fetch the values via a JSONata expression.

[
    {
        "id": "64036d4f75497fb8",
        "type": "inject",
        "z": "33667947.7eff86",
        "name": "timestamp",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 1780,
        "wires": [
            [
                "2dccac77b6123021"
            ]
        ]
    },
    {
        "id": "f991e0b7c84f3a55",
        "type": "change",
        "z": "33667947.7eff86",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "av_test",
                "pt": "global",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "av_test.rain",
                "pt": "global",
                "to": "10.0",
                "tot": "num"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 330,
        "y": 1780,
        "wires": [
            []
        ]
    },
    {
        "id": "2dccac77b6123021",
        "type": "change",
        "z": "33667947.7eff86",
        "name": "tel_message_gw",
        "rules": [
            {
                "t": "delete",
                "p": "payload",
                "pt": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "{}",
                "tot": "json"
            },
            {
                "t": "set",
                "p": "payload.content",
                "pt": "msg",
                "to": "\"TEST: \\n\" &\t\"Testwert: \" & $globalContext('test.rain') & \"mm\" & \"\\n\" &\t\"Schwellwert: \" & $globalContext('payload.rain') & \"mm\"",
                "tot": "jsonata"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 590,
        "y": 1780,
        "wires": [
            [
                "025cf57658c78c16"
            ]
        ]
    }
]

The global context data neither shows up in the context data view nor in the message (via debug-node). Could please someone give me a help!

I meanwhile found the reason: The global context was stored in "file", not in "default".

When storing the global context in the "default" storage, it works!!!!!!!

All of the functions dealing with context variables support the extra parameter to direct them to the correct store. Please read the help information, it is all there.

That was because your flow was not actually wired

chrome_ZRTZOHj00X

1 Like

thanks for the hint.
I am not a very good developer. I already work some time with node red. I wasn't able to find some documentation on context data to be used in JSONata. This is for me all by try and error or searching extensively the net.
If you have some link where to find a docu on this I would apreciate this.

I will try to find it myselfe, but anyhow!
Thanks for help.

I don't think it is well document that the second optional input is a store name.
$globalContext(string[, string])

Just for info
In a change node you don't need to define the global an object if it does not exist, the context store will do that for you. if the global context var does exist and is not an object, then you would need to define it.

You also don't need to delete the payload as you set it to {} which overwrites/deletes previous payload properties.

You also do not really require JSONata and its overheads, you could do it in a template node
e.g.

[{"id":"64036d4f75497fb8","type":"inject","z":"408c50b493cacaff","name":"timestamp","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":140,"wires":[["f991e0b7c84f3a55"]]},{"id":"f991e0b7c84f3a55","type":"change","z":"408c50b493cacaff","name":"","rules":[{"t":"set","p":"av_test.rain","pt":"global","to":"10.0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":140,"wires":[["a208449e2ebe908e"]]},{"id":"a208449e2ebe908e","type":"template","z":"408c50b493cacaff","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"TEST: \nTestwert: {{global[default].av_test.rain}}mm\nSchwellwert: mm","output":"str","x":620,"y":140,"wires":[["e95cba3f5a055faa"]]},{"id":"e95cba3f5a055faa","type":"debug","z":"408c50b493cacaff","name":"debug 335","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":510,"y":240,"wires":[]}]

You can also define the store in the template
{{global[store].name}}

thanks for the hint!
I never worked before with the template-node. So I need to have a look into what it does.
What I understand from your post JSONata in a change node cannot do the trick to specify the storage to use.

I will check! Thanks!

Incorrect the second optional input defines the store to use. otherwise the default store is used.
e.g
$globalContext("test.av_test.rain", "file")

1 Like

If I edit the JSONata string I get some help. But only "$globalContext(string)".
Where can I find more about the syntax of $globalContext. I did not find anywhere a docu on this. The JSONata docu itselfe does not give any special hints to node red - what is understandable. But in node red I also did not find very much on JSONata specifics.

A link would be fine - if there is a documentation an not only experts knowledge.

Again if you read my first post I state that it is not well documented and i provide the syntax. In my second post I provide an example using context storage named "file". So use the store name you want where it says "file".

The flow and global context is an node-red addition to the standard JSONata functions.

The only example I have seen of the syntax is in the JSONata expression editor, in the function reference section.

thanks for help!
By the way: I read your posts!

I anyhow hoped there is some documentation, even if not well documented.
But at least there is your and others expert knowledge!

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