Periodic Compare

Hi - I'm trying to figure out how to approach the following problem. I have a flow which uses a Raspberry Pi to control an output, I use an input to get feedback from the device being controlled.

All being well - the input follows the output - but... what I want to do is periodically (1 hz) compare the status of the input and output - if they are not matched for more than 2 cycles (so 2 seconds) then I want to trigger an action.

I planned to use a join node with 2 status nodes but I can't figure out how to have this accumulate 2 cycles before triggering.

This is what I have so far - but this will only output a message when the status of the I/O changes. I want to do the check independently. (at the bottom - ignore the rest of the flow - just experimenting)

[{"id":"de2d441d.e51f68","type":"status","z":"d0a0c3ed.6a34b","name":"RPi_Out","scope":["11a8e39c.f4df0c"],"x":80,"y":500,"wires":[["ede3f672.6b63b8"]]},{"id":"d24a3029.7cc5c","type":"status","z":"d0a0c3ed.6a34b","name":"RPi_In","scope":["89f1a0ed.f4fe8"],"x":90,"y":540,"wires":[["ede3f672.6b63b8"]]},{"id":"ede3f672.6b63b8","type":"join","z":"d0a0c3ed.6a34b","name":"Check","mode":"custom","build":"array","property":"","propertyType":"full","key":"topic","joiner":"msg:topic","joinerType":"str","accumulate":false,"timeout":"5","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":250,"y":520,"wires":[["f7fcde79.829da"]]},{"id":"f7fcde79.829da","type":"debug","z":"d0a0c3ed.6a34b","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":380,"y":520,"wires":[]}]

You can something like this. The inject nodes are to simulate your inputs for testing. Note that I have used key/value pairs in the Join node so the in and out values can be distinguished.

[{"id":"9cb9ac8a.3c7438","type":"inject","z":"bdd7be38.d3b55","name":"out 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":1313,"wires":[["81625b69.36f2d8"]]},{"id":"3dbc4df6.e01d6a","type":"inject","z":"bdd7be38.d3b55","name":"out 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":89,"y":1352,"wires":[["81625b69.36f2d8"]]},{"id":"84dde60b.ad4b68","type":"inject","z":"bdd7be38.d3b55","name":"in 1","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":87,"y":1426,"wires":[["e9649e8.83116e"]]},{"id":"54ce1754.94963","type":"inject","z":"bdd7be38.d3b55","name":"in 0","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":91,"y":1472,"wires":[["e9649e8.83116e"]]},{"id":"bb2fe387.a7d098","type":"join","z":"bdd7be38.d3b55","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":385,"y":1392,"wires":[["76f39fca.1728e8"]]},{"id":"76f39fca.1728e8","type":"function","z":"bdd7be38.d3b55","name":"Compare","func":"// set msg.reset if out == in\nif (msg.payload.out == msg.payload.in) {\n    msg.reset = true\n}\nreturn msg;","outputs":1,"noerr":0,"x":527,"y":1392,"wires":[["21d54a01.2111f6"]]},{"id":"21d54a01.2111f6","type":"trigger","z":"bdd7be38.d3b55","op1":"","op2":"Notify","op1type":"nul","op2type":"str","duration":"2","extend":false,"units":"s","reset":"","bytopic":"all","name":"Wait 2 secs","x":684,"y":1391,"wires":[["4d09b891.b476a8"]]},{"id":"4d09b891.b476a8","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":607,"y":1457,"wires":[]},{"id":"81625b69.36f2d8","type":"change","z":"bdd7be38.d3b55","name":"topic: out","rules":[{"t":"set","p":"topic","pt":"msg","to":"out","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":227,"y":1333,"wires":[["bb2fe387.a7d098"]]},{"id":"e9649e8.83116e","type":"change","z":"bdd7be38.d3b55","name":"topic: in","rules":[{"t":"set","p":"topic","pt":"msg","to":"in","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":229,"y":1440,"wires":[["bb2fe387.a7d098"]]}]

Hi Colin - That's perfect - thank you very much. I'll spend a bit of time going through that so I can get a handle on how to do things (new to Node-Red)

Many thanks