Thank you so much guys, I think I go with the last idea, because i want the fastest way for my function to react and detect a steep temperature curve climb.
Sideinfo: I'm trying to extract a "Shower occupied" state from the temperature Data to switch the Shower lights on and off, and for some other automations...
So me and chatgpt trying to get the Array thing running. But now I don't get any outputs anymore. And shouldn't i be able to see my array data in the Context Data Tab? Nothing appears in there. This is my code:
// Get the previous values from the context store
var previousValues = context.get("previousValues") || [];
var threshold = 4; // Temperaturschwelle in Grad Celsius
var timeWindow = 60; // Zeitfenster in Sekunden
var ret;
// Add debug statements here
console.log("Incoming Message Payload:", msg.payload);
var currentTimestamp = Date.now();
var currentValue = parseFloat(msg.payload); // Umwandlung in Gleitkommazahl
if (isNaN(currentValue)) {
console.log("Ungültige Payload:", msg.payload);
} else {
// Holen Sie die vorherigen Werte aus dem Kontext
if (previousValues.length > 0) {
var previousValueBeforeMinute = previousValues[0].value;
var previousTimestampBeforeMinute = previousValues[0].timestamp;
var deltaTime = (currentTimestamp - previousTimestampBeforeMinute) / 1000;
var deltaValue = currentValue - previousValueBeforeMinute;
if (deltaTime <= timeWindow && deltaValue >= threshold) {
ret = "Temperatur erhöht sich um " + deltaValue + " Grad Celsius innerhalb von " + deltaTime + " Sekunden."
}
}
// Aktualisieren Sie die Liste der vorherigen Werte
previousValues.unshift({ value: currentValue, timestamp: currentTimestamp });
// Entfernen Sie alte Werte, die älter als eine Minute sind
var cutoffTimestamp = currentTimestamp - (timeWindow * 1000);
previousValues = previousValues.filter(function (item) {
return item.timestamp >= cutoffTimestamp;
});
// Schreiben Sie die aktualisierte Liste in den Kontext
context.set("previousValues", previousValues);
}
if (ret) return { payload: ret };
return;
and this the test flow:
[{"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 // Holen Sie die vorherigen Werte aus dem Kontext\n if (previousValues.length > 0) {\n var previousValueBeforeMinute = previousValues[0].value;\n var previousTimestampBeforeMinute = previousValues[0].timestamp;\n var deltaTime = (currentTimestamp - previousTimestampBeforeMinute) / 1000;\n var deltaValue = currentValue - previousValueBeforeMinute;\n\n if (deltaTime <= timeWindow && deltaValue >= threshold) {\n ret = \"Temperatur erhöht sich um \" + deltaValue + \" Grad Celsius innerhalb von \" + deltaTime + \" Sekunden.\"\n }\n }\n\n // Aktualisieren Sie die Liste der vorherigen Werte\n previousValues.unshift({ 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;\n","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":1680,"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":1720,"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":1520,"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":1560,"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":1600,"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":1640,"wires":[["df8c984ddabd30e4"]]}]