# Accessing global context values in function?

**URL:** <https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318>\
**Category:** General\
**Created:** [21 November 2024 15:12 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318 "2024-11-21T15:12:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 15:12 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/1 "2024-11-21T15:12:29Z")

</div>

Apologies for the noob question, I'm just starting off with Node Red.

I'm trying to make a flow such that If the Generator is Running then set in a time delay ....then after the delay check to see if Battery Charge - AC Consumption is less than 1000. If less, shut off generator, if not, do nothing.

I can see AC Consumption and Battery Charge but I'm not sure how to reference those in my function. I know I'll need to put some if else logic in the function, but I don't know how to reference those values. Many thanks for any help.

 ![logic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2db008fa948dea737e3a13c5962dc46c0ff70c79.jpeg)

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 November 2024 15:29 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/2 "2024-11-21T15:29:59Z")

</div>

check if this helps.

```auto
[{"id":"e6ee54547726fd4a","type":"function","z":"972775b3ccaa2485","name":"Check Global Context","func":"if (global.get(\"condition1\") == \"Yes\") {\n msg.payload=\"Condition 1 Met\"}\n else {msg.payload = \"Condition 1 not met\"\n }\n\n return msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":2400,"wires":[["30af994c30ebc8e1"]]},{"id":"152b53caea2a44c1","type":"inject","z":"972775b3ccaa2485","name":"Check","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":330,"y":2400,"wires":[["e6ee54547726fd4a"]]},{"id":"30af994c30ebc8e1","type":"debug","z":"972775b3ccaa2485","name":"debug 318","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":2400,"wires":[]},{"id":"babcc51a29551240","type":"function","z":"972775b3ccaa2485","name":"Set Global context","func":"global.set(\"condition1\", msg.payload);\nreturn msg;\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":550,"y":2320,"wires":[[]]},{"id":"dc1c3d4cfb5cfe13","type":"inject","z":"972775b3ccaa2485","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Yes","payloadType":"str","x":330,"y":2320,"wires":[["babcc51a29551240"]]},{"id":"19a2576c7aef715f","type":"inject","z":"972775b3ccaa2485","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"No","payloadType":"str","x":330,"y":2360,"wires":[["babcc51a29551240"]]}]

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 November 2024 15:36 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/3 "2024-11-21T15:36:31Z")

</div>

You may find it better to collect all the information in one object. Then there would be no need for context, and when 1 item changes you have all the other information. Each item must have a unique identifier, in the example I have used msg.topic.

You can use a join node in manual mode to do this, after three items have come in any change to them will send an output that has all the info from previous readings.

Here is an example I have used inject nodes, but you can use your incoming nodes and if they have no topic field you can add them with a change node.

```auto
[{"id":"3eec1b2334fe4b33","type":"inject","z":"d1395164b4eec73e","name":"Generator state","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"genState","payload":"true","payloadType":"bool","x":240,"y":7580,"wires":[["2990d3bb5c32cf09"]]},{"id":"2990d3bb5c32cf09","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","useparts":false,"accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":390,"y":7620,"wires":[["96fc0b979686861b"]]},{"id":"b961b9912ac41161","type":"inject","z":"d1395164b4eec73e","name":"vattery charge","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"batCharge","payload":"56","payloadType":"num","x":230,"y":7640,"wires":[["2990d3bb5c32cf09"]]},{"id":"2644fcce954af7e7","type":"inject","z":"d1395164b4eec73e","name":"AC concumption","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"acComp","payload":"700","payloadType":"num","x":240,"y":7700,"wires":[["2990d3bb5c32cf09"]]},{"id":"96fc0b979686861b","type":"debug","z":"d1395164b4eec73e","name":"debug 2579","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":7560,"wires":[]}]

```

Now you have all the info in one object for you function or other nodes.

 ![chrome_screenshot_21 Nov 2024 15_33_50 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/0/c0c94b69effc2a2506395d229826567b98b1d19f.png)

You may benifit from watching the [essentials videos](https://m.youtube.com/watch?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6&v=ksGeUD26Mw0&embeds_referring_euri=https%3A%2F%2Fnodered.org%2F&source_ve_path=OTY3MTQ&feature=emb_imp_woyt) which will give you the basics of messages and standard nodes.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 15:40 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/4 "2024-11-21T15:40:04Z")

</div>

Do I copy and paste that code you supplied into the function or do I need to import it somehow?

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 15:43 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/5 "2024-11-21T15:43:07Z")

</div>

Thank you, that seems intuitive. So I could take my generator state node and all the other nodes I need and join them together for processing and they'd be subelements in the msg for me to reference. Thank you very much, I'll try that.

I appreciate the help for these basic questions that I probably could have answered with more reading.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 November 2024 15:45 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/6 "2024-11-21T15:45:22Z")

</div>

> [@OffGridNoob](#):
>
> Do I copy and paste that code you supplied into the function or do I need to import it somehow?

No sorry I should of added  
[How to import/export a flow](https://nodered.org/docs/user-guide/editor/workspace/import-export)

To your OG topic, you can read how to access context in a function here [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [21 November 2024 15:52 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/7 "2024-11-21T15:52:53Z")

</div>

its a flow, so you need to import it Copy/Ctrl+i/Paste/Deploy.  
I was just trying to show you how to set and get global context in a function node. But as @E1cid suggested, avoiding context and getting a linear flow is best option.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 15:54 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/8 "2024-11-21T15:54:00Z")

</div>

So far I'm not sure how to reference the individual data components that are joined in my switch. I'll take a look at that video and look into using a change node to add a topic.

 ![obj](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/0/702b3d7ecfbd51ffc3915283603a71b19106a65a.jpeg)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 November 2024 15:57 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/9 "2024-11-21T15:57:59Z")

</div>

You will need to add topics with change nodes if the input nodes do not allow you to add a topic in them.

Add a debug and the you can see the joined object

There’s a great page in the docs ([Working with messages : Node-RED](https://nodered.org/docs/user-guide/messages)) that will explain how to use the debug panel to find the right path/value for any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

![BX00Cy7yHi](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd1f333a9e043b061b39917c328b0094d3e52d30.gif)

You can use the path to reference the data in a switch node and most other nodes. You can use multiple switch nodes in series or parallel to do your logic.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 16:20 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/10 "2024-11-21T16:20:20Z")

</div>

Awesome, I will get to studying. Excited to learn this, many thanks!

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 16:31 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/11 "2024-11-21T16:31:01Z")

</div>

That debug link you sent has opened it up. Now I'm seeing the components from the join. I had to set the join node to not do automatic, and to send to debug after 1 message changed. Now it's becoming more clear. Thank you!

 ![debug](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/1/d1273b6ecf41f788fd2a59f762b6f88d1aef9d34.jpeg)

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 16:45 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/12 "2024-11-21T16:45:26Z")

</div>

It appears I do not need a change node because it is publishing topics.

 ![topic](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/2/82b7a21273f363dcb99ede376d84241ce7e7dc7c.jpeg)

I'm just not yet sure how to reference those individual topics in my switch for function.

 ![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/4/34264d59c1d1936c3869338141afe8a4a2d767c1.jpeg)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 November 2024 17:04 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/13 "2024-11-21T17:04:31Z")

</div>

Use the copy path button in the debug node, the path is the way to reference the value in a switch node.

I would also advise to add your own topics as the ones from the input nodes have spaces, which complicates the path for the switch node. Add topics and use underscores instead of spaces.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [21 November 2024 17:19 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/14 "2024-11-21T17:19:07Z")

</div>

This seems to be working now. When you say add my own topics (to get rid of spaces), is that done with the Change node?

 ![switch](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdea885c5d4646258fed75732b3f02dc4a6d9733.jpeg)

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [21 November 2024 17:57 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/15 "2024-11-21T17:57:15Z")

</div>

Yes, unless you can add them manually in your input nodes.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [22 November 2024 15:46 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/16 "2024-11-22T15:46:47Z")

</div>

Thanks to all the help I got here (especially the join command in lieu of global context), I have been able to create a generator auto-start and auto-stop flow.

One flow I'm having a bit of issue with is meant to detect when the generator has run out of gas and isn't supplying power anymore, so that it shuts off the relay.

It seems my error was thinking the message from the join module would be updated to the latest values after the delay. This delay is so the generator can ramp up before its power is evaluated. But what is happening is the messages that are delayed are not being dynamically updated to the current values by the time they get to the function that evaluates them, so the generator keeps shutting down.

Is there a way to make the data in the join node always current value even as it travels along the flow? Or, alternatively, not have AC Consumption, Battery Charge, and MPPT power evaluated until AFTER the delay?

 ![join](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5a1caca0eb255ec10b461d4efcd3e716aec8d36.jpeg)

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [22 November 2024 16:09 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/17 "2024-11-22T16:09:57Z")

</div>

> [@OffGridNoob](#):
>
> make the data in the join node always current value

I think maybe you misunderstand messages and Node-red.  
The join node is not continually monitoring the "state" of it's 4 inputs. It only ever does anything when a message arrives from one of the inputs. What it does then is to compare the latest inputs and (maybe) send a message on down the line.

> [@OffGridNoob](#):
>
> But what is happening is the messages that are delayed are not being dynamically updated to the current values

Maybe you can replace the delay node with a trigger node? Set it to send nothing, wait 120 seconds and then send the _latest_ msg object. You might need to be able to reset the trigger depending on the incoming message.

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [22 November 2024 16:21 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/18 "2024-11-22T16:21:38Z")

</div>

Yes, I misunderstood. I will take a look at the trigger node, many thanks!

---

<div class="post-metadata">

**Author:** ![OffGridNoob](https://avatars.discourse-cdn.com/v4/letter/o/41988e/32.png) [@OffGridNoob](https://discourse.nodered.org/u/OffGridNoob)\
**Post date:** [22 November 2024 16:47 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/19 "2024-11-22T16:47:25Z")

</div>

I think I've figured it out.

I just referenced the global context variable in my function and that returns the latest data (after the delay).

> power = global.get("victronenergy.battery.\_512.Dc.\_0.Power");

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [20 February 2025 16:48 UTC](https://discourse.nodered.org/t/accessing-global-context-values-in-function/93318/20 "2025-02-20T16:48:24Z")

</div>

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