How to close a tab I opened

I have created a flow that opens a tab via dashboard when a PIR sensor is engaged. That works just fine, now I want to close the tab once the PIR sensor is disengaged. I used the template node, here is an export of my flow:

[
    {
        "id": "b00bcee008e3115e",
        "type": "tab",
        "label": "PIR Sensor",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5ce944f57e859078",
        "type": "debug",
        "z": "b00bcee008e3115e",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 860,
        "y": 100,
        "wires": []
    },
    {
        "id": "52db481af51cb249",
        "type": "rpi-gpio in",
        "z": "b00bcee008e3115e",
        "name": "PIR",
        "pin": "17",
        "intype": "tri",
        "debounce": "25",
        "read": false,
        "bcm": true,
        "x": 270,
        "y": 220,
        "wires": [
            [
                "5ce944f57e859078",
                "d4ca777270fcb0ac"
            ]
        ]
    },
    {
        "id": "103aaeb6eb547ed2",
        "type": "comment",
        "z": "b00bcee008e3115e",
        "name": "Mnotion detected",
        "info": "GPIO PIN 17\n",
        "x": 300,
        "y": 160,
        "wires": []
    },
    {
        "id": "83ba863d8f712574",
        "type": "ui_toast",
        "z": "b00bcee008e3115e",
        "position": "top right",
        "displayTime": "3",
        "highlight": "",
        "sendall": true,
        "outputs": 0,
        "ok": "OK",
        "cancel": "",
        "raw": false,
        "className": "",
        "topic": "",
        "name": "",
        "x": 990,
        "y": 380,
        "wires": []
    },
    {
        "id": "d4ca777270fcb0ac",
        "type": "function",
        "z": "b00bcee008e3115e",
        "name": "function 1",
        "func": "let x = msg.payload;\nif (x == 1) {\n    msg.payload = \"PIR Engaged\";\n} else {\n    msg.payload = \"PIR Disengaged\";\n}\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 300,
        "wires": [
            [
                "e004296762ee3eab"
            ]
        ]
    },
    {
        "id": "e004296762ee3eab",
        "type": "change",
        "z": "b00bcee008e3115e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 630,
        "y": 340,
        "wires": [
            [
                "90ef5423a301f0ee"
            ]
        ]
    },
    {
        "id": "401d7a13b1eba4e2",
        "type": "comment",
        "z": "b00bcee008e3115e",
        "name": "Change Payload",
        "info": "Function: Changes payload output to show specific message\nChange: Tell Node Red not to show the topic by setting msg.topic to \"nothing\"\n",
        "x": 540,
        "y": 240,
        "wires": []
    },
    {
        "id": "90ef5423a301f0ee",
        "type": "ui_template",
        "z": "b00bcee008e3115e",
        "group": "2ddab6e5bd137008",
        "name": "",
        "order": 0,
        "width": 0,
        "height": 0,
        "format": "<script>\n  (function(scope) {\n    scope.$watch('msg.payload', function(data) {\n        if (data == \"PIR Engaged\") {\n          window.top.open(\"http://bluepgt:8080\");\n        } \n        if (data == \"PIR Disengaged\") {\n          window.top.close();\n        } \n    });\n})(scope);",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "className": "",
        "x": 800,
        "y": 360,
        "wires": [
            [
                "83ba863d8f712574",
                "9856968e79889736"
            ]
        ]
    },
    {
        "id": "9856968e79889736",
        "type": "debug",
        "z": "b00bcee008e3115e",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 1100,
        "y": 240,
        "wires": []
    },
    {
        "id": "2ddab6e5bd137008",
        "type": "ui_group",
        "name": "Group 1",
        "tab": "1c316425e3ec6b74",
        "order": 1,
        "disp": true,
        "width": 6
    },
    {
        "id": "1c316425e3ec6b74",
        "type": "ui_tab",
        "name": "Tab 3",
        "icon": "dashboard",
        "order": 3
    }
]

I'm running node red on a Raspberry pi 4B.
Nodered version: 4.0.3
Node.js version: 18.20.4

Is this way possible?

Since you use window.top.open you need to store its result somewhere (e.g. in scope or window) and call close() on it.

window._popupWindow = window.top.open(...
// Close the window if it's still open
if (window._popupWindow && !window._popupWindow.closed) {
    window._popupWindow.close();
}

Yes of course :wink:
Thanks for the help.

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