# Can I create / call context.name\[variable\]

**URL:** <https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258>\
**Category:** General\
**Tags:** function-node\
**Created:** [6 February 2024 14:42 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258 "2024-02-06T14:42:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [6 February 2024 14:42 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/1 "2024-02-06T14:42:08Z")

</div>

I am creating a node that will monitor ON duration for many switches under MQTT control. Is there a way to create / identify context like this? Switch behavior can happen in any random sequence (ie: SW 1 OFF, SW 4 ON, SW 2 OFF).

Code sample below created to illustrate what I'm trying to do, not actual code.

```auto
> device = msg.device; // this is the switch number
> state = msg.payload; // "ON" or "off"
> context.timer[device] = new Date().getTime(); // ms
> context.state[device] = state;
> 
> if (state === 'off') duration[device] = new Date().getTime() - context.timer[device];

```

TIA st

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2024 14:45 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/2 "2024-02-06T14:45:35Z")

</div>

Please check the Node-RED official documentation first.

[https://nodered.org/docs/user-guide/context](https://nodered.org/docs/user-guide/context)

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [6 February 2024 15:38 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/3 "2024-02-06T15:38:50Z")

</div>

What makes you think I didn't? And before you tell me to search the forum, I've done that. And before you tell me to search the internet, I've done that.

But hey, thanks for responding anyway.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [6 February 2024 15:44 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/4 "2024-02-06T15:44:22Z")

</div>

I was going to say that the page that I shared answers your question explicitly. However, that isn't quite accurate, it links to the function page which has this section on using context which DOES explicitly answer your question.

[https://nodered.org/docs/user-guide/writing-functions#storing-data](https://nodered.org/docs/user-guide/writing-functions#storing-data)

---

<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:** [6 February 2024 15:56 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/5 "2024-02-06T15:56:55Z")

</div>

> [@StrongTown](#):
>
> What makes you think I didn't?

To answer that question, your syntax is wrong, so one would assume you never read the documentation.

---

<div class="post-metadata">

**Author:** ![StrongTown](https://avatars.discourse-cdn.com/v4/letter/s/4bbf92/32.png) [@StrongTown](https://discourse.nodered.org/u/StrongTown)\
**Post date:** [6 February 2024 15:58 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/6 "2024-02-06T15:58:32Z")

</div>

I bow to you and offer my thanks.

I understand the use of `set` and `get` with saving context (`global` or` flow`) but never thought about (related them to) creating a simple node local context.

Node-red continues to amaze me.

---

<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:** [6 May 2024 15:59 UTC](https://discourse.nodered.org/t/can-i-create-call-context-name-variable/85258/7 "2024-05-06T15:59:25Z")

</div>

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