# Identify manual override, however context gets lost?!

**URL:** <https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917>\
**Category:** General\
**Created:** [2 March 2021 21:23 UTC](https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917 "2021-03-02T21:23:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![docNeptun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/docneptun/32/14370_2.png) [@docNeptun](https://discourse.nodered.org/u/docNeptun)\
**Post date:** [2 March 2021 21:23 UTC](https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917/1 "2021-03-02T21:23:55Z")

</div>

**What I want to achieve:** I am using the [Blind Control Node](https://github.com/rdmtc/node-red-contrib-sun-position) for my shutters. However, in addition to the calculated positions coming from this node, the shutters may also be opened or closed with physical buttons and/or voice assistant commands.  
[only] in this case, I want to feed the new blind position back into the Blind Control Node to trigger it's "positionOverride" function (the automation is then suppressed for some time).

**my best idea so far:** After a lot of trial & error, I came up with a function that should distinguish between values coming from the controller node versus values coming from manual input... and decide, what to do...

**what doesn't work:** , I seem to be doing something wrong with the context storage. The auto value gets correctly stored in the node-context, however it cannot be retrieved if the node is re-triggered.. the count-value, strangely, behaves as expected.. please help!!!

```auto
// function to determine manual override
// Out1: to shutterblind
// Out2: back to controller node

var autoPos = context.get("autoPos")||10;

// Test if context is maintained correctly :-/
var count = context.get("count")||0;
count+=1;
context.set("count",count);

if (msg.hasOwnProperty("blindCtrl")) {
 // function was triggered by controller node
 node.status({fill:"grey",shape:"dot",text:"new auto "+msg.payload});
 context.set("autoPos", msg.payload);
 return [msg, null];
}
else {
 // function was triggered by a new value coming from the blinds
 
 // debug, why is autoPos reverted to the default?????
 node.status({fill:"grey",shape:"dot",text:"auto is "+autoPos});
 
 // allow for slight variations
if(Math.abs(msg.payload - autoPos)<=0.04)
 {
  // real position equals autoValue
  // do nothing
  // node.status({}); 
 }
 else
 {
 // relevante Positionsänderung
 
 var OverrideMsg = {
    topic: "levelOverwrite",
    payload: -1,
    position: -1,
    importance: 1
  }

 // node.status({fill:"blue",shape:"dot",text:"Override "+payload+" >> "+myValue});
 }

 return [null, OverrideMsg];
}

```

---

<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:** [2 March 2021 21:39 UTC](https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917/2 "2021-03-02T21:39:32Z")

</div>

Hi @docNeptun, welcome to the forum.

What sort of values will `autoPos` get set to?

You may be hitting problems with the line:

```auto
var autoPos = context.get("autoPos")||10;

```

The way the `||` operator works is that it returns the left-hand side if it has a true-like value, otherwise it returns the right-hand side. In JavaScript, the number `0` has a false-like value. That means if `autoPos` is ever set to `0`, this line of code will reset it to `10`.

It is safer to use the slightly more explicit code that checks for `undefined`:

```auto
var autoPos = context.get("autoPos");
if (autoPos === undefined) {
   autoPos = 10;
}

```

---

<div class="post-metadata">

**Author:** ![docNeptun](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/docneptun/32/14370_2.png) [@docNeptun](https://discourse.nodered.org/u/docNeptun)\
**Post date:** [2 March 2021 21:51 UTC](https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917/3 "2021-03-02T21:51:32Z")

</div>

> [@knolleary](#):
>
> That means if `autoPos` is ever set to `0` , this line of code will reset it to `10` .

Wow, that's the solution. Thank you...  
I wanted to be clever and avoid to have an undefined variable ... but of course autoPos may well be zero (a.k.a. the shutter completely closed).

---

<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 March 2021 21:51 UTC](https://discourse.nodered.org/t/identify-manual-override-however-context-gets-lost/41917/4 "2021-03-16T21:51:52Z")

</div>

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