Compare sensor value with previous reading and return a value based on comparison

Hello, I'm struggling on how to use context within a function node to write sensor value to context and comparing it with a subsequent one. The comparison should return previous value if the subsequent is 2 times smaller or 2x larger than the previous one. Perhaps anyone could help?

post your flow (less the unneeded stuff like influx node) & we'll take a look.

select the relevant nodes (use ctrl+click to select them) then export (ctrl+e)

paste between a code block

```
like this
```

[{"id":"2b201943.8d4446","type":"change","z":"f6f2187d.f17ca8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number(payload)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":740,"wires":[["1809fcef.2ba5c3"]]},{"id":"1809fcef.2ba5c3","type":"function","z":"f6f2187d.f17ca8","name":"???Function to compare with previous value??","func":"msg.payload = msg.payload.toString();\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":720,"wires":[["cb74e9f0.eeb6a8","b781c6dd.38e9c8"]]}]

Where in your function node are you looking at context?
Have you read about using context in a function node in the documentation?

Hello zenofmud,
Tried to read the documentation but it is above my understanding, I am not familiar with programming.

I have added function node but not sure how to write a logical sentence in a way that sensor value would be compared with the previous value and If it is not two times smaller or two times larger would pass through to influxdb, otherwise previous value should be sent to influxdb.

so if it isn't two times larger of smaller, do you use that value as the next value to check? for example lets say the values coming in are

2,3,4,5,6,7,8,7,6,5,4,3,2,-5

  • 2 to 3 is a difference of one so you would send 2 - now do you save 2 or 3 as the value to check 4 against?

Here is a working demo but there are some assumptions made - it might work for you.

/* SPEC
    The comparison should return previous value if the subsequent
    is 2 times smaller or 2x larger than the previous one.
*/


var current = msg.payload; //get current value
var previous = context.get("previous"); //get previous value


// if previous is zero or empty, things wont work as expected, return current message
if (!previous) {
    return msg;
}

if (current > (previous*2.0) || current < (previous/2.0)) {
    msg.payload = previous;
} 
context.set("previous", msg.payload); //update previous
return msg;

demo flow...

PSLLFIeKpw

[{"id":"1809fcef.2ba5c3","type":"function","z":"dd03fa8d.2f4be8","name":"compare with previous value","func":"/* SPEC\n    The comparison should return previous value if the subsequent\n    is 2 times smaller or 2x larger than the previous one.\n*/\n\n\nvar current = msg.payload; //get current value\nvar previous = context.get(\"previous\"); //get previous value\n\n\n// if previous is zero or empty, things wont work as expected, return current message\nif (!previous) {\n    return msg;\n}\n\nif (current > (previous*2.0) || current < (previous/2.0)) {\n    msg.payload = previous;\n} \ncontext.set(\"previous\", msg.payload); //update previous\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1320,"y":820,"wires":[["a0817925.854638"]]},{"id":"93c914f8.22df08","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"num","x":1010,"y":760,"wires":[["1809fcef.2ba5c3"]]},{"id":"7f8a9b03.032214","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":1010,"y":800,"wires":[["1809fcef.2ba5c3"]]},{"id":"2ea83b10.a17404","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"num","x":1010,"y":840,"wires":[["1809fcef.2ba5c3"]]},{"id":"610f7260.fae66c","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"10","payloadType":"num","x":1010,"y":880,"wires":[["1809fcef.2ba5c3"]]},{"id":"246850.89f567b","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":1010,"y":1000,"wires":[["1809fcef.2ba5c3"]]},{"id":"6e908273.f1209c","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"150","payloadType":"num","x":1010,"y":1080,"wires":[["1809fcef.2ba5c3"]]},{"id":"a0817925.854638","type":"debug","z":"dd03fa8d.2f4be8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1390,"y":880,"wires":[]},{"id":"8431e5e5.7f8ee8","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":1010,"y":1040,"wires":[["1809fcef.2ba5c3"]]},{"id":"fb1e1824.9c55f8","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"40","payloadType":"num","x":1010,"y":960,"wires":[["1809fcef.2ba5c3"]]},{"id":"517e0819.f62b58","type":"inject","z":"dd03fa8d.2f4be8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"num","x":1010,"y":920,"wires":[["1809fcef.2ba5c3"]]}]

Hello zenofmud,

In your example if left is oldest and right is newest values.

2 would be sent through to influxdb since it is initial value.
3 would be sent through to influxdb since it is within 1 and 3
4 would be sent through since it is within 1.5 and 6
5 would be sent through since within 2 and 8
6 would be sent through since within 2.5 and 10
5 would be sent through since within 3 and 12
4 would be sent through since within 2.5 and 10
3 would be sent through since within 2 and 8
2 would be sent through since within 1.5 and 6
-5 would not go through since it is out of range 1 and 4 then previous value 2 would be sent through to influxdb.
next value after -5 should be within range within 1.5 and 6 otherwise again value 2 would be sent to influxdb

I hope it makes sense

Nope, you have confused me more.

Ok that I get

this confuses me because the previous value 2 times 2 is 4 and 2 * -2 is -4

Can you clarify this? are you sure you don't mead tthat you should check the previous value +2 and -2?
(but that wouldn't explain the #4 and #6 cases)

3 would be sent through because previous value was 2, then:
lower limit would be: 0.5x2=1
higher limit would be: 2x2=4

Hello Steve,

Looks right but for some reason I don't get the same result in my flow. I've underlined incorrect value, which should be 10 since it's 2x larger than previous one.
Here is a screenshot below. Maybe context is somehow turned off by default for me?

what version of node-red do you have? (visible in the node-red menu)

v1.2.5

where does the 0.5 come from??

Hello,
that would be 2 times smaller of previous value (0.5 of previous value, which is 2). It would be the lower threshold.

Ahh I think I know what is going on. You said:

Ok, I did some reading and the way you posed the statement can lead to multiple interpretations and mine didn't match yours :woozy_face:

I think what you mean is "if the current value is less than one half the previous value or more than double the previous value". This matches with what you said the results would be because if the previous value is 3, one half of that is 1.5 and twice that is 6.

Hello Steve,

It worked, I've deleted portion of your code and it worked out

if (!previous) {
    return msg;
}

Thank you for your help and have a great day!

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