Changing the inject node repeat option from UI

Morning all,

I'm new to Node Red so sorry if this is a dumb question.

I am controlling a solenoid to come on for 500 milliseconds and go off for 20 mins. This all works fine. I would however like to allow a variable time to be entered from the UI to set the 20 min wait time.

I've tried to upload my json file but the forum won't let me as i'm a new user?

So to describe my setup I have an inject with the repeat interval set to 20 mins going into a trigger set to 500ms going to a Pi output pin for the solenoid.

I would really appreciate your help.

Thanks

Grigsy

The inject node cannot be programmatically set, but you can do what you want in another way - check out Colin's neat solution or the sophisticated node-red-contrib-msg-resend node.

One way would be to replace the repeating inject with a function node containing a JavaScript setTimeout function.

Grigsy,

Have you had a look at the BigTimer contrib node?

Morning all,

Thanks for your feedback.

I have had a look at the suggestions above but not really go anywhere however I have had a play with the change node and I am very nearly there.

For context I am trying to allow for a time to be selected in the UI that will set a timer for the drum on my pond. This will trigger the drum to wash every X minutes. So you set the time for the drum to wait before a 500ms trigger is sent then repeat the "wait time".

The attached now works for the input timer and works the drum, however if I change the time, it runs two different timing intervals.

It appears that I need to clear the Global.delay and the the msg.delay before injecting a new time.

Your help as always is greatly appreciated!

Thanks

Julian

I've pasted below as I am still not allowed to upload attachements.

