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

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:

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 120103f

The "vbSensor" variable is definitely set:

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

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.

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).

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")


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!

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:

{"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?

The template node documentation says:

It is possible to use a property from the flow context or global context. Just use {{}} or {{}} , 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.

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:

With or without so I also learn something? :slightly_smiling_face:

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.

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

1 Like

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).


Good to know, thanks

1 Like

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

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

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

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