Trying to write a Function Node for monitoring temperatures

Interesting that the temperature rises almost linearly with very little sign of tailing off, and it doesn't seem to be maintained at the maximum for a significant period.

Stable State whilst the day

Decresing Part, highest Zoom Level

It does - of course. Inject "20", wait a few seconds, then inject "34". You'll get your message.
The issue I tried to put your attention to is rather that you'd not detect when the temperature level change peaks and falls back (up - down) within the timespan you're monitoring. You'll need additional logic if you intend to cover that case.

1 Like
[{"id":"df8c984ddabd30e4","type":"function","z":"65879b662c0554cb","name":"Temperatur stieg um 5° innerhab 1 Minute","func":"// Get the previous values from the context store\nvar previousValues = context.get(\"previousValues\") || [];\nvar threshold = 4; // Temperaturschwelle in Grad Celsius\nvar timeWindow = 60; // Zeitfenster in Sekunden\n\nvar ret;\n\n// Add debug statements here\nconsole.log(\"Incoming Message Payload:\", msg.payload);\n\nvar currentTimestamp = Date.now();\nvar currentValue = parseFloat(msg.payload); // Umwandlung in Gleitkommazahl\n\nif (isNaN(currentValue)) {\n    console.log(\"UngĂŒltige Payload:\", msg.payload);\n} else {\n    // Suchen Sie den am weitesten entfernten Wert innerhalb einer Minute\n    var furthestValue = null;\n\n    for (var i = 0; i < previousValues.length; i++) {\n        if (currentTimestamp - previousValues[i].timestamp <= timeWindow * 1000) {\n            furthestValue = previousValues[i].value;\n            node.warn(furthestValue);\n        } else {\n            // Wenn der Wert Ă€lter als eine Minute ist, brechen Sie die Schleife ab\n            break;\n        }\n    }\n\n    \n\n    // Wenn ein Wert gefunden wurde, vergleichen Sie ihn mit dem aktuellen Wert\n    if (furthestValue !== null) {\n        var deltaValue = currentValue - furthestValue;\n\n        if (deltaValue >= threshold) {\n            ret = \"Temperatur erhöht sich um \" + deltaValue + \" Grad Celsius innerhalb einer Minute.\";\n        }\n    }\n\n    // Aktualisieren Sie die Liste der vorherigen Werte\n    previousValues.push({ value: currentValue, timestamp: currentTimestamp });\n\n    // Entfernen Sie alte Werte, die Ă€lter als eine Minute sind\n    var cutoffTimestamp = currentTimestamp - (timeWindow * 1000);\n    previousValues = previousValues.filter(function (item) {\n        return item.timestamp >= cutoffTimestamp;\n    });\n\n    // Schreiben Sie die aktualisierte Liste in den Kontext\n    context.set(\"previousValues\", previousValues);\n}\n\nif (ret) return { payload: ret };\nreturn;","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":1680,"wires":[["bd638a3a8d3097ce"]]},{"id":"bd638a3a8d3097ce","type":"debug","z":"65879b662c0554cb","name":"debug 17","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":740,"y":1680,"wires":[]},{"id":"573256e42d0bd41b","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"24","payloadType":"str","x":110,"y":1740,"wires":[["df8c984ddabd30e4"]]},{"id":"93262112d7fb3756","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"34","payloadType":"str","x":110,"y":1780,"wires":[["df8c984ddabd30e4"]]},{"id":"96c056c3a0a84ee4","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"20","payloadType":"str","x":110,"y":1580,"wires":[["df8c984ddabd30e4"]]},{"id":"041aefd845cc3632","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"21","payloadType":"str","x":110,"y":1620,"wires":[["df8c984ddabd30e4"]]},{"id":"2340c5a79093b489","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"22","payloadType":"str","x":110,"y":1660,"wires":[["df8c984ddabd30e4"]]},{"id":"05bc41703f604c86","type":"inject","z":"65879b662c0554cb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"23.3","payloadType":"str","x":110,"y":1700,"wires":[["df8c984ddabd30e4"]]}]

With this, I don't get any :frowning:

The code you have under

    // Suchen Sie den am weitesten entfernten Wert innerhalb einer Minute

is completely useless and not at all necessary. When you push (new) incoming data to the (back of the) array, the oldest value will always be at index 0. Values that fall out of your monitoring window will be removed by the code under

    // Entfernen Sie alte Werte, die Àlter als eine Minute sind

keeping the array crisp - with the oldest value valid for you monitoring window always at index 0.

Keep the code as it was (using push!), inject the values, enjoy the result!

1 Like

OMG turns out, that there was sth. wrong with my debug node, so it doesn't show anything. Reloaded the page and it works. Sorry for the inconvinience and thank you so much for your help!

Since you have not included the scale in the first image it is not possible to know the size of the steps.

1 Like

That's right and I think I can explain it:
Sensor is on the outside of the pipe, not washed around by the fluid and showering usually doesn't last long. So it increases whilst the water is flowing and if the fresh water gets stopped, it starts decreasing...

My bad, sorry:

here it is at steady state

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