[{"id":"6fdfb8a.b02d748","type":"tab","label":"Drum Timer","disabled":false,"info":""},{"id":"cb9e3ab0.4048f8","type":"rpi-gpio out","z":"6fdfb8a.b02d748","name":"","pin":"11","set":"","level":"0","freq":"","out":"out","x":560,"y":440,"wires":},{"id":"c2b3185b.efcb18","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"delay","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":280,"wires":[["f41b5591.e6e738"]]},{"id":"fe45d2c3.71a03","type":"delay","z":"6fdfb8a.b02d748","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":280,"wires":[["7b8e3ee9.8a41d","50fc16d9.235f58","c2b3185b.efcb18"]],"outputLabels":["0"]},{"id":"f41b5591.e6e738","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":280,"wires":[["fe45d2c3.71a03"]]},{"id":"47481bf6.e86d94","type":"ui_dropdown","z":"6fdfb8a.b02d748","name":"","label":"Set Drum Timer","tooltip":"","place":"Select option","group":"38b8797e.5fd136","order":4,"width":0,"height":0,"passthru":true,"options":[{"label":"1 Minutes","value":60000,"type":"num"},{"label":"2 Minutes","value":120000,"type":"num"},{"label":"15 Minutes","value":900000,"type":"num"},{"label":"25 Minutes","value":1500000,"type":"num"},{"label":"30 Minutes","value":1800000,"type":"num"},{"label":"45 Minutes","value":2700000,"type":"num"},{"label":"10 seconds","value":10000,"type":"num"},{"label":"20 seconds","value":20000,"type":"num"}],"payload":"","topic":"delay","x":80,"y":280,"wires":[["c2b3185b.efcb18"]]},{"id":"7b8e3ee9.8a41d","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":360,"wires":[["4d6c34e5.9839dc","895e26f5.c66b88"]]},{"id":"50fc16d9.235f58","type":"debug","z":"6fdfb8a.b02d748","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":280,"wires":},{"id":"4d6c34e5.9839dc","type":"simpletime","z":"6fdfb8a.b02d748","name":"","x":470,"y":360,"wires":[["151d975d.abf959"]]},{"id":"151d975d.abf959","type":"ui_text","z":"6fdfb8a.b02d748","group":"38b8797e.5fd136","order":3,"width":0,"height":0,"name":"","label":"Time of Last Wash","format":"{{mydate}} {{mytime}}","layout":"col-center","x":650,"y":360,"wires":},{"id":"895e26f5.c66b88","type":"trigger","z":"6fdfb8a.b02d748","op1":"0","op2":"1","op1type":"num","op2type":"num","duration":"500","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":400,"y":440,"wires":[["cb9e3ab0.4048f8"]]},{"id":"38b8797e.5fd136","type":"ui_group","z":"","name":"Pond Filter","tab":"e438b126.3fec6","order":2,"disp":true,"width":"6","collapse":false},{"id":"e438b126.3fec6","type":"ui_tab","z":"","name":"Julian's Pond","icon":"dashboard","disabled":false,"hidden":false}]

Unfortunately your flow isn't currently importable.Please read the following post How to share code or flow json and then edit the above message.

[{"id":"6fdfb8a.b02d748","type":"tab","label":"Drum Timer","disabled":false,"info":""},{"id":"cb9e3ab0.4048f8","type":"rpi-gpio out","z":"6fdfb8a.b02d748","name":"","pin":"11","set":"","level":"0","freq":"","out":"out","x":560,"y":440,"wires":[]},{"id":"c2b3185b.efcb18","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"delay","pt":"global","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":280,"wires":[["f41b5591.e6e738"]]},{"id":"fe45d2c3.71a03","type":"delay","z":"6fdfb8a.b02d748","name":"","pauseType":"delayv","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":620,"y":280,"wires":[["7b8e3ee9.8a41d","50fc16d9.235f58","c2b3185b.efcb18"]],"outputLabels":["0"]},{"id":"f41b5591.e6e738","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"delay","pt":"msg","to":"delay","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":280,"wires":[["fe45d2c3.71a03"]]},{"id":"47481bf6.e86d94","type":"ui_dropdown","z":"6fdfb8a.b02d748","name":"","label":"Set Drum Timer","tooltip":"","place":"Select option","group":"38b8797e.5fd136","order":4,"width":0,"height":0,"passthru":true,"options":[{"label":"1 Minutes","value":60000,"type":"num"},{"label":"2 Minutes","value":120000,"type":"num"},{"label":"15 Minutes","value":900000,"type":"num"},{"label":"25 Minutes","value":1500000,"type":"num"},{"label":"30 Minutes","value":1800000,"type":"num"},{"label":"45 Minutes","value":2700000,"type":"num"},{"label":"10 seconds","value":10000,"type":"num"},{"label":"20 seconds","value":20000,"type":"num"}],"payload":"","topic":"delay","x":80,"y":280,"wires":[["c2b3185b.efcb18"]]},{"id":"7b8e3ee9.8a41d","type":"change","z":"6fdfb8a.b02d748","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":360,"wires":[["4d6c34e5.9839dc","895e26f5.c66b88"]]},{"id":"50fc16d9.235f58","type":"debug","z":"6fdfb8a.b02d748","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":280,"wires":[]},{"id":"4d6c34e5.9839dc","type":"simpletime","z":"6fdfb8a.b02d748","name":"","x":470,"y":360,"wires":[["151d975d.abf959"]]},{"id":"151d975d.abf959","type":"ui_text","z":"6fdfb8a.b02d748","group":"38b8797e.5fd136","order":3,"width":0,"height":0,"name":"","label":"Time of Last Wash","format":"{{mydate}} {{mytime}}","layout":"col-center","x":650,"y":360,"wires":[]},{"id":"895e26f5.c66b88","type":"trigger","z":"6fdfb8a.b02d748","op1":"0","op2":"1","op1type":"num","op2type":"num","duration":"500","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":400,"y":440,"wires":[["cb9e3ab0.4048f8"]]},{"id":"38b8797e.5fd136","type":"ui_group","z":"","name":"Pond Filter","tab":"e438b126.3fec6","order":2,"disp":true,"width":"6","collapse":false},{"id":"e438b126.3fec6","type":"ui_tab","z":"","name":"Julian's Pond","icon":"dashboard","disabled":false,"hidden":false}]

You will understand the issue if you debug the property msg._msgid in the output of your delay node.

What happens is that each time you change the time you are introducing a new message in your flow. These messages will loop forever in your flow.

This is a modification of your flow to ease the understanding.

r-02

Hi Andrei and thank you for your response.

I understand that I am sending multiple messages that never run out my issue is I am unsure how to resolve the problem.

I have tried addiing a change node and setting the global.delay and msg.delay to 0 then passing the msg.payload through but his doesn't work.

Any further help would be appreciated.

Julian

Not ideal but I have added a button that injects a Msg.reset = true to the delay mode so I can reset it and the set the new wait time.

Thanks for your help everyone

1 Like

I found this thread as I wanted the same functionality. I slightly tweaked the code, but result is the same:

This flow functions and an inject node, but allows the interval to be changed.

The basic principle that the delay node output if fed back to itself through a change node which sets the appropriate delay for each message.

The 3 sample inject node set the interval to 1s, 2s, 5s. The interval is sent in the msg.payload in milliseconds.

These inject nodes can be supplemented with a dashboard input field (slider, dropdown) so it can be controlled from the screen as well.

This flow does not start sending out messages when deployed, if that is required, create another inject node with default rate and set to trigger on startup.

I also added a "Autostart with saved value" this should be used if the flow variable is saved to file (in that case update the change nodes to use the file storage) so it can restart the trigger with the last delay value read from the file system (set this inject node to trigger on startup).

The interval is stored in a flow context variable, and this value is inserted into the message every time (msg.delay) by the change node above the delay node. This ensures that if the rate value in the context value changes, it is applied at the next message.

I added the delay "100ms" and the "msg.reset" nodes, because when the rate changes it introduces a new message the recursive loop and that would mess up the timing.

"Stop" inject node resets the delay node (deleted the message that keeps triggeting itelf) and stops the process.

[
    {
        "id": "3234f262056babaf",
        "type": "delay",
        "z": "f6b1667f7ee0d1b0",
        "name": "Recursive delay",
        "pauseType": "delayv",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": true,
        "outputs": 1,
        "x": 780,
        "y": 720,
        "wires": [
            [
                "63daa2969066caec",
                "572e6832bcf30618"
            ]
        ]
    },
    {
        "id": "63daa2969066caec",
        "type": "debug",
        "z": "f6b1667f7ee0d1b0",
        "name": "debug 34",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 1020,
        "y": 720,
        "wires": []
    },
    {
        "id": "d6577d4db9cbded4",
        "type": "inject",
        "z": "f6b1667f7ee0d1b0",
        "name": "1s rate",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1000",
        "payloadType": "num",
        "x": 130,
        "y": 580,
        "wires": [
            [
                "4158a248e1e2b3e4"
            ]
        ]
    },
    {
        "id": "b97d8dcaebe9cc85",
        "type": "inject",
        "z": "f6b1667f7ee0d1b0",
        "name": "2s rate",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "2000",
        "payloadType": "num",
        "x": 130,
        "y": 620,
        "wires": [
            [
                "4158a248e1e2b3e4"
            ]
        ]
    },
    {
        "id": "9415f762de6c9ced",
        "type": "inject",
        "z": "f6b1667f7ee0d1b0",
        "name": "5s rate",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "5000",
        "payloadType": "num",
        "x": 130,
        "y": 660,
        "wires": [
            [
                "4158a248e1e2b3e4"
            ]
        ]
    },
    {
        "id": "4158a248e1e2b3e4",
        "type": "change",
        "z": "f6b1667f7ee0d1b0",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "ratelimit",
                "pt": "flow",
                "to": "payload",
                "tot": "msg"
            },
            {
                "t": "set",
                "p": "topic",
                "pt": "msg",
                "to": "tick",
                "tot": "str"
            },
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "trigger",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 360,
        "y": 620,
        "wires": [
            [
                "b300a73d7dc605ae",
                "dc0ddfa85869080c"
            ]
        ]
    },
    {
        "id": "572e6832bcf30618",
        "type": "change",
        "z": "f6b1667f7ee0d1b0",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "delay",
                "pt": "msg",
                "to": "ratelimit",
                "tot": "flow"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 780,
        "y": 620,
        "wires": [
            [
                "3234f262056babaf"
            ]
        ]
    },
    {
        "id": "bf8022099b6ce347",
        "type": "inject",
        "z": "f6b1667f7ee0d1b0",
        "name": "Stop",
        "props": [
            {
                "p": "reset",
                "v": "",
                "vt": "date"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 510,
        "y": 780,
        "wires": [
            [
                "3234f262056babaf"
            ]
        ]
    },
    {
        "id": "966379b5822bea96",
        "type": "comment",
        "z": "f6b1667f7ee0d1b0",
        "name": "Programmable interval inject",
        "info": "This flow functions and an inject node, but allows the interval to be changed.\nThe basic principle that the delay node output if fed back to itself through a change node which sets the appropriate delay for each message.\n\nThe 3 sample inject node set the interval to 1s, 2s, 5s. The interval is sent in the msg.payload in milliseconds.\nThese inject nodes can be supplemented with a dashboard input field (slider, dropdown) so it can be controlled from the screen as well.\nThis flow does not start sending out messages when deployed, if that is required, create another inject node with default rate and set to trigger on startup. \nI also added a \"Autostart with saved value\" this should be used if the flow variable is saved to file (in that case update the change nodes to use the file storage) so it can restart the trigger with the last delay value read from the file system (set this inject node to trigger on startup).\n\nThe interval is stored in a flow context variable, and this value is inserted into the message every time by the change node above the delay node. This ensures that if the rate value in the context value changes, it is applied at the next message.\n\nI added the delay 100ms and the msg.reset nodes, because when the rate changes it introduces a new message the recursive loop and that would mess up the timing.\n\n\"Stop\" inject node resets the delay node (deleted the message that keeps triggeting itelf) and stops the process.",
        "x": 170,
        "y": 520,
        "wires": []
    },
    {
        "id": "20fc9533e15a9b16",
        "type": "inject",
        "z": "f6b1667f7ee0d1b0",
        "name": "Autostart with saved value",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "tick",
        "payload": "trigger",
        "payloadType": "str",
        "x": 510,
        "y": 560,
        "wires": [
            [
                "572e6832bcf30618"
            ]
        ]
    },
    {
        "id": "dc0ddfa85869080c",
        "type": "change",
        "z": "f6b1667f7ee0d1b0",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "reset",
                "pt": "msg",
                "to": "reset",
                "tot": "str"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 460,
        "y": 720,
        "wires": [
            [
                "3234f262056babaf"
            ]
        ]
    },
    {
        "id": "b300a73d7dc605ae",
        "type": "delay",
        "z": "f6b1667f7ee0d1b0",
        "name": "",
        "pauseType": "delay",
        "timeout": "100",
        "timeoutUnits": "milliseconds",
        "rate": "1",
        "nbRateUnits": "1",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": false,
        "allowrate": false,
        "outputs": 1,
        "x": 550,
        "y": 620,
        "wires": [
            [
                "572e6832bcf30618"
            ]
        ]
    }
]