# Need help by function for tracking msg.payload

**URL:** <https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053>\
**Category:** General\
**Created:** [30 January 2024 19:59 UTC](https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053 "2024-01-30T19:59:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tech152](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@tech152](https://discourse.nodered.org/u/tech152)\
**Post date:** [30 January 2024 19:59 UTC](https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053/1 "2024-01-30T19:59:51Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [30 January 2024 20:34 UTC](https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053/2 "2024-01-30T20:34:09Z")

</div>

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.

```auto
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]
}

```

---

<div class="post-metadata">

**Author:** ![tech152](https://avatars.discourse-cdn.com/v4/letter/t/5e9695/32.png) [@tech152](https://discourse.nodered.org/u/tech152)\
**Post date:** [31 January 2024 16:10 UTC](https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053/3 "2024-01-31T16:10:15Z")

</div>

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.

```auto

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

```

---

<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:** [30 April 2024 16:10 UTC](https://discourse.nodered.org/t/need-help-by-function-for-tracking-msg-payload/85053/4 "2024-04-30T16:10:42Z")

</div>

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