Need help by function for tracking msg.payload

Hi,
i need a function with 2 out´s, which track my msg.payload.

  • If msg.payload input less than -1000 send on first out "1" and on second output the input msg.payload value
  • After this, if the value greater than +1000, send on first out "0" and on second output "0"

These two functions are so simple and I programmed them

But now:
After the input value was -1000 for the first time, every time the input value changes until +1000 is reached, the input value should be to second output.

Can help some one, to create this function?

I am not sure I have computed this correctly.
The if statements returns for a given criteria, so it will not continue passed that point.

const lastValue = context.get('lastValue');


/* If msg.payload input less than -1000 send on first out "1" and on second output the input msg.payload value */
if(msg.payload < -1000){
    context.set('lastValue',msg.payload);
    return [{payload:1},msg]
}

/* After this, if the value greater than +1000, send on first out "0" and on second output "0" */
if(lastValue < -1000 && msg.payload > -1){
   context.set('lastValue',msg.payload);
    return [{payload:0},{payload:0}]
}

/* ?? */
if(!lastValue){
    // do nothing ??
    return [undefined, undefined]
}

/* After the input value was -1000 for the first time, every time the input value changes until +1000 is reached, the input value should be to second output. */
if(msg.payload < -1){
    context.set('lastValue',msg.payload);
    return [undefined,msg]
}

Thank for help. context was the function i have search.

It wasn't 100% functional, but I adjusted it the way it should be. It wasn't perfectly described by me either.

I would now like to add a modification, namely that the function is only deactivated when the value of 1000 is exceeded for more than 30 seconds at a time.


const lastValue = context.get('lastValue');
const active = context.get('active');

/* If Input greater then 1000 deactivate function */
if (msg.payload > 1000) {
    context.set('lastValue', msg.payload);
    context.set('active', 0);
    return [{ payload: 0 }, { payload: 0 }]
}

/* If function active */
if (active > 0) {
    context.set('lastValue', msg.payload);
    context.set('active', 1);
    return [{ payload: 1 }, msg]
}

/* If msg.payload input less than -1000 activate function */
if (msg.payload < -1000) {
    context.set('lastValue', msg.payload);
    context.set('active', 1);
    return [{ payload: 1 }, msg]
}

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