I receive a payload value that rises every second, after a while it'll stop for a moment and reset to 0 again.
What I want to do is capture that max value.
I'm struggling to hold onto the last value to compare it against the new one.
I receive a payload value that rises every second, after a while it'll stop for a moment and reset to 0 again.
What I want to do is capture that max value.
I'm struggling to hold onto the last value to compare it against the new one.
You will need to save the last value. You could use a function
node to save the value as a node variable and check it each time a msg comes thru or you could use a switch
node and a change
node to test and save (if the new value is greater) the value as a flow variable.
If you haven't worked with context, take a read of the documentation here: Working with context : Node-RED
You can also look at the smooth node - as that also can return the max value seen (as well as mean, min, etc) - and can be reset by a msg.reset message.
Solution:
pl = msg.payload.CycleTimeMach;
pl2 = msg.payload.CycleTimeMan;
avg = flow.get("AvgOldMach.S8")
avg2 = flow.get("AvgOldMan.S8")
if (pl < avg) {
flow.set("AvgNewMach", {S8: avg})
}
if (pl2 < avg2) {
flow.set("AvgNewMan", {S8: avg})
}
flow.set("AvgOldMach", {S8: pl})
flow.set("AvgOldMan", {S8: pl2})
I was trying to do it with the flow context last night but my head just wouldn't compute. I guess all I really needed was a good coffee and fresh eyes.
Edit:
Probably worth adding that I setup the context in another node using
flow.set("AvgOldMach", {S8: 0})
flow.set("AvgNewMach", {S8: 0})
flow.set("AvgOldMan", {S8: 0})
flow.set("AvgNewMan", {S8: 0})
from a one-hit timestamp injection that fires every time the flow starts
In the function
node you could use this
avg = flow.get("AvgOldMach.S8") || 0 // get flow var, it it doesn't exist use 0
avg2 = flow.get("AvgOldMan.S8") || 0 // get flow var, it it doesn't exist use 0
and eliminate the need to have the other node to set up the context.
Ah brilliant, I couldn't figure out how to achieve this.
thanks
The users guide is very informative: Writing Functions : Node-RED
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.