Identify manual override, however context gets lost?!

What I want to achieve: I am using the Blind Control Node 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!!!

// 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 ];
}

Hi @docNeptun, welcome to the forum.

What sort of values will autoPos get set to?

You may be hitting problems with the line:

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:

var autoPos = context.get("autoPos");
if (autoPos === undefined) {
   autoPos = 10;
}
1 Like

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

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