# How to close a tab I opened

**URL:** <https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132>\
**Category:** Dashboard\
**Created:** [2 October 2024 14:52 UTC](https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132 "2024-10-02T14:52:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bluepgt](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bluepgt](https://discourse.nodered.org/u/bluepgt)\
**Post date:** [2 October 2024 14:52 UTC](https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132/1 "2024-10-02T14:52:39Z")

</div>

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:

```auto
[
    {
        "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?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [6 October 2024 06:45 UTC](https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132/2 "2024-10-06T06:45:31Z")

</div>

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

```js
window._popupWindow = window.top.open(...

```

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

```

---

<div class="post-metadata">

**Author:** ![bluepgt](https://avatars.discourse-cdn.com/v4/letter/b/cdc98d/32.png) [@bluepgt](https://discourse.nodered.org/u/bluepgt)\
**Post date:** [7 October 2024 08:39 UTC](https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132/3 "2024-10-07T08:39:22Z")

</div>

Yes of course 😉  
Thanks for the help.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 October 2024 08:40 UTC](https://discourse.nodered.org/t/how-to-close-a-tab-i-opened/92132/4 "2024-10-21T08:40:10Z")

</div>

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