@thomashsk Hier auch nochmal eine Version mit geometrischen und arithmetischem Mittelwert:
[image: 1674768866102-92f303dc-b2ee-41de-84b9-21227adc5e8d-image-resized.png]
Spoiler
[
{
"id": "312b30368688d654",
"type": "function",
"z": "289f539dcc33814e",
"name": "arithm. Mittelwert",
"func": "var arr = context.get(\"values\") || [];\n\nif (msg.reset && arr.length > 0) {\n\n msg.payload = arr.reduce((/** @type {any} */ total,/** @type {any} */ value) => total + value ) / arr.length; \n context.set(\"values\", []);\n return msg;\n}\n\nvar value = Number(msg.payload);\nif (!isNaN(value)){\n arr.push(value);\n context.set(\"values\", arr);\n}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 650,
"y": 3120,
"wires": [
[
"b5a8e6d648d9ff98"
]
]
},
{
"id": "b5a8e6d648d9ff98",
"type": "debug",
"z": "289f539dcc33814e",
"name": "Ausgabe arith Mittelwert",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 910,
"y": 3120,
"wires": []
},
{
"id": "38011f1a68961bc5",
"type": "inject",
"z": "289f539dcc33814e",
"name": "",
"props": [
{
"p": "payload"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "6",
"payloadType": "num",
"x": 350,
"y": 3100,
"wires": [
[
"62f8d65a3c45681f"
]
]
},
{
"id": "8da2f3ad1ffce675",
"type": "inject",
"z": "289f539dcc33814e",
"name": "reset gibt Mittelwert aus",
"props": [
{
"p": "reset",
"v": "true",
"vt": "bool"
}
],
"repeat": "",
"crontab": "59 23 * * *",
"once": false,
"onceDelay": 0.1,
"topic": "",
"x": 310,
"y": 3180,
"wires": [
[
"4fefb2a427b83dcd"
]
]
},
{
"id": "fc708604b390381a",
"type": "function",
"z": "289f539dcc33814e",
"name": "geom. Mittelwert",
"func": "var arr = context.get(\"values\") || [];\n\nif (msg.reset && arr.length > 0) {\n\n msg.payload = Math.pow(arr.reduce((/** @type {any} */ total,/** @type {any} */ value) => total * value ), 1 / arr.length); \n context.set(\"values\", []);\n return msg;\n}\n\nvar value = Number(msg.payload);\nif (!isNaN(value)){\n arr.push(value);\n context.set(\"values\", arr);\n}\n",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 640,
"y": 3160,
"wires": [
[
"b7832abc04318141"
]
]
},
{
"id": "b7832abc04318141",
"type": "debug",
"z": "289f539dcc33814e",
"name": "Ausgabe geom. Mittelwert",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 890,
"y": 3160,
"wires": []
},
{
"id": "4fefb2a427b83dcd",
"type": "junction",
"z": "289f539dcc33814e",
"x": 460,
"y": 3180,
"wires": [
[
"fc708604b390381a",
"312b30368688d654"
]
]
},
{
"id": "62f8d65a3c45681f",
"type": "junction",
"z": "289f539dcc33814e",
"x": 460,
"y": 3100,
"wires": [
[
"fc708604b390381a",
"312b30368688d654"
]
]
}
]
Das geometrische Mittel wird einfach berechnet nach indem alle Werte multipliziert werden und die n-te Wurzel gezogen wird, wobei n die Anzahl der Elemente sind.
var arr = context.get("values") || [];
if (msg.reset && arr.length > 0) {
msg.payload = Math.pow(arr.reduce((/** @type {any} */ total,/** @type {any} */ value) => total * value ), 1 / arr.length);
context.set("values", []);
return msg;
}
var value = Number(msg.payload);
if (!isNaN(value)){
arr.push(value);
context.set("values", arr);
}
Zusätzlich noch eine kleine Verbesserung in dem Code - so dass reset beim leeren Array keine Fehler mehr schmeisst.