# Continue a Flow after same Message recived for x times in x seconds

**URL:** <https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486>\
**Category:** General\
**Created:** [9 April 2025 06:16 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486 "2025-04-09T06:16:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matzeeg3](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@matzeeg3](https://discourse.nodered.org/u/matzeeg3)\
**Post date:** [9 April 2025 06:16 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486/1 "2025-04-09T06:16:05Z")

</div>

Hi everybody,  
i try to build a flow that enables or disables a Pool Heater.  
The flow has different variables suche as solar produiction batterie soc and so and. the flow worked as i expected it.  
now to my last little problem 🙂 i like to have a little break in the flow.  
for example if the solar porduction raises above 1200W the heater should turn on but only after the solar production has read 5 till den time above 1200W in 2 mins.  
i tryed many things but i don´t get it running.  
may someone can help me?  
thanks in advanced  
kind regards  
matze

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [9 April 2025 07:58 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486/2 "2025-04-09T07:58:11Z")

</div>

You can use a switch and trigger node to send the Heating ON message after 2 minutes delay:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/daa7e6ceb61dd4ac8e534405d9c2f76e9f785b2e.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f92e7ad379d9969db06b92198d93df6832a9472b.png)

And you can use a change node to set msg.reset when the input falls below 1200

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/9/49e21da07c0b801acf86f108efa8635c44c1ea52.png)

