# Context variable is reset on zero

**URL:** <https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023>\
**Category:** General\
**Created:** [17 April 2020 16:28 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023 "2020-04-17T16:28:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 16:28 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/1 "2020-04-17T16:28:36Z")

</div>

I use a node red v1.0.2 docker image for raspberry.  
After I adjusted some values for my sensors, I detected some strange behaviour of my functions.  
I fiddled around several hours over this problem an can now easily show the problem.

**1. create 2 inject nodes with payload true and false**  
**2. connect them to a function node:**  
var cnt = context.get("cnt") || 3;  
if (msg.payload === true) {  
cnt += 1;  
}  
else {  
cnt -= 1;  
}  
context.set("cnt", cnt);  
msg.payload = cnt;  
return msg;

**3. connect a debug node and watch the payload**

You will see, that you can increase the counter but not decrease below 0. As soon as the counter reaches 0 it will be initialized with 3.

FCK, I wasted a day of my lifetime over this problem.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 April 2020 16:44 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/2 "2020-04-17T16:44:56Z")

</div>

Welcome to the forum @sg25.  
Look at the line  
`var cnt = context.get("cnt") || 3;`  
and consider what it does if `context.get("cnt")` returns 0.  
The `||` operator means evalute the expression on the left and test it for truthiness. That test returns `false` for values like false, undefined, empty string and zero. If the test on the left of `||` returns `false` then the value on the right of `||` is returned and assigned to the variable cnt.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [17 April 2020 17:01 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/3 "2020-04-17T17:01:06Z")

</div>

And what can you do if you need to have such moving integer is

```auto
var cnt = context.get("cnt") 
if(!cnt){
    cnt = 0
}

```

But you can't have 3 as default if that value is not defined yet.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 April 2020 18:03 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/4 "2020-04-17T18:03:35Z")

</div>

If you search the forum this exact question comes up often.

Here is one almost identical from less than a day ago...

> [@Context is reset to default when input is 0](https://discourse.nodered.org/t/context-is-reset-to-default-when-input-is-0/24914/2):
>
> because of this line context.dim = context.dim || 100.0; When you set context.dim to 0, the next time it runs, it gets the 100.0 THE REASON IS... 0 == false Do this instead... if(context.dim == "undefined"){ context.dim = 100.0 //init value ONLY if its undefined } NOTE: accessing context in this manner is depreciated (see the node red documentation on context.get, context.set)

---

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 18:03 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/5 "2020-04-17T18:03:48Z")

</div>

@Colin.  
That is maybe an explanation but an inconvenient and dangerous functionality. Is there any remark about this issue in the documentation?

Normally I initialize variables at the first start of node red before the first real values are triggered by the sensors to prevent unexpected switching actions. A lot of variables are not initialized to zero in my project. If the sensor state changes to zero node red will set the init value, uups... Radio starts playing in the night, the mower is digging thru the snow...what ever.

It is not logical. The context variable is not deleted or undefined. You can see them in the context data browser all the time. The definition || was for me: Take the value from the context memory or, if not defined, initialize with value x. Sorry, but this behaviour should be fixed.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 April 2020 18:09 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/6 "2020-04-17T18:09:14Z")

</div>

Its a feature of JS language (and very useful when you understand it) & only dangerous if you don't learn how to use it.

I think it's called a short circuit if you wanna look it up.

It is a pain when you need to check for zero (it's because zero is same as false is this case)

Search JavaScript truthiness for more info.

---

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 18:21 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/7 "2020-04-17T18:21:09Z")

</div>

Now I know it. Thank you for the hint.

I used the examples here [https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions) and I found a lot more examples on the internet using ||.

Before I wrote my message, I searched for _node red context variables zero_ and other phrases in this forum and google. Without success. Maybe it's because of my poor English.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [17 April 2020 18:23 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/8 "2020-04-17T18:23:07Z")

</div>

> [@sg25](#):
>
> Maybe it's because of my poor English.

Wow normally I "get a sense" of non English writing... Yours is pretty good. Fooled me.

---

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 18:52 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/9 "2020-04-17T18:52:03Z")

</div>

@Steve-Mcl  
Thank you but I sometimes use a translator, write the English version and try to understand the German translation. If this is not successful I'm playing with the words until it fits. That helps to learn better English especially in the age of 62.

To complete the discussion I have corrected the code from above:

var cnt = context.get("cnt");  
if (cnt === undefined) {  
cnt = 3;  
}  
if (msg.payload === true) {  
cnt += 1;  
}  
else {  
cnt -= 1;  
}  
context.set("cnt", cnt);  
msg.payload = cnt;  
return msg;

That works!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [17 April 2020 21:04 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/10 "2020-04-17T21:04:32Z")

</div>

> [@sg25](#):
>
> Before I wrote my message, I searched for _node red context variables zero_ and other phrases in this forum and google. Without success

It isn't anything to do with context variables, or even node red, it just happens to be useful (often) in this situation. It is much more common to initialise something to zero or an empty array or object or something similar. It is perfectly safe in that situation. The only time it is a problem, the case of numbers, is if the default setting is not zero but zero is a valid value. So all these examples are fine

```auto
var x = context.get("x") || 0;
var x = context.get("x") || [];
var x = context.get("x") || {};
var x = context.get("x") || false;

```

but these are not (or are at least risky)

```auto
var x = context.get("x") || 3;
var x = context.get("x") || true;

```

It is just a feature of javascript that one has to know and watch out for.

---

<div class="post-metadata">

**Author:** ![sg25](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@sg25](https://discourse.nodered.org/u/sg25)\
**Post date:** [17 April 2020 21:42 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/11 "2020-04-17T21:42:30Z")

</div>

I have mentioned what I'm thought about ||. I was wrong but now it's clear. Thank you.

I wrote this topic maybe too early. My experience with javascript and node red is only 4 weeks old (pastime in the corona jail). My project, 20 different sensors and 6 cameras, is grown without any problems except this issue.

---

<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:** [16 June 2020 21:42 UTC](https://discourse.nodered.org/t/context-variable-is-reset-on-zero/25023/12 "2020-06-16T21:42:31Z")

</div>

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