Close gate after one message has passed through

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

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.

1 Like

That was easy. :grinning:
thanks for your help

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.

1 Like

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?

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.

[
    {
        "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"
    }
]

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

[{"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

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 and here.

2 Likes

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