Last message comparation with new message

Hi I want to have a function or node to read values from a sensor so it will be like a comparation between the last msg on payload with the new one to se the difference for example if the read from 5 seconds before was 5 and the new one 10 see if the value increase. Some help, examples or anything please.

Use node context in your function node.
Somewhere at the start of your function:

var lastMsg = context.get("lastMsg");
context.set("lastMsg", msg.payload);

this way you now always have the previous value available as lastMsg to compare against.
You can do the same with a switch and a change node. Just use a flow variable instead of the context one above. First check the current message against the flow variable in a switch node and than in the following change node assign the flow variable to the new value.
Dont forget to check for undefined in either case as when the first time message will arrive lastMsg will have no value yet.

Johannes

1 Like

alright I think I'm kind off lost so I will show you to see if I got you.

This is what you say?

[{"id":"1cf581ea.2d93be","type":"function","z":"b5b3dd9f.8e4f4","name":"","func":"var lastMsg = context.get(\"lastMsg\");\ncontext.set(\"lastMsg\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":480,"wires":[["245f920c.3f13fe"]]},{"id":"eee1e704.54dee8","type":"inject","z":"b5b3dd9f.8e4f4","name":"values","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":480,"wires":[["1cf581ea.2d93be"]]},{"id":"245f920c.3f13fe","type":"switch","z":"b5b3dd9f.8e4f4","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":590,"y":480,"wires":[["7e38e71b.157288"]]},{"id":"7e38e71b.157288","type":"change","z":"b5b3dd9f.8e4f4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":760,"y":480,"wires":[["b72127da.d5de98"]]},{"id":"b72127da.d5de98","type":"debug","z":"b5b3dd9f.8e4f4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":480,"wires":[]}]

Here is two simple examples that should give you something to work from:

