Within xxx seconds

Hi all.
I'm new to nodered (I'm using NR 7.2.10 inside HA) so please be patient.

I need to generate an event, triggered by the reception of two MQTT messages. Regardless of the rx order it's important for me that, in order to generate the event, the second message should arrive within xxx seconds.

Let me do an example:

if MQTTMESS1_is_received() {
    start timer()
    while (timer < xxx seconds) {
        if MQTTMESS2_is_received() {
            GENERATE_EVENT()
        }
    }
}
timer_stop()

It's not important which is the message that arrives before. Both messages should start the timer..

Someone could give me some suggestion ?

Thanks.

This seems to work for me:

[{"id":"d74d10e6.1767","type":"join","z":"fbf14815.771e68","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":750,"y":760,"wires":[["c4934972.d8a75"]]},{"id":"118f1320.d242b5","type":"debug","z":"fbf14815.771e68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1210,"y":700,"wires":[]},{"id":"76806418.4ee22c","type":"inject","z":"fbf14815.771e68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"faketopic1","payload":"1","payloadType":"str","x":310,"y":760,"wires":[["d0dc7858.d8a48"]]},{"id":"29a0fee5.5597a2","type":"inject","z":"fbf14815.771e68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"faketopic2","payload":"1","payloadType":"str","x":310,"y":820,"wires":[["d0dc7858.d8a48"]]},{"id":"d0dc7858.d8a48","type":"change","z":"fbf14815.771e68","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t    \"payload\": payload,\t    \"topic\": topic,\t    \"timestamp\": $millis()\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":760,"wires":[["d74d10e6.1767"]]},{"id":"c4934972.d8a75","type":"switch","z":"fbf14815.771e68","name":"Within 2 seconds","property":"payload[1].timestamp - payload[0].timestamp","propertyType":"jsonata","rules":[{"t":"lt","v":"2000","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":950,"y":760,"wires":[["118f1320.d242b5"],["67d1e00c.78a0c8"]]},{"id":"67d1e00c.78a0c8","type":"debug","z":"fbf14815.771e68","name":"IGNORED","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1200,"y":780,"wires":[]}]

Here's another variant using the Batch node, this one will work for every subsequent message:

[{"id":"9373d0dc.2fc5f","type":"batch","z":"fbf14815.771e68","name":"","mode":"count","count":"2","overlap":"1","interval":"2","allowEmptySequence":false,"topics":[],"x":850,"y":1120,"wires":[["e2cf7a3a.5137f8"]]},{"id":"eb0e0b9f.bfdac","type":"inject","z":"fbf14815.771e68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"faketopic1","payload":"1","payloadType":"str","x":470,"y":1120,"wires":[["b8ba37a6.5b2588"]]},{"id":"99e4fc4d.c5cbd8","type":"debug","z":"fbf14815.771e68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1450,"y":1120,"wires":[]},{"id":"4d5a2fa.d93fed","type":"inject","z":"fbf14815.771e68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"faketopic2","payload":"2","payloadType":"str","x":470,"y":1160,"wires":[["b8ba37a6.5b2588"]]},{"id":"e2cf7a3a.5137f8","type":"join","z":"fbf14815.771e68","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":1030,"y":1120,"wires":[["d541cf27.d56248"]]},{"id":"b8ba37a6.5b2588","type":"change","z":"fbf14815.771e68","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"{\t    \"payload\": payload,\t    \"topic\": topic,\t    \"timestamp\": $millis()\t}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":660,"y":1120,"wires":[["9373d0dc.2fc5f"]]},{"id":"d541cf27.d56248","type":"switch","z":"fbf14815.771e68","name":"Within 2 seconds","property":"payload[1].timestamp - payload[0].timestamp","propertyType":"jsonata","rules":[{"t":"lt","v":"2000","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1230,"y":1120,"wires":[["99e4fc4d.c5cbd8"],[]]}]

Here is one that relies on the join nodes time out and a switch node to count the payloads

[{"id":"76806418.4ee22c","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mqtt/topic/1","payload":"a payload","payloadType":"str","x":155.33331298828125,"y":2938,"wires":[["d74d10e6.1767"]]},{"id":"d74d10e6.1767","type":"join","z":"c74669a0.6a34f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"2","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":555.3333129882812,"y":2938,"wires":[["bd409431.b55458"]]},{"id":"29a0fee5.5597a2","type":"inject","z":"c74669a0.6a34f8","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"mqtt/topic/2","payload":"a payload 2","payloadType":"str","x":165.33331298828125,"y":2998,"wires":[["d74d10e6.1767"]]},{"id":"bd409431.b55458","type":"switch","z":"c74669a0.6a34f8","name":"","property":"$count($$.payload.*)","propertyType":"jsonata","rules":[{"t":"eq","v":"2","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":745.3333129882812,"y":2938,"wires":[["118f1320.d242b5"]]},{"id":"118f1320.d242b5","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":955.3333129882812,"y":3018,"wires":[]}]

It should ignore repeated topics and just allow two different topics with x seconds.

Many thanks to all !

Building flows without using scripts, initially takes a lot of effort but readability is at the top.

I'm just wondering: speaking in terms of computational load, which is better ? Nodes or scripts inside function ?

Thanks!

As I understand it, a node is really just someone's script in a custom package... so I would say it is basically the same.

1 Like

There is a small overhead in function nodes, but unless you are running it thousands of times a second then don't worry about it.

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