> [@matzeeg3](#):
>
> same Message recived for x times in x seconds

This is more difficult since you will need to maintain a counter.  
Does the suggestion I posted above not meet your requirement?

---

<div class="post-metadata">

**Author:** ![kitori](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@kitori](https://discourse.nodered.org/u/kitori)\
**Post date:** [9 April 2025 08:12 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486/3 "2025-04-09T08:12:35Z")

</div>

Hmmm...

First function node: save values in an array for a threshold:

```auto
let counter = context.get("counter")

const payload = msg.payload
const now = Date.now()

const THRESHOLD_S = 10 // CHANGE THIS

const THRESHOLD_MILLIS = THRESHOLD_S * 1000
const LOWER_LIMIT = now - THRESHOLD_MILLIS

counter.push({ ts: now, payload: payload })

const result = counter.filter((d) => d.ts > LOWER_LIMIT);

context.set("counter", result)

msg.payload = result

```

Seconds function node: calculate the average value of the array. only return a value, if there are min 5 elements. Then you can use a switch node

```auto
const counter = msg.payload

if (counter.length >= 5) {
    const average = counter.reduce((total, next) => total + next.payload, 0) / counter.length || 0
    flow.set("mov_avg", average);
    msg.payload = average

    
} else {
    msg.payload = 0
    flow.set("mov_avg", 0);
}

msg.topic = "mov_avg"

return [msg]

```

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/053490b325823d120b4c27b321a069ba0ac3e54e.png)

```auto
[{"id":"64a746a022e9cb73","type":"inject","z":"1bc8ef14da938222","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"power","payload":"power","payloadType":"flow","x":190,"y":140,"wires":[["b8e53938ffde04f5","6ebfbe869d32f7b1"]]},{"id":"b8e53938ffde04f5","type":"ui-chart","z":"1bc8ef14da938222","group":"10dbfcaff36a3f43","name":"","label":"chart","order":2,"chartType":"line","category":"topic","categoryType":"msg","xAxisLabel":"","xAxisProperty":"","xAxisPropertyType":"property","xAxisType":"time","xAxisFormat":"","xAxisFormatType":"auto","yAxisLabel":"","yAxisProperty":"","ymin":"","ymax":"","action":"append","stackSeries":false,"pointShape":"circle","pointRadius":4,"showLegend":true,"removeOlder":"5","removeOlderUnit":"60","removeOlderPoints":"60","colors":["#0095ff","#ff0000","#ff7f0e","#2ca02c","#a347e1","#d62728","#ff9896","#9467bd","#c5b0d5"],"textColor":["#666666"],"textColorDefault":true,"gridColor":["#e5e5e5"],"gridColorDefault":true,"width":6,"height":8,"className":"","x":1270,"y":140,"wires":[[]]},{"id":"89c547d001b617e1","type":"ui-chart","z":"1bc8ef14da938222","group":"10dbfcaff36a3f43","name":"","label":"chart","order":3,"chartType":"line","category":"topic","categoryType":"msg","xAxisLabel":"","xAxisProperty":"","xAxisPropertyType":"property","xAxisType":"time","xAxisFormat":"","xAxisFormatType":"auto","yAxisLabel":"","yAxisProperty":"","ymin":"","ymax":"","action":"append","stackSeries":false,"pointShape":"circle","pointRadius":4,"showLegend":true,"removeOlder":"5","removeOlderUnit":"60","removeOlderPoints":"60","colors":["#0095ff","#ff0000","#ff7f0e","#2ca02c","#a347e1","#d62728","#ff9896","#9467bd","#c5b0d5"],"textColor":["#666666"],"textColorDefault":true,"gridColor":["#e5e5e5"],"gridColorDefault":true,"width":6,"height":8,"className":"","x":1270,"y":280,"wires":[[]]},{"id":"721b7f0f01b6a757","type":"ui-slider","z":"1bc8ef14da938222","group":"10dbfcaff36a3f43","name":"","label":"slider","tooltip":"","order":1,"width":0,"height":0,"passthru":false,"outs":"all","topic":"topic","topicType":"msg","thumbLabel":"true","showTicks":"always","min":0,"max":"2000","step":"100","className":"","iconPrepend":"","iconAppend":"","color":"","colorTrack":"","colorThumb":"","x":410,"y":420,"wires":[["7bc95104d2e13cc5"]]},{"id":"7bc95104d2e13cc5","type":"change","z":"1bc8ef14da938222","name":"","rules":[{"t":"set","p":"power","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":620,"y":420,"wires":[[]]},{"id":"6ebfbe869d32f7b1","type":"function","z":"1bc8ef14da938222","name":"save last values","func":"let counter = context.get(\"counter\")\n\nconst payload = msg.payload\nconst now = Date.now()\n\nconst THRESHOLD_S = 10 // CHANGE THIS\n\nconst THRESHOLD_MILLIS = THRESHOLD_S * 1000\nconst LOWER_LIMIT = now - THRESHOLD_MILLIS\n\ncounter.push({ ts: now, payload: payload })\n\nconst result = counter.filter((d) => d.ts > LOWER_LIMIT);\n\ncontext.set(\"counter\", result)\n\nmsg.payload = result\n\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"// Der Code hier wird ausgeführt,\n// wenn der Node gestartet wird\ncontext.set(\"counter\",[])","finalize":"","libs":[],"x":380,"y":220,"wires":[["9673144e7e63928b"]]},{"id":"9673144e7e63928b","type":"function","z":"1bc8ef14da938222","name":"analyze","func":"const counter = msg.payload\n\nif (counter.length >= 5) {\n const average = counter.reduce((total, next) => total + next.payload, 0) / counter.length || 0\n flow.set(\"mov_avg\", average);\n msg.payload = average\n\n \n} else {\n msg.payload = 0\n flow.set(\"mov_avg\", 0);\n}\n\n\nmsg.topic = \"mov_avg\"\n\nreturn [msg]","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":560,"y":220,"wires":[["b8e53938ffde04f5","a24779010b746aed"]]},{"id":"a24779010b746aed","type":"switch","z":"1bc8ef14da938222","name":"","property":"payload","propertyType":"msg","rules":[{"t":"gt","v":"1200","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":710,"y":280,"wires":[["366bfbdda8a6d323"],["61dbcac03cfae8ca"]]},{"id":"366bfbdda8a6d323","type":"change","z":"1bc8ef14da938222","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":240,"wires":[["89c547d001b617e1"]]},{"id":"61dbcac03cfae8ca","type":"change","z":"1bc8ef14da938222","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":870,"y":320,"wires":[["89c547d001b617e1"]]},{"id":"10dbfcaff36a3f43","type":"ui-group","name":"Group Name","page":"be8a2dc2ed454530","width":6,"height":1,"order":1,"showTitle":true,"className":"","visible":"true","disabled":"false"},{"id":"be8a2dc2ed454530","type":"ui-page","name":"Test","ui":"174e44d306da2771","path":"/page2","icon":"home","layout":"grid","theme":"171855b186577ac4","order":1,"className":"","visible":"true","disabled":"false"},{"id":"174e44d306da2771","type":"ui-base","name":"My Dashboard","path":"/dashboard","includeClientData":true,"acceptsClientConfig":["ui-notification","ui-control","ui-button"],"showPathInSidebar":false,"showPageTitle":true,"navigationStyle":"default","titleBarStyle":"default"},{"id":"171855b186577ac4","type":"ui-theme","name":"Default Theme","colors":{"surface":"#ffffff","primary":"#0094CE","bgPage":"#eeeeee","groupBg":"#ffffff","groupOutline":"#cccccc"},"sizes":{"pagePadding":"12px","groupGap":"12px","groupBorderRadius":"4px","widgetGap":"12px"}}]

```

---

<div class="post-metadata">

**Author:** ![matzeeg3](https://avatars.discourse-cdn.com/v4/letter/m/f6c823/32.png) [@matzeeg3](https://discourse.nodered.org/u/matzeeg3)\
**Post date:** [9 April 2025 13:53 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486/4 "2025-04-09T13:53:18Z")

</div>

hi i did not get the flow running. i don´t have a node-red gui is that the problem?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [8 July 2025 13:54 UTC](https://discourse.nodered.org/t/continue-a-flow-after-same-message-recived-for-x-times-in-x-seconds/96486/5 "2025-07-08T13:54:06Z")

</div>

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