# Close gate after one message has passed through

**URL:** <https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575>\
**Category:** General\
**Created:** [13 January 2022 09:50 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575 "2022-01-13T09:50:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [13 January 2022 09:50 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/1 "2022-01-13T09:50:35Z")

</div>

Hi,  
I'm using node-red-contrib-simple-gate. I am opening it with an inject node at a certain time every day. Is there a way to close it after one message has passed through?  
best regards

---

<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:** [13 January 2022 09:53 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/2 "2022-01-13T09:53:18Z")

</div>

I've never used it but at a guess....  
Connect a 2nd wire to the gate output through to a change node (to set the required close operation topic/payload) back into the gate node input.

---

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [13 January 2022 10:04 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/3 "2022-01-13T10:04:41Z")

</div>

That was easy. 😀  
thanks for your help

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [13 January 2022 10:19 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/4 "2022-01-13T10:19:46Z")

</div>

There is a possible race condition doing that. If a second message arrived at the gate just as the first was being released then it might get through the gate before the close message gets back to the gate again. So very occasionally there might be two messages released.  
If that is a problem then there are other solutions.

---

<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:** [13 January 2022 10:29 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/5 "2022-01-13T10:29:42Z")

</div>

Hadn't considered that. it would be very tight - like 2 messages sent in concession which can realistically only be done by a node outputting 2 messages at once?

I imagine one of the solutions would be the use of a semaphore in conjunction with the gate?

---

<div class="post-metadata">

**Author:** ![MR76](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@MR76](https://discourse.nodered.org/u/MR76)\
**Post date:** [13 January 2022 10:30 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/6 "2022-01-13T10:30:52Z")

</div>

I don't think that could happen in my case. I'm storing a number value from a S7 PLC in a persist node once a day. The cycle time is 500ms. I think that is enough time for the gate to close again.

```auto
[
    {
        "id": "ddcccfcf499de63b",
        "type": "gate",
        "z": "ca154bf7060b45dc",
        "name": "",
        "controlTopic": "control",
        "defaultState": "closed",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "defaultCmd": "default",
        "statusCmd": "status",
        "persist": false,
        "storeName": "memory",
        "x": 470,
        "y": 240,
        "wires": [
            [
                "d694bf6c62474b7d",
                "5b3e9735be5d02e8",
                "1206eb0daaea1370"
            ]
        ]
    },
    {
        "id": "5113bc8bd2d6426d",
        "type": "inject",
        "z": "ca154bf7060b45dc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "30 14 * * 1,2,3,4",
        "once": false,
        "onceDelay": 0.1,
        "topic": "control",
        "payload": "open",
        "payloadType": "str",
        "x": 180,
        "y": 200,
        "wires": [
            [
                "ddcccfcf499de63b"
            ]
        ]
    },
    {
        "id": "1c7abb57336715ae",
        "type": "timeouttrigger",
        "z": "ca154bf7060b45dc",
        "ontimeouttype": "bool",
        "ontimeoutval": "false",
        "duration": "500",
        "units": "ms",
        "name": "",
        "x": 920,
        "y": 180,
        "wires": [
            [
                "757bded9c11c190a"
            ]
        ]
    },
    {
        "id": "1206eb0daaea1370",
        "type": "change",
        "z": "ca154bf7060b45dc",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 750,
        "y": 180,
        "wires": [
            [
                "1c7abb57336715ae"
            ]
        ]
    },
    {
        "id": "9e5c6d341cfe5b4e",
        "type": "s7 in",
        "z": "ca154bf7060b45dc",
        "endpoint": "f84e3498.70aae8",
        "mode": "single",
        "variable": "Counter",
        "diff": false,
        "name": "",
        "x": 140,
        "y": 240,
        "wires": [
            [
                "ddcccfcf499de63b"
            ]
        ]
    },
    {
        "id": "d694bf6c62474b7d",
        "type": "persist in",
        "z": "ca154bf7060b45dc",
        "name": "Value",
        "storageNode": "ebae278a.7d9f58",
        "x": 710,
        "y": 240,
        "wires": []
    },
    {
        "id": "798d7dfbe8f38b8c",
        "type": "inject",
        "z": "ca154bf7060b45dc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 470,
        "y": 300,
        "wires": [
            [
                "d694bf6c62474b7d"
            ]
        ]
    },
    {
        "id": "5b3e9735be5d02e8",
        "type": "change",
        "z": "ca154bf7060b45dc",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "close",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "control",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 470,
        "y": 160,
        "wires": [
            [
                "ddcccfcf499de63b"
            ]
        ]
    },
    {
        "id": "757bded9c11c190a",
        "type": "s7 out",
        "z": "ca154bf7060b45dc",
        "endpoint": "f84e3498.70aae8",
        "variable": "reset",
        "name": "",
        "x": 1050,
        "y": 180,
        "wires": []
    },
    {
        "id": "f84e3498.70aae8",
        "type": "s7 endpoint",
        "transport": "iso-on-tcp",
        "address": "192.168.178.10",
        "port": "102",
        "rack": "0",
        "slot": "2",
        "localtsaphi": "01",
        "localtsaplo": "00",
        "remotetsaphi": "01",
        "remotetsaplo": "00",
        "connmode": "rack-slot",
        "adapter": "",
        "busaddr": "2",
        "cycletime": "500",
        "timeout": "2000",
        "name": "",
        "vartable": [
            {
                "addr": "DB2,WORD10",
                "name": "Counter"
            },
            {
                "addr": "DB2,X0.0",
                "name": "Reset"
            }
        ]
    },
    {
        "id": "ebae278a.7d9f58",
        "type": "persist-store",
        "filename": "persistence2.json",
        "interval": "30"
    }
]

```

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [13 January 2022 15:11 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/7 "2022-01-13T15:11:47Z")

</div>

The best solution I have found for this (thanks to @Colin) is

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/1/11e5dc6de8f68fb6dde609cf5dda75eec2927cea.png)

```auto
[{"id":"fbd3a65b056cdd0d","type":"inject","z":"523968221e696a28","name":"message in","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":170,"y":620,"wires":[["a2fe8de8ecc0501d"]]},{"id":"cc86232451944c83","type":"inject","z":"523968221e696a28","name":"open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"open","payloadType":"str","x":350,"y":660,"wires":[["e8464d1608046ae7"]]},{"id":"a2fe8de8ecc0501d","type":"function","z":"523968221e696a28","name":"close after","func":"return [[msg,{payload:'close',topic:'control'}]]","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":620,"wires":[["e8464d1608046ae7"]]},{"id":"e8464d1608046ae7","type":"gate","z":"523968221e696a28","name":"","controlTopic":"control","defaultState":"open","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","defaultCmd":"default","statusCmd":"status","suppressWarnings":false,"persist":false,"storeName":"memoryOnly","x":490,"y":620,"wires":[["69207b01cfe27d1f"]]},{"id":"69207b01cfe27d1f","type":"debug","z":"523968221e696a28","name":"message out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":620,"wires":[]}]

```

The `function` node ("close after") On Message tab contains just the single line of code

```auto
return [[msg,{payload:'close',topic:'control'}]]

```

This should eliminate the possibility of a race condition. (As I understand how NR handles messages, it is not possible for any message to reach the gate in between the two sent by the `function` node.) For the record, and in case you are interested, aspects of this issue have been discussed [here](https://github.com/drmibell/node-red-contrib-simple-gate/issues/12) and [here](https://discourse.nodered.org/t/timing-question-avoiding-race-conditions/55616).

---

<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:** [27 January 2022 15:12 UTC](https://discourse.nodered.org/t/close-gate-after-one-message-has-passed-through/56575/8 "2022-01-27T15:12:28Z")

</div>

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