[{"id":"7782f553.5f6a14","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"fae9e60.83a8e98","type":"function","z":"7782f553.5f6a14","name":"","func":"const lastMsg = context.get(\"lastMsg\");\ncontext.set(\"lastMsg\", msg.payload);\nif (msg.payload === lastMsg) { return null; }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":140,"wires":[["c07e2872.b0d0e"]]},{"id":"a34a854.8f2c878","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":190,"y":140,"wires":[["fae9e60.83a8e98","e158c1c7.4b17e8"]]},{"id":"3c3e5c59.25a864","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"str","x":190,"y":220,"wires":[["fae9e60.83a8e98","e158c1c7.4b17e8"]]},{"id":"c07e2872.b0d0e","type":"debug","z":"7782f553.5f6a14","name":"doing it with a function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":140,"wires":[]},{"id":"e158c1c7.4b17e8","type":"switch","z":"7782f553.5f6a14","name":"","property":"payload","propertyType":"msg","rules":[{"t":"neq","v":"lastMsg","vt":"flow"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":220,"wires":[["21c4ed.0b91b314"]]},{"id":"21c4ed.0b91b314","type":"change","z":"7782f553.5f6a14","name":"","rules":[{"t":"set","p":"lastMsg","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":540,"y":220,"wires":[["38b89bbe.3903d4"]]},{"id":"38b89bbe.3903d4","type":"debug","z":"7782f553.5f6a14","name":"doing it with switch & change","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":790,"y":220,"wires":[]},{"id":"b6f4484.b377cb8","type":"comment","z":"7782f553.5f6a14","name":"Only if the payload changed","info":"","x":240,"y":60,"wires":[]},{"id":"893e0d4e.ef92c","type":"comment","z":"7782f553.5f6a14","name":"Only once the payload changed by five or more compared to the last passed value","info":"","x":410,"y":320,"wires":[]},{"id":"5ab967ea.56a31","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":190,"y":400,"wires":[["c2856659.770968"]]},{"id":"e28e6160.a4f448","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"str","x":190,"y":480,"wires":[["c2856659.770968"]]},{"id":"7fa74445.90e38c","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"8","payloadType":"str","x":190,"y":560,"wires":[["c2856659.770968"]]},{"id":"c2856659.770968","type":"function","z":"7782f553.5f6a14","name":"","func":"const lastMsg = context.get(\"lastMsg\");\nlet diff = msg.payload - lastMsg;\nif (diff < 0) { diff = -diff }\nif (diff < 5) { return null }\ncontext.set(\"lastMsg\", msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":420,"y":560,"wires":[["47815ca1.4e081c"]]},{"id":"47815ca1.4e081c","type":"debug","z":"7782f553.5f6a14","name":"function only five or more difference","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":690,"y":560,"wires":[]},{"id":"c9174fa.e5cd03","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"11","payloadType":"str","x":190,"y":640,"wires":[["c2856659.770968"]]},{"id":"10f62866.047d8","type":"inject","z":"7782f553.5f6a14","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"13","payloadType":"str","x":190,"y":720,"wires":[["c2856659.770968"]]}]
1 Like

I have another cuestion could I use an if in a switch case function ? because I will have two cases when I set my minvalue and maxvalue to open the valve so when the value gets to the max value the valve will close. There will start my second case when the values has to decrease until the values that are coming from my sensor gets to the min value again will open the valve.

Disclaimer:
I dont really know as i dont know how the data looks that comes in and i dont know whats supposed to come out.
Im only giving you methods and strategies here that you need to adapt to your problem.
I cant give you any full solutions as i dont really now your set up or the result your after.

But in principle yes. You can have if statements within a case of a switch statement.

I was trying to figure out how to combine the function from your example with the switch case but it didn't work I think that it what I want. two cases when the values coming in when the valve is open that are increasing and the second case when the valve is close an are decreasing.

this is my funcition

const lastMsg = context.get("lastMsg");
let diff = msg.payload - lastMsg;
switch (diff){
    case diff > 5 :
       context.set("lastMsg", msg.payload);
       return msg;
	
	case diff < -5:
	    context.set("lastMsg", msg.payload);
        return msg;
}

also I tried this but also did't work.

const lastMsg = context.get("lastMsg");
let diff = msg.payload - lastMsg;
function getGrade(diff) {
    let grade;
    // Write your code here
    switch(true) {
        case diff >= 0 && diff <= 5:
        msg.payload = "F";
        break;
        case diff > 5 && diff <= 10:
        msg.payload = "E";
        break;
        case diff > 10 && diff <= 15:
        msg.payload = "D";
        break;
        case diff > 15 && diff <= 20:
        msg.payload = "C";
        break;
        case diff > 20 && diff <= 25:
        msg.payload = "B";
        break;
        case diff > 25 && diff <= 30:
        msg.payload = "A";
        break;
    }

    return msg;
}

this is the flow

[{"id":"95ef4d67.013ba","type":"function","z":"4ba9c110.0dd8d","name":"","func":"const lastMsg = context.get(\"lastMsg\");\nlet diff = msg.payload - lastMsg;\nfunction getGrade(diff) {\n    let grade;\n    // Write your code here\n    switch(true) {\n        case diff >= 0 && diff <= 5:\n        msg.payload = \"F\";\n        break;\n        case diff > 5 && diff <= 10:\n        msg.payload = \"E\";\n        break;\n        case diff > 10 && diff <= 15:\n        msg.payload = \"D\";\n        break;\n        case diff > 15 && diff <= 20:\n        msg.payload = \"C\";\n        break;\n        case diff > 20 && diff <= 25:\n        msg.payload = \"B\";\n        break;\n        case diff > 25 && diff <= 30:\n        msg.payload = \"A\";\n        break;\n    }\n\n    return msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":660,"y":1920,"wires":[["f91c3dc8.c7937"]]},{"id":"f91c3dc8.c7937","type":"debug","z":"4ba9c110.0dd8d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":1920,"wires":[]},{"id":"d1f4e12c.dc661","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":430,"y":1800,"wires":[["95ef4d67.013ba"]]},{"id":"64169f11.135e8","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"str","x":430,"y":1880,"wires":[["95ef4d67.013ba"]]},{"id":"416d55e9.dd751c","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"8","payloadType":"str","x":430,"y":1960,"wires":[["95ef4d67.013ba"]]},{"id":"1f59ac75.258e94","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"11","payloadType":"str","x":430,"y":2040,"wires":[["95ef4d67.013ba"]]},{"id":"4b42062b.a25f48","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"13","payloadType":"str","x":430,"y":2120,"wires":[["95ef4d67.013ba"]]}]

You don't appear to have a context.set in your code.

1 Like

Three things:

  • as @Colin says you never set your last Value
  • you are leaving out a very important line from my example: if (diff < 0) { diff = -diff; } without this line your conditions won’t work when the value is rising as the diff will be a negative value.
  • you define a function but you never call that function anywhere
1 Like

well I tried now with this but still not working

const lastMsg = context.get("lastMsg");
let diff = msg.payload - lastMsg;
if (diff < 0) { diff = -diff; }
function getGrade(diff) {
    let grade;
    // Write your code here
    switch(true) {
        case diff >= 0 && diff <= 5:
        msg.payload = "F";
        context.set("lastMsg", msg.payload);
        break;
        case diff > 5 && diff <= 10:
        msg.payload = "E";
        context.set("lastMsg", msg.payload);
        break;
        case diff > 10 && diff <= 15:
        msg.payload = "D";
        context.set("lastMsg", msg.payload);
        break;
        case diff > 15 && diff <= 20:
        msg.payload = "C";
        context.set("lastMsg", msg.payload);
        break;
        case diff > 20 && diff <= 25:
        msg.payload = "B";
        context.set("lastMsg", msg.payload);
        break;
        case diff > 25 && diff <= 30:
        msg.payload = "A";
        context.set("lastMsg", msg.payload);
        break;
    }

    return msg;
}

also this is not working.

const lastMsg = context.get("lastMsg");
context.set("lastMsg", msg.payload);
switch (msg.payload){
    case msg.payload < lastMsg:
       msg.payload = "F";
       return msg;
	
	case msg.payload > lastMsg:
	    msg.payload = "A";
        return msg;
}

The function getGrade is never called. You have defined it but not called it anywhere. You need, after the diff calculation, to insert
getGrade(diff)

Also I don't think you want to set lastMsg to "A", "B" or whatever, but to the incoming payload instead, so remove the context.set lines within the function and put one after the diff calculation.

You can get rid of the diff < 0 test if instead you use this to calculate the difference
let diff = Math.abs(msg.payload - lastMsg)

2 Likes

Hi thanks to help me. Actually I was thinking a different way to do what I want so this is how I figure out.

[{"id":"e8aa550c.601298","type":"function","z":"4ba9c110.0dd8d","name":"dropdown case","func":"var statusv = global.get(\"valvestatus\");\nconst lastMsg = context.get(\"lastMsg\");\ncontext.set(\"lastMsg\", msg.payload);\nswitch (statusv){\n    case \"OPEN\" :\n       if (msg.payload < lastMsg) { return null; }\n\t   return msg;\n\t\n\tcase \"CLOSE\":\n\t    if (msg.payload > lastMsg) { return null; }\n\t\treturn msg;\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1360,"y":580,"wires":[["55a14ff4.d605a"]]},{"id":"55a14ff4.d605a","type":"debug","z":"4ba9c110.0dd8d","name":"doing it with a function","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1700,"y":560,"wires":[]},{"id":"ca53e1a1.e2871","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":1130,"y":560,"wires":[["e8aa550c.601298"]]},{"id":"dbecb8bd.5bbf18","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"str","x":1130,"y":640,"wires":[["e8aa550c.601298"]]},{"id":"19ab867a.b0014a","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OPEN","payloadType":"str","x":1080,"y":400,"wires":[["7c7bc289.29caec"]]},{"id":"6f873978.a38b38","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"CLOSE","payloadType":"str","x":1100,"y":480,"wires":[["7c7bc289.29caec"]]},{"id":"7c7bc289.29caec","type":"function","z":"4ba9c110.0dd8d","name":"","func":"var status=msg.payload;\n\nglobal.set('valvestatus',status);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1260,"y":440,"wires":[["49fda722.12aee8"]]},{"id":"49fda722.12aee8","type":"debug","z":"4ba9c110.0dd8d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1430,"y":440,"wires":[]},{"id":"294b3c0.24e43c4","type":"inject","z":"4ba9c110.0dd8d","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"str","x":1120,"y":720,"wires":[["e8aa550c.601298"]]}]

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