How can I determine if a payload value is unchanged for five minutes?

How would you determine if a sensor value is unchanged for a given time?

Read it and store it in a flow or global variable, a few minutes later read it again and compare the two values. If they are the same quit, if they are different “sound the alarms” :rofl: and move the latest value to the flow/global variable.

I thought of that, but it's a continuous stream of data. About 5 seconds between readings. If I store a value and get, for example 100, and over the next few minutes the value goes up to 105 and down to 100 and the second reading is taken. The algorithm says "nothing changed" when the data really is changing.

But. It's better than nothing. Thanks for the tip.

How does your data come in? Is it "pushed" -- say by an MQTT subscribe or a serial port message. Or is it "polled" where you read it periodically?

If its pushed, have an RBE node pass the data only if its changed. Then have this feed a trigger node set for 5 minutes in "watchdog" mode to send if its not been triggerd during the interval.

If its polled, you will probably need to decrease your polling interval and let it feed the RBE node to "create" the pushed data.

Put a function node in there with a stored variable and you can do pretty much anything you can dream up as zenofmud alludes.

I thought about RBE. It may yet be my solution.
The data is polled.

Here is where I am at:
I separate the max and min values using the smooth node, then test them. If they are the same then the value has not changed.

Here is where I am having a problem:

var a=flow.get('max') || 0;
var b=flow.get('min') || 0;

msg.payload = false;
if (a-b === 0 ){
    msg.payload = true;
}
return msg;

This function always returns "true" even if the vars are different.

Where are you setting max and min? Can the smooth node set flow variables?

You could try something like below the property msg.sameFive will be true if payload has been same for five minutes, else it returns false.

[{"id":"aeedaf20.c1b9f","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"sameFive","pt":"msg","to":"$millis() - $flowContext(\"previous.time\")  > 300000 and payload = $flowContext(\"previous.payload\")","tot":"jsonata"},{"t":"set","p":"previous.time","pt":"flow","to":"payload != $flowContext(\"previous.payload\") ? $millis() : $flowContext(\"previous.time\")","tot":"jsonata"},{"t":"set","p":"previous.payload","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":2540,"wires":[["b0b8b129.ac52a8"]]},{"id":"8bd4840.459f","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":170,"y":2540,"wires":[["aeedaf20.c1b9f"]]},{"id":"ce7cd8ca.968f","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":180,"y":2580,"wires":[["aeedaf20.c1b9f"]]},{"id":"b0b8b129.ac52a8","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"sameFive","statusType":"msg","x":610,"y":2560,"wires":[]}]

They are coming from two smooth nodes. They are set to ten iterations and output either the highest or lowest value seen respectively.

My question is why is if (a-b === 0 ) always true regardless of the values for a and b?

Hard to tell as we have no idea what your context store values are, but do you ever, or have you ever set flow max or min to a value, if not the 0-0 === 0.

Thanks- I haven't worked with JSonata before- now's a good time to learn.

I checked a and b with debug nodes, they are numbers in the range of 100 to 200.

The deug will not hold the context values you need to check the context panel to see what flow.max and min's values are. As you are setting a and b to context values.
[edit]
try this

var a=flow.get('max') || 0;
var b=flow.get('min') || 0;
node.warn(`a equals ${a} and b equals ${b}`);
msg.payload = false;
if (a-b === 0 ){
    msg.payload = true;
}
return msg;
1 Like

Thanks for the tip- I have never used the node.warn node.
But it helped me find my problem.
doh(300)

I was saving my min and max in global.set, and in the function I was doing a flow.get. So, yes, they would always be zero.

3 Likes

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