# Keep getting "undefined" when trying to access Context Data Flow variable

**URL:** <https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824>\
**Category:** General\
**Created:** [5 June 2020 11:06 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824 "2020-06-05T11:06:09Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 11:06 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/1 "2020-06-05T11:06:09Z")

</div>

I've spent a lot of time trying different approaches and haven't been able to figure this out so far.

I've got a flow for an alarm system which uses Zigbee2mqtt running under Home Assistant, some Ikea buttons and some Xiaomi vibration sensors, and it all works fairly well.

As part of the flow, I set a Context Data Flow variable "alarmActive" (in file) and am using a Switch node to successfully read the boolean value and enable or disable the alarm.

However, now I'm trying to set and read a variable which stores a name value to indicate which vibration sensor was triggered and use that variable in a notification service node (Home Assistant).

I've tried multiple variations of code within a Function node and they all result in "undefined" when trying to read a Context Flow variable "vbSensor" in a debug node attached to the Function node.

The code I have in my function node currently is:

```auto
var sensor = flow.get("vbSensor");
msg.sensor = sensor;
return msg;

```

I've attached screenshots of the flow and the output. I think I might be misunderstanding scope in some way or else not referencing the Context variable correctly in either the function or the debug node? It's strange though that the Switch node can successfully read the value of the "alarmActive" variable. When I try to read that value with the function node code, it also produces an "undefined" value. What am I doing wrong?

 ![Annotation 2020-06-05 120103](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/866445d614c9fb28f6c655045e76211089c8e18b.png) ![Annotation 2020-06-05 120103f](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6edf456a3bc3ecc7b319e0583e244eb7d127e93a.png)

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 11:07 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/2 "2020-06-05T11:07:48Z")

</div>

The "vbSensor" variable is definitely set:

 ![Annotation 2020-06-05 092632](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/4/64e0dd34d62c34682d12b9d3660a2aed1be85148.png)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 11:16 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/3 "2020-06-05T11:16:21Z")

</div>

Is the variable set on the same flow? Perhaps you should be using `global` context instead?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 11:27 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/4 "2020-06-05T11:27:21Z")

</div>

> [@diagonali](#):
>
> I think I might be misunderstanding scope in some way

I have to say I find it a bit confusing myself. On some cases "flow" refers to nodes directly linked to each other but in the case of context, it seems to refer to nodes on the same tab. And in the documentation tabs are actually also called "flows" if I remember correctly.

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 11:32 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/5 "2020-06-05T11:32:44Z")

</div>

Hi thanks for getting back to me. the Context variable is definitely on the same "flow" i.e. Tab. I've just tried setting a global variable and reading that back with `global.get("vbSensor")` and that doesn't seem to work either. The variable is definitely set, I can see it in the sidebar but still getting "undefined" from the debug node. I'm wondering if "undefined" means I need to drill down into the object or something, but I'm not sure how to see what's actually coming back or if undefined means "null". I used an if statement to test for the var "sensor" and it for some reason responded as if the variable wasn't set (skipped the if statement).

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [5 June 2020 11:36 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/6 "2020-06-05T11:36:29Z")

</div>

From that screenshot I can see you have multiple context stores defined - `file` and `memoryOnly`.

The `vbSensor` value is stored in the `file` store.

Calling `flow.get("vbSensor")` will return the value of `vbSensor` from the default context store - which will be either `file` or `memoryOnly`. My guess is `memoryOnly` is the default store.

To get the value out of the `file` store you need to tell it to get from that store: `flow.get("vbSensor", "file")`

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 11:56 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/7 "2020-06-05T11:56:16Z")

</div>

That's explained perfectly it and is exactly what I needed to know. I knew I was missing something! I've just tested it and it works as expected now. Thanks very much!

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 12:06 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/8 "2020-06-05T12:06:38Z")

</div>

One more thing... I'm trying to use the variable in a Service call node and trying to use the Mustache template format to include the vbSensor variable. I've got this:

```auto
{"title":"ALARM ACTIVATED","message":"{{flow.vbSensor}} activated alarm"}

```

Is it possible to reference the file variable in the mustache template format?

