# Context value question

**URL:** <https://discourse.nodered.org/t/context-value-question/37755>\
**Category:** General\
**Created:** [19 December 2020 13:50 UTC](https://discourse.nodered.org/t/context-value-question/37755 "2020-12-19T13:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [19 December 2020 13:50 UTC](https://discourse.nodered.org/t/context-value-question/37755/1 "2020-12-19T13:50:55Z")

</div>

Hi,

In a function node I store the "nvalue" of an On/Off switch (domoticz) in a context variable.  
This function node is part of a flow which turns a TV on/off if the Switch is turned On/Off.

```auto
if (msg.payload.idx == '351') {
    
    //store nvalue
    var nvalue=context.get('nvalue');
    
    if (typeof nvalue=='undefined'){
       nvalue=0; //set default
    }
    
    nvalue=msg.payload.nvalue;
    context.set('nvalue',nvalue); //save it
    
    return msg;
}

```

I want to use this context variable in an IF statement in another Function node.  
Which sync's the Switch if the TV is turned On/Off with the remote.  
I have this code, but it looks like the context variable is either not saved nor passed to this fuction node.

```auto
var tvstate = context.get('nvalue');

if (tvstate === 0) {

    msg.payload = {
        "idx": 351,
        "nvalue": 1,
    }
    
    return msg;
}

```

Does anyone has an Idea what I am missing here?

---

<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:** [19 December 2020 13:57 UTC](https://discourse.nodered.org/t/context-value-question/37755/2 "2020-12-19T13:57:29Z")

</div>

context is node specific  
flow is flow specific  
global is every where  
so use global.set and global.get

> **[Working with context : Node-RED](https://nodered.org/docs/user-guide/context#context-scopes)**

---

<div class="post-metadata">

**Author:** ![gschmidt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gschmidt/32/60349_2.png) [@gschmidt](https://discourse.nodered.org/u/gschmidt)\
**Post date:** [20 December 2020 08:38 UTC](https://discourse.nodered.org/t/context-value-question/37755/3 "2020-12-20T08:38:43Z")

</div>

Thanx, I’ll try!

---

<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:** [18 February 2021 08:38 UTC](https://discourse.nodered.org/t/context-value-question/37755/4 "2021-02-18T08:38:47Z")

</div>

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