Ich habe das mangels Batterie nicht testen können.
Das Skript geht die Batterien nach und nach durch und startet automatisch wieder, wenn ich die Verbindung unterbreche.
Den Comport musste ich natürlich umbiegen.
[
{
"id": "26771494c726dd85",
"type": "tab",
"label": "Pytes",
"disabled": false,
"info": "",
"env": []
},
{
"id": "fe3238beacae91ad",
"type": "junction",
"z": "26771494c726dd85",
"x": 340,
"y": 180,
"wires": [
[
"b28980471e986893",
"fdaee2e16d78cc87"
]
]
},
{
"id": "665e7ef72b226bc2",
"type": "junction",
"z": "26771494c726dd85",
"x": 780,
"y": 260,
"wires": [
[
"65a7b88c58ac941a"
]
]
},
{
"id": "65a7b88c58ac941a",
"type": "junction",
"z": "26771494c726dd85",
"x": 60,
"y": 260,
"wires": [
[
"6d9a8c4540741cbf"
]
]
},
{
"id": "79724f85531596e4",
"type": "function",
"z": "26771494c726dd85",
"name": "convert to msgs",
"func": "const text = msg.payload;\n\nif (typeof text !== \"string\") {\n node.error(\"msg.payload ist kein String\", msg);\n return null;\n}\n\n// In einzelne Zeilen zerlegen\nconst lines = text\n .split(/\\r?\\n/)\n .map(line => line.trim())\n .filter(line => line !== \"\");\n\n// Nur Zeilen berücksichtigen, die mit einer Batterienummer beginnen\nconst dataLines = lines.filter(line => /^\\d+\\s/.test(line));\n\nconst messages = [];\n\nfor (const line of dataLines) {\n\n // Spalten sind durch mindestens 2 Leerzeichen getrennt\n const columns = line.split(/\\s{2,}/);\n\n if (columns.length < 9) {\n node.warn(\"Zeile konnte nicht verarbeitet werden: \" + line);\n continue;\n }\n\n const battery = {\n battery: Number(columns[0]),\n volt: Number(columns[1]),\n tempr: Number(columns[2]),\n baseState: columns[3],\n voltState: columns[4],\n currState: columns[5],\n tempState: columns[6],\n coulomb: Number(columns[7].replace(\"%\", \"\")),\n capacity: Number(columns[8].replace(/[^\\d]/g, \"\"))\n };\n\n messages.push({\n ...msg,\n payload: battery\n });\n}\n\n// Alle Nachrichten nacheinander über denselben Ausgang senden\nreturn [messages];",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 940,
"y": 80,
"wires": [
[
"4a8dcae7d0f7d8a8"
]
]
},
{
"id": "68c635b31d513d6d",
"type": "debug",
"z": "26771494c726dd85",
"name": "debug 4",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 1260,
"y": 160,
"wires": []
},
{
"id": "6a16c00e7b0e2997",
"type": "ioBroker out",
"z": "26771494c726dd85",
"name": "",
"topic": "",
"ack": "true",
"autoCreate": "true",
"stateName": "",
"role": "",
"payloadType": "",
"readonly": "",
"stateUnit": "",
"stateMin": "",
"stateMax": "",
"x": 1280,
"y": 80,
"wires": []
},
{
"id": "4a8dcae7d0f7d8a8",
"type": "function",
"z": "26771494c726dd85",
"name": "function 2",
"func": "const battery = msg.payload;\n\nif (!battery || battery.battery === undefined) {\n node.error(\"Ungültiges Battery-Objekt\", msg);\n return null;\n}\n\nconst batteryId = battery.battery;\n\nconst states = [\n {\n id: \"voltage\",\n value: battery.volt,\n name: \"Voltage\",\n role: \"value.voltage\",\n type: \"number\",\n readonly: true,\n unit: \"mV\",\n min: 0\n },\n {\n id: \"temperature\",\n value: battery.tempr / 1000,\n name: \"Temperature\",\n role: \"value.temperature\",\n type: \"number\",\n readonly: true,\n unit: \"°C\"\n },\n {\n id: \"baseState\",\n value: battery.baseState,\n name: \"Base State\",\n role: \"text\",\n type: \"string\",\n readonly: true\n },\n {\n id: \"voltageState\",\n value: battery.voltState,\n name: \"Voltage State\",\n role: \"text\",\n type: \"string\",\n readonly: true\n },\n {\n id: \"currentState\",\n value: battery.currState,\n name: \"Current State\",\n role: \"text\",\n type: \"string\",\n readonly: true\n },\n {\n id: \"temperatureState\",\n value: battery.tempState,\n name: \"Temperature State\",\n role: \"text\",\n type: \"string\",\n readonly: true\n },\n {\n id: \"charge\",\n value: battery.coulomb,\n name: \"Charge\",\n role: \"value.battery\",\n type: \"number\",\n readonly: true,\n unit: \"%\",\n min: 0,\n max: 100\n },\n {\n id: \"capacity\",\n value: battery.capacity,\n name: \"Capacity\",\n role: \"value\",\n type: \"number\",\n readonly: true,\n unit: \"mAh\",\n min: 0\n }\n];\n\nconst messages = [];\n\nfor (const state of states) {\n const out = {\n topic: `0_userdata.0.pytes.01.${batteryId}.${state.id}`,\n payload: state.value,\n\n stateName: state.name,\n stateRole: state.role,\n stateType: state.type,\n stateReadonly: state.readonly\n };\n\n if (state.unit !== undefined) {\n out.stateUnit = state.unit;\n }\n\n if (state.min !== undefined) {\n out.stateMin = state.min;\n }\n\n if (state.max !== undefined) {\n out.stateMax = state.max;\n }\n\n messages.push(out);\n}\n\nreturn [messages];",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1100,
"y": 80,
"wires": [
[
"6a16c00e7b0e2997",
"68c635b31d513d6d"
]
]
},
{
"id": "c0f1afd033e9efcf",
"type": "inject",
"z": "26771494c726dd85",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "0.1",
"topic": "",
"payload": "true",
"payloadType": "bool",
"x": 110,
"y": 40,
"wires": [
[
"6d9a8c4540741cbf"
]
]
},
{
"id": "478993d3b5047088",
"type": "serial control",
"z": "26771494c726dd85",
"name": "",
"serial": "a78922aa38d9e450",
"x": 840,
"y": 180,
"wires": [
[]
]
},
{
"id": "c0ff5d2e0b5d2616",
"type": "debug",
"z": "26771494c726dd85",
"name": "debug 14",
"active": false,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "true",
"targetType": "full",
"statusVal": "",
"statusType": "auto",
"x": 800,
"y": 40,
"wires": []
},
{
"id": "723e5a9cad6671c7",
"type": "comment",
"z": "26771494c726dd85",
"name": "Serial-Ports",
"info": "/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller-if00-port0\n\n/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0",
"x": 90,
"y": 100,
"wires": []
},
{
"id": "69611b30df398dd3",
"type": "serial request",
"z": "26771494c726dd85",
"name": "",
"serial": "a78922aa38d9e450",
"x": 600,
"y": 80,
"wires": [
[
"c0ff5d2e0b5d2616",
"598d93854bab5014"
]
]
},
{
"id": "6d9a8c4540741cbf",
"type": "delay",
"z": "26771494c726dd85",
"name": "",
"pauseType": "delay",
"timeout": "2",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 300,
"y": 80,
"wires": [
[
"8979945491174ae1"
]
]
},
{
"id": "8979945491174ae1",
"type": "function",
"z": "26771494c726dd85",
"name": "naechster",
"func": "const batts =[\"bat 1\", \"bat 2\", \"bat 3\", \"bat 4\", \"bat 5\", \"bat 6\", \"bat 7\",\"bat 8\"];\n\nflow.set(\"batts\", batts);\n\nvar pos = parseInt(flow.get(\"pos\"));\npos++;\nif(pos > batts.length -1){pos=0}\nmsg.payload = batts[pos];\nflow.set (\"pos\", pos);\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "// Der Code hier wird ausgeführt,\n// wenn der Node gestartet wird\nflow.set(\"pos\", 0);\n",
"finalize": "",
"libs": [],
"x": 460,
"y": 80,
"wires": [
[
"69611b30df398dd3"
]
]
},
{
"id": "a418a9b4681de96b",
"type": "catch",
"z": "26771494c726dd85",
"name": "",
"scope": null,
"uncaught": false,
"x": 220,
"y": 180,
"wires": [
[
"fe3238beacae91ad"
]
]
},
{
"id": "598d93854bab5014",
"type": "switch",
"z": "26771494c726dd85",
"name": "",
"property": "payload",
"propertyType": "msg",
"rules": [
{
"t": "istype",
"v": "string",
"vt": "string"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 2,
"x": 750,
"y": 80,
"wires": [
[
"79724f85531596e4",
"6d9a8c4540741cbf"
],
[
"fe3238beacae91ad"
]
]
},
{
"id": "5a79550b570ff05d",
"type": "change",
"z": "26771494c726dd85",
"name": "Öffne Verbindung",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "{\"enabled\":true}",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 630,
"y": 220,
"wires": [
[
"478993d3b5047088",
"665e7ef72b226bc2"
]
]
},
{
"id": "b28980471e986893",
"type": "change",
"z": "26771494c726dd85",
"name": "Schließe Verbindung",
"rules": [
{
"t": "set",
"p": "payload",
"pt": "msg",
"to": "{\"enabled\":false}",
"tot": "json"
}
],
"action": "",
"property": "",
"from": "",
"to": "",
"reg": false,
"x": 640,
"y": 180,
"wires": [
[
"478993d3b5047088"
]
]
},
{
"id": "fdaee2e16d78cc87",
"type": "delay",
"z": "26771494c726dd85",
"name": "",
"pauseType": "delay",
"timeout": "4",
"timeoutUnits": "seconds",
"rate": "1",
"nbRateUnits": "1",
"rateUnits": "second",
"randomFirst": "1",
"randomLast": "5",
"randomUnits": "seconds",
"drop": false,
"allowrate": false,
"outputs": 1,
"x": 440,
"y": 220,
"wires": [
[
"5a79550b570ff05d"
]
]
},
{
"id": "ecfde46ecc908741",
"type": "inject",
"z": "26771494c726dd85",
"name": "Test",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "",
"payload": "true",
"payloadType": "bool",
"x": 230,
"y": 240,
"wires": [
[
"fe3238beacae91ad"
]
]
},
{
"id": "a78922aa38d9e450",
"type": "serial-port",
"name": "SerialPort",
"serialport": "/dev/serial/by-id/usb-Silicon_Labs_CP2102_USB_to_UART_Bridge_Controller_0001-if00-port0",
"serialbaud": "115200",
"databits": "8",
"parity": "none",
"stopbits": "1",
"waitfor": "",
"dtr": "none",
"rts": "none",
"cts": "none",
"dsr": "none",
"newline": "250",
"bin": "false",
"out": "time",
"addchar": "\\r\\n",
"responsetimeout": "10000"
}
]