subflow Script auf CCU ausführen

Node-RED als CCU3/RaspberryMatic Addon, WebApp, HomeKit, ...

Moderator: Co-Administratoren

Antworten
Benutzeravatar
Henke
Beiträge: 1524
Registriert: 27.06.2022, 20:51
System: CCU
Hat sich bedankt: 141 Mal
Danksagung erhalten: 306 Mal

subflow Script auf CCU ausführen

Beitrag von Henke » 15.10.2022, 20:52

Mein Problem war beim Ausführen von Scripten auf der CCU aus NodeRed heraus, das ich entweder den SDV als Editor nicht direkt benutzen konnte (Node "CCU-script") oder ich keine Werte setzen bzw. auslesen konnte (Node "CCU-programm").
Abgesehen davon war das Setzen von "var/src/this" immer recht nervig.
Des weiteren mussten zu viele Systemvariablen angelegt werden, die nicht (ohne Trick) direkt in NodeRed aktualisiert werden und das System einfach unübersichtlich machen.
Daher habe ich eine Subnode geschrieben, die mir die Arbeit abnimmt und die Probleme löst.
Screenshot 2022-10-15 202431.jpg
Testprogramm

Code: Alles auswählen

var val = "$val$";
var this = "$this$";
var src = "$src$";
WriteLine ( "val  >" # val # "<" );
WriteLine ( "this >" # this # "<");
WriteLine ( "src  >" # src # "<");

string sSrc = "$src$";
if ( !sSrc ) {
return;
}

if (sSrc ) {
object oSrc = dom.GetObject(sSrc);
if (oSrc) {
WriteLine ( "----------" );
WriteLine ( oSrc.Name() );
 }
 }
Natürlich kann auch direkt eines eurer Scripte auf der CCU genutzt werden.

Testaufrufe mit "nix" (real eher ein timer), einem Fest eingetragenen Datenpunkt, ein Datenpunkt über "get value" und ein Datenpunkt über "value":
Screenshot 2022-10-15 203628.jpg
Subflow inkl. Testaufrufe

Code: Alles auswählen

[
    {
        "id": "023fb9fcd6b3b2ab",
        "type": "subflow",
        "name": "run script",
        "info": "val über msg.payload\r\nsrc über msg.datapointName\r\nsetzen!",
        "category": "",
        "in": [
            {
                "x": 100,
                "y": 100,
                "wires": [
                    {
                        "id": "3c4528adcca3d599"
                    }
                ]
            }
        ],
        "out": [
            {
                "x": 1300,
                "y": 160,
                "wires": [
                    {
                        "id": "c0432a17cdefaed0",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1310,
                "y": 220,
                "wires": [
                    {
                        "id": "132e7a487fa42dce",
                        "port": 0
                    },
                    {
                        "id": "3a762f05f4399717",
                        "port": 0
                    }
                ]
            },
            {
                "x": 1310,
                "y": 280,
                "wires": [
                    {
                        "id": "8b07bf506dd71e21",
                        "port": 0
                    }
                ]
            }
        ],
        "env": [
            {
                "name": "this",
                "type": "str",
                "value": "$this$",
                "ui": {
                    "type": "input",
                    "opts": {
                        "types": [
                            "str",
                            "num"
                        ]
                    }
                }
            }
        ],
        "meta": {},
        "color": "#DDAA99",
        "outputLabels": [
            "Ok",
            "Debug",
            "Error"
        ],
        "status": {
            "x": 1280,
            "y": 340,
            "wires": [
                {
                    "id": "01c9b12e2bc02971",
                    "port": 0
                },
                {
                    "id": "562a93bb2535a55e",
                    "port": 0
                }
            ]
        }
    },
    {
        "id": "d0cb1870940d5698",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "this",
        "rules": [
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$this$",
                "fromt": "str",
                "to": "this",
                "tot": "env"
            },
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$src$",
                "fromt": "str",
                "to": "src",
                "tot": "flow"
            },
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$val$",
                "fromt": "str",
                "to": "val",
                "tot": "flow"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "objects.script",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 100,
        "wires": [
            [
                "132e7a487fa42dce"
            ]
        ]
    },
    {
        "id": "132e7a487fa42dce",
        "type": "ccu-script",
        "z": "023fb9fcd6b3b2ab",
        "name": "payload -> script",
        "script": "",
        "ccuConfig": "38263145.35ea0e",
        "topic": "${CCU}/${Interface}",
        "x": 620,
        "y": 100,
        "wires": [
            [
                "02229eea2e4a27c8"
            ]
        ]
    },
    {
        "id": "565d0e8c19b2f6dd",
        "type": "template",
        "z": "023fb9fcd6b3b2ab",
        "name": "Script aus CCU laden",
        "field": "objects.script",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "plain",
        "template": "object o =  dom.GetObject(ID_PROGRAMS).Get(\"$this$\");\ninteger iId  =o.Rule().RuleDestination().DestSingleDestinations();\nstring script  =o.Rule().RuleDestination().DestinationValueAt(0);\nstring sPrgName = o.Name();\nstring this = \"$this$\";\nstring val = \"$val$\";\nstring src = \"$src$\";",
        "output": "str",
        "x": 260,
        "y": 180,
        "wires": [
            [
                "091f91d8bb4ad553"
            ]
        ]
    },
    {
        "id": "3a762f05f4399717",
        "type": "ccu-script",
        "z": "023fb9fcd6b3b2ab",
        "name": "payload -> script",
        "script": "",
        "ccuConfig": "38263145.35ea0e",
        "topic": "${CCU}/${Interface}",
        "x": 620,
        "y": 160,
        "wires": [
            [
                "8d97a25fb8f13acc"
            ]
        ]
    },
    {
        "id": "f21a6546314fb326",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "this",
        "rules": [
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$this$",
                "fromt": "str",
                "to": "this",
                "tot": "env"
            },
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$val$",
                "fromt": "str",
                "to": "val",
                "tot": "flow"
            },
            {
                "t": "change",
                "p": "objects.script",
                "pt": "msg",
                "from": "$src$",
                "fromt": "str",
                "to": "src",
                "tot": "flow"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "objects.script",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 450,
        "y": 160,
        "wires": [
            [
                "3a762f05f4399717"
            ]
        ]
    },
    {
        "id": "091f91d8bb4ad553",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "val",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "src",
                "pt": "flow",
                "to": "datapointName",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 250,
        "y": 220,
        "wires": [
            [
                "d0cb1870940d5698"
            ]
        ]
    },
    {
        "id": "08120a53682728c3",
        "type": "catch",
        "z": "023fb9fcd6b3b2ab",
        "name": "Fehler umleiten",
        "scope": null,
        "uncaught": true,
        "x": 620,
        "y": 240,
        "wires": [
            [
                "8b07bf506dd71e21",
                "562a93bb2535a55e"
            ]
        ]
    },
    {
        "id": "8b07bf506dd71e21",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Error",
                "pt": "flow",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 820,
        "y": 240,
        "wires": [
            []
        ]
    },
    {
        "id": "6e43e3aba59853df",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "Error",
                "pt": "flow",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 240,
        "y": 140,
        "wires": [
            [
                "565d0e8c19b2f6dd"
            ]
        ]
    },
    {
        "id": "c0432a17cdefaed0",
        "type": "switch",
        "z": "023fb9fcd6b3b2ab",
        "name": "Keine Ausgabe bei Fehler",
        "property": "Error",
        "propertyType": "flow",
        "rules": [
            {
                "t": "neq",
                "v": "1",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 1090,
        "y": 160,
        "wires": [
            [
                "01c9b12e2bc02971"
            ]
        ]
    },
    {
        "id": "01c9b12e2bc02971",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "Ok",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 320,
        "wires": [
            []
        ]
    },
    {
        "id": "562a93bb2535a55e",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "Error",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1090,
        "y": 360,
        "wires": [
            []
        ]
    },
    {
        "id": "c1c38ab44734a460",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "flow bereinigen",
        "rules": [
            {
                "t": "delete",
                "p": "val",
                "pt": "flow"
            },
            {
                "t": "delete",
                "p": "src",
                "pt": "flow"
            },
            {
                "t": "delete",
                "p": "PrgName",
                "pt": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1120,
        "y": 100,
        "wires": [
            []
        ]
    },
    {
        "id": "8d97a25fb8f13acc",
        "type": "function",
        "z": "023fb9fcd6b3b2ab",
        "name": "Restore Message",
        "func": "var newMsg = {};\n newMsg = flow.get ( \"oriMsg\" );\n var script = {};\nscript.Name = flow.get(\"PrgName\");\nscript.objects = msg.objects;\nscript.payload = msg.payload;\nnewMsg.script = script;\n\n delete flow.oriMsg;\nreturn newMsg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 830,
        "y": 160,
        "wires": [
            [
                "c0432a17cdefaed0",
                "c1c38ab44734a460"
            ]
        ]
    },
    {
        "id": "3c4528adcca3d599",
        "type": "function",
        "z": "023fb9fcd6b3b2ab",
        "name": "Clone Message",
        "func": "const oriMsg = RED.util.cloneMessage(msg);\nflow.set(\"oriMsg\", oriMsg );\n\nif (msg.payload == null)\n    msg.payload = \"\";\n\nif (msg.datapointName == null)\n    msg.datapointName = \"\";\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 240,
        "y": 100,
        "wires": [
            [
                "6e43e3aba59853df"
            ]
        ]
    },
    {
        "id": "02229eea2e4a27c8",
        "type": "change",
        "z": "023fb9fcd6b3b2ab",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "PrgName",
                "pt": "flow",
                "to": "objects.sPrgName",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 840,
        "y": 100,
        "wires": [
            [
                "f21a6546314fb326"
            ]
        ]
    },
    {
        "id": "5b8719b83e1f72f1",
        "type": "subflow:023fb9fcd6b3b2ab",
        "z": "3bef3877d73779f4",
        "name": "Anpassen: Name ode ID des CCU Programms",
        "env": [
            {
                "name": "this",
                "value": "30433",
                "type": "num"
            },
            {
                "name": "val",
                "value": "",
                "type": "str"
            },
            {
                "name": "src",
                "value": "sss",
                "type": "env"
            }
        ],
        "x": 1160,
        "y": 3580,
        "wires": [
            [
                "ed19b3954d8b481a"
            ],
            [
                "061942f2f3dbbe09"
            ],
            [
                "94ef7a0e523ea346"
            ]
        ]
    },
    {
        "id": "289bf9784e5e4fab",
        "type": "inject",
        "z": "3bef3877d73779f4",
        "name": "val und src - zm Testen datapoint anpassen",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "datapointName",
                "v": "HmIP-RF.0010DD899E5483:1.ACTUAL_TEMPERATURE",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "ValMsg",
        "payloadType": "str",
        "x": 660,
        "y": 3640,
        "wires": [
            [
                "5b8719b83e1f72f1"
            ]
        ]
    },
    {
        "id": "ed19b3954d8b481a",
        "type": "debug",
        "z": "3bef3877d73779f4",
        "name": "Ok",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": true,
        "complete": "true",
        "targetType": "full",
        "statusVal": "script.payload",
        "statusType": "msg",
        "x": 1470,
        "y": 3580,
        "wires": []
    },
    {
        "id": "061942f2f3dbbe09",
        "type": "debug",
        "z": "3bef3877d73779f4",
        "name": "Debug",
        "active": false,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1470,
        "y": 3660,
        "wires": []
    },
    {
        "id": "94ef7a0e523ea346",
        "type": "debug",
        "z": "3bef3877d73779f4",
        "name": "Error",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1470,
        "y": 3720,
        "wires": [],
        "icon": "node-red/alert.svg"
    },
    {
        "id": "a45e383808f1b7c1",
        "type": "inject",
        "z": "3bef3877d73779f4",
        "name": "nix",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 410,
        "y": 3700,
        "wires": [
            [
                "3260960dd2d4b640"
            ]
        ]
    },
    {
        "id": "3260960dd2d4b640",
        "type": "ccu-get-value",
        "z": "3bef3877d73779f4",
        "name": "zum Testen Channel anpassen",
        "ccuConfig": "38263145.35ea0e",
        "iface": "CCU-Jack",
        "channel": "JACK000003:3 Shelly1PM-1:3 Temp 1 Rücklauf",
        "sysvar": "",
        "sysvarProperty": "value",
        "datapoint": "VOLTAGE",
        "datapointProperty": "all",
        "setProp": "huminity",
        "setPropType": "msg",
        "x": 690,
        "y": 3700,
        "wires": [
            [
                "91049906d6d2e60c"
            ]
        ]
    },
    {
        "id": "251714a59d6b8548",
        "type": "ccu-value",
        "z": "3bef3877d73779f4",
        "name": "",
        "iface": "CCU-Jack",
        "channel": "JACK000003:3 Shelly1PM-1:3 Temp 1 Rücklauf",
        "datapoint": "VOLTAGE",
        "mode": "",
        "start": true,
        "change": false,
        "cache": true,
        "queue": true,
        "on": 0,
        "onType": "undefined",
        "ramp": 0,
        "rampType": "undefined",
        "working": false,
        "ccuConfig": "38263145.35ea0e",
        "topic": "${CCU}/${Interface}/${channel}/${datapoint}",
        "x": 870,
        "y": 3760,
        "wires": [
            [
                "5b8719b83e1f72f1"
            ]
        ]
    },
    {
        "id": "91049906d6d2e60c",
        "type": "change",
        "z": "3bef3877d73779f4",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "datapointName",
                "pt": "msg",
                "to": "huminity.deviceName",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "huminity.payload",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 950,
        "y": 3700,
        "wires": [
            [
                "5b8719b83e1f72f1"
            ]
        ]
    },
    {
        "id": "893f3ba212834f86",
        "type": "inject",
        "z": "3bef3877d73779f4",
        "name": "nix",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 770,
        "y": 3580,
        "wires": [
            [
                "5b8719b83e1f72f1"
            ]
        ]
    }
]
Sämtliche Variablen des ausgeführten Scrips können dann mit NodeRed weiter bearbeitet werden.
Beim Beispielscript sieht es so aus:
Screenshot 2022-10-15 204735.jpg
Die Originalnachricht wird beibehalten und die Ergebnisse unter "script" zusammengefasst.

LG
Michael

Antworten

Zurück zu „RedMatic“