Trigger inject node using HTTP request

Hello!

I want to ask a question about inject node and HTTP requests.

I have an inject node that is configured working on an interval of 5 seconds.
My goal is to be able to trigger the inject node by using HTTP requests in order to start and stop.

Any help would be appreciated.

Best regards.

You can't stop/start an inject node, but you could use a node-red-contrib-simple-gate node to block or allow through the messages from the inject node.

Easy enough to pause a repeating inject by blocking the msg.

chrome_AbSXabUnUu

demo flow (use CTRL-I to import)

[{"id":"46f1036c0b5f6b8c","type":"http in","z":"fe471bfa726693b8","name":"","url":"/pause","method":"get","upload":false,"swaggerDoc":"","x":1090,"y":580,"wires":[["fc3612c3a8301232"]]},{"id":"06fb4054f7ef1d7c","type":"http in","z":"fe471bfa726693b8","name":"","url":"/resume","method":"get","upload":false,"swaggerDoc":"","x":1090,"y":620,"wires":[["afdf484497bb0a27"]]},{"id":"fc3612c3a8301232","type":"change","z":"fe471bfa726693b8","name":"Set flow.pauseInject = true","rules":[{"t":"set","p":"pauseInject","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":580,"wires":[["4f9ddd7c2cb98bf5"]]},{"id":"afdf484497bb0a27","type":"change","z":"fe471bfa726693b8","name":"Set flow.pauseInject = false","rules":[{"t":"set","p":"pauseInject","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":1300,"y":620,"wires":[["4f9ddd7c2cb98bf5"]]},{"id":"6cb64f61850390f1","type":"http response","z":"fe471bfa726693b8","name":"HTTP Response 200","statusCode":"200","headers":{},"x":1660,"y":580,"wires":[]},{"id":"fb8df2844a9de85a","type":"inject","z":"fe471bfa726693b8","name":"Every 5 sec","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1100,"y":500,"wires":[["ac47afbe45a8f609"]]},{"id":"ac47afbe45a8f609","type":"switch","z":"fe471bfa726693b8","name":"is paused? \\n not paused!","property":"pauseInject","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1290,"y":500,"wires":[["14b18453af9198a7"],["87abb18314482647"]]},{"id":"14b18453af9198a7","type":"debug","z":"fe471bfa726693b8","name":"flow.pauseInject === true - do nothing","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1590,"y":480,"wires":[]},{"id":"87abb18314482647","type":"debug","z":"fe471bfa726693b8","name":"flow.pauseInject !== true - do something","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1580,"y":520,"wires":[]},{"id":"4f9ddd7c2cb98bf5","type":"function","z":"fe471bfa726693b8","name":"Pause Status","func":"const paused = flow.get('pauseInject') || false\n\nif (paused) {\n    node.status({fill:\"red\",shape:\"ring\",text:\"Paused\"})\n} else {\n    node.status({fill:\"green\",shape:\"dot\",text:\"Unpaused\"})\n}\n\nreturn msg","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1485,"y":600,"wires":[["6cb64f61850390f1","0ed5945d20cfb087"]],"icon":"font-awesome/fa-question-circle-o","l":false},{"id":"0ed5945d20cfb087","type":"debug","z":"fe471bfa726693b8","name":"debug 307","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1670,"y":620,"wires":[]}]
1 Like

Here is my example using node-red-contrib-queue-gate

using url parameters to start and stop updating of a timestamp.

Start - http://yourip:1880/nr-help-timestamp?action=start
If you refresh the page the timestamp will change every 5 seconds

Stop - http://yourip:1880/nr-help-timestamp?action=stop
when you refresh the page the timestamp will remain the same

[
    {
        "id": "839ddaad8d0899de",
        "type": "inject",
        "z": "c272682f840e082e",
        "name": "",
        "props": [
            {
                "p": "timestamp",
                "v": "",
                "vt": "date"
            }
        ],
        "repeat": "5",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "x": 650,
        "y": 100,
        "wires": [
            [
                "553fd81486ef7866"
            ]
        ]
    },
    {
        "id": "402eea59f3ef5733",
        "type": "change",
        "z": "c272682f840e082e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "nr_help_timestamp",
                "pt": "flow",
                "to": "timestamp",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 1040,
        "y": 140,
        "wires": [
            []
        ]
    },
    {
        "id": "3de4a4d8269e31f2",
        "type": "http in",
        "z": "c272682f840e082e",
        "name": "",
        "url": "/nr-help-timestamp",
        "method": "get",
        "upload": false,
        "swaggerDoc": "",
        "x": 200,
        "y": 180,
        "wires": [
            [
                "2c7eecc2f2d6f0e7",
                "03bc98d66492f823",
                "0cdfd60ecbdef030"
            ]
        ]
    },
    {
        "id": "553fd81486ef7866",
        "type": "q-gate",
        "z": "c272682f840e082e",
        "name": "",
        "controlTopic": "control",
        "defaultState": "open",
        "openCmd": "open",
        "closeCmd": "close",
        "toggleCmd": "toggle",
        "queueCmd": "queue",
        "defaultCmd": "default",
        "triggerCmd": "trigger",
        "flushCmd": "flush",
        "resetCmd": "reset",
        "peekCmd": "peek",
        "dropCmd": "drop",
        "statusCmd": "status",
        "maxQueueLength": "100",
        "keepNewest": false,
        "qToggle": false,
        "persist": false,
        "storeName": "memory",
        "x": 830,
        "y": 140,
        "wires": [
            [
                "402eea59f3ef5733"
            ]
        ]
    },
    {
        "id": "1c18c01330c5be67",
        "type": "function",
        "z": "c272682f840e082e",
        "name": "headders, paylod and status code",
        "func": "msg.statusCode = \"200\";\nmsg.headers = {};\nmsg.headers['X-Robots-Tag'] = \"noindex\";\nmsg.headers['content-type'] = 'text/html';\n\n\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 700,
        "y": 240,
        "wires": [
            [
                "a13ecdd84523da3b"
            ]
        ]
    },
    {
        "id": "a13ecdd84523da3b",
        "type": "http response",
        "z": "c272682f840e082e",
        "name": "",
        "statusCode": "",
        "headers": {},
        "x": 930,
        "y": 240,
        "wires": []
    },
    {
        "id": "2c7eecc2f2d6f0e7",
        "type": "debug",
        "z": "c272682f840e082e",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 410,
        "y": 200,
        "wires": []
    },
    {
        "id": "0cdfd60ecbdef030",
        "type": "change",
        "z": "c272682f840e082e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "nr_help_timestamp",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 440,
        "y": 240,
        "wires": [
            [
                "1c18c01330c5be67"
            ]
        ]
    },
    {
        "id": "03bc98d66492f823",
        "type": "switch",
        "z": "c272682f840e082e",
        "name": "",
        "property": "payload.action",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "start",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "stop",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 410,
        "y": 160,
        "wires": [
            [
                "95f2702c1e79dfa3"
            ],
            [
                "8cfba2cb0cb70f38"
            ]
        ]
    },
    {
        "id": "95f2702c1e79dfa3",
        "type": "change",
        "z": "c272682f840e082e",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "open",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "control",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 620,
        "y": 140,
        "wires": [
            [
                "553fd81486ef7866"
            ]
        ]
    },
    {
        "id": "8cfba2cb0cb70f38",
        "type": "change",
        "z": "c272682f840e082e",
        "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": 620,
        "y": 180,
        "wires": [
            [
                "553fd81486ef7866"
            ]
        ]
    }
]

You can send the http request and have it action a trigger node
e.g

[{"id":"184f4bd41cfa6871","type":"http in","z":"b9860b4b9de8c8da","name":"","url":"/my_trigger","method":"get","upload":false,"swaggerDoc":"","x":180,"y":60,"wires":[["7e7eddab92114949","586d5c3c36b05da6"]]},{"id":"7e7eddab92114949","type":"http response","z":"b9860b4b9de8c8da","name":"","statusCode":"","headers":{},"x":430,"y":40,"wires":[]},{"id":"586d5c3c36b05da6","type":"switch","z":"b9860b4b9de8c8da","name":"","property":"payload.reset","propertyType":"msg","rules":[{"t":"eq","v":"stop","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":100,"wires":[["a45820d6622d54a7"],["e530a41c5ce1de3f"]]},{"id":"a45820d6622d54a7","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":80,"wires":[["25d23891bc61af28"]]},{"id":"e530a41c5ce1de3f","type":"change","z":"b9860b4b9de8c8da","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"$number($$.payload.delay)","tot":"jsonata"},{"t":"set","p":"payload","pt":"msg","to":"payload.msg","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":120,"wires":[["25d23891bc61af28"]]},{"id":"25d23891bc61af28","type":"trigger","z":"b9860b4b9de8c8da","name":"","op1":"","op2":"0","op1type":"pay","op2type":"str","duration":"-250","extend":false,"overrideDelay":true,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":660,"y":100,"wires":[["1b1e36805660c0bf"]]},{"id":"1b1e36805660c0bf","type":"debug","z":"b9860b4b9de8c8da","name":"debug 324","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":200,"wires":[]}]

to start trigger http://ip:1880/my_trigger?msg={%22test%22:1}&delay=5000
To stop trigger http://ip:1880/my_trigger?reset=stop
This could be done in numerous ways. This is an example and may need additional checks.

1 Like

Loving this thread .... In less than an hour you have 3 ways to to do it. .... Any other Ideas?. Threads like this open your mind to the possibilities.

2 Likes

I would like to thank you all for your kind replies!
I will try all the suggestions.

It's true meeki007, this is the power of the community!!!

Best regards to all.

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