What's the best way to approach this in order to embed the variable value in the Data field? Is there documentation for the mustache template objects available?

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 12:23 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/9 "2020-06-05T12:23:45Z")

</div>

The template node documentation says:

> It is possible to use a property from the flow context or global context. Just use `{{flow.name}}` or `{{global.name}}` , or for persistable store `store` use `{{flow[store].name}}` or `{{global[store].name}}` .

I assume as you use multiple stores you'd need to use the latter syntax. E.g. `{{flow['file'].vbSensor}}` if I read the documentation correctly.

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 14:13 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/10 "2020-06-05T14:13:48Z")

</div>

That's great, thanks! I tried it and it works but you just need to put the store name without single quotes: `{{flow['file'].vbSensor}}`

Are there are any obvious downsides to using the file store instead of the memory store? File works fine though... I use file so that if my Raspberry Pi is rebooted it will have a record of the last sensor activated.

What I was originally trying to do (and this may need another post) was to get the name of the activated Xiaomi Vibration sensor from the Zigbee2mqtt node itself earlier in the flow instead of setting a variable for each one as it's triggered. This way works ok now, but I couldn't find a way to get the "name" of the previous node and insert that into the Data field of the service node. This is what I have at the moment:

 ![Annotation 2020-06-05 151014](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/063377b523ce69f2f70e7cfb6650fdee6ec4dbd2.png)

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 14:22 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/11 "2020-06-05T14:22:09Z")

</div>

> [@diagonali](#):
>
> That's great, thanks! I tried it and it works but you just need to put the store name without single quotes: `{{flow['file'].vbSensor}}`

With or without so I also learn something? 🙂

---

<div class="post-metadata">

**Author:** ![ristomatti](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ristomatti/32/5857_2.png) [@ristomatti](https://discourse.nodered.org/u/ristomatti)\
**Post date:** [5 June 2020 14:27 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/12 "2020-06-05T14:27:04Z")

</div>

> [@diagonali](#):
>
> Are there are any obvious downsides to using the file store instead of the memory store? File works fine though... I use file so that if my Raspberry Pi is rebooted it will have a record of the last sensor activated.

One theoretical downside is the extra writes to disk when the context is persisted if you run your Raspberry from an SD card. But as the context is written to disk only every 30 seconds I doubt this to corrupt anyone's SD card.

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 14:45 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/13 "2020-06-05T14:45:27Z")

</div>

Sorry, without: `{{flow[file].vbSensor}}`. Also, thanks for info about SD card. I've got a high endurance one so should be ok.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [5 June 2020 15:46 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/14 "2020-06-05T15:46:35Z")

</div>

> [@ristomatti](#):
>
> context is written to disk only every 30 seconds

Sort of... I have a flow that saves to context something new every hour. It will write to the disk only when there is some stuff to be written and not every 30 seconds.

I keep below excerpt from a previous post always close to remind me how saving context to the filesystem works:

> It only flushes when there are changes to flush, but will ensures it doesn't flush any more frequently than the configured interval (default is 30 seconds).

---

<div class="post-metadata">

**Author:** ![diagonali](https://avatars.discourse-cdn.com/v4/letter/d/e36b37/32.png) [@diagonali](https://discourse.nodered.org/u/diagonali)\
**Post date:** [5 June 2020 16:08 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/15 "2020-06-05T16:08:07Z")

</div>

Good to know, thanks

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [5 June 2020 18:00 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/16 "2020-06-05T18:00:14Z")

</div>

> [@ristomatti](#):
>
> But as the context is written to disk only every 30 seconds

To change the default setting of flushing data every 30 seconds, add `flushInterval` to the filesystem config in settings.

```auto
// Sets the flush interval to every 5 minutes instead of 30 seconds

 file: { module: 'localfilesystem',
          config: {
                  flushInterval: '300'
                  },
          },

```

---

<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:** [19 June 2020 18:00 UTC](https://discourse.nodered.org/t/keep-getting-undefined-when-trying-to-access-context-data-flow-variable/27824/17 "2020-06-19T18:00:17Z")

</div>

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