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

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 :slight_smile: 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

You can use a switch and trigger node to send the Heating ON message after 2 minutes delay:
image

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

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

Hmmm...

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

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

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]

[{"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"}}]

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