# Stop the execution of a Function for 30 seconds after Event

**URL:** <https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296>\
**Category:** General\
**Created:** [7 February 2024 16:42 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296 "2024-02-07T16:42:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [7 February 2024 16:42 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/1 "2024-02-07T16:42:37Z")

</div>

Hi, I have a Flow where I Monitor my pv.  
The pv Sends it's data every second.  
If the grid feed is greater than x for 5 seconds, I Set a gpio pin to high. If I buy from the grid for more than 3 seconds the pin gets LOW.  
This works.

What I strugle with:

after a state change of the pin, the state should not be able to change again within 30 seconds.

How can i do that?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 February 2024 16:48 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/2 "2024-02-07T16:48:10Z")

</div>

Hi @Andi, welcome to the forum.

The delay node has a "Rate Limit" option which lets you pass no more than 1 message per 30 seconds. You probably want to set it to "Drop intermediate messages".

---

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [7 February 2024 17:09 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/3 "2024-02-07T17:09:06Z")

</div>

Wow thank you for your fast Response.  
I will try it, as soon as i am at home.  
Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [7 February 2024 18:48 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/4 "2024-02-07T18:48:45Z")

</div>

hm i got the delay working as you said but now i think that this is not what i need.  
im new to node-red and obviously i dont know where my problem actually is..

i rebuild my flow as a test where injection nodes replace the pv-measurements function which is connected to the two switches..

CODE:

```auto
[
    {
        "id": "eb386b679fa0c7d1",
        "type": "tab",
        "label": "Flow 5",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "e5fe2cf44070a6f9",
        "type": "switch",
        "z": "eb386b679fa0c7d1",
        "name": "Einspeise-Schwelle Überschritten",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gte",
                "v": "1250",
                "vt": "num"
            },
            {
                "t": "lt",
                "v": "1250",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 340,
        "y": 180,
        "wires": [
            [
                "94773eb50994da1f"
            ],
            [
                "baa13c7457b59439"
            ]
        ]
    },
    {
        "id": "d166d98b1d38f0d3",
        "type": "inject",
        "z": "eb386b679fa0c7d1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "40",
        "payloadType": "num",
        "x": 90,
        "y": 200,
        "wires": [
            [
                "e5fe2cf44070a6f9"
            ]
        ]
    },
    {
        "id": "26e9d0c2a6dd38f2",
        "type": "inject",
        "z": "eb386b679fa0c7d1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "1",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "1260",
        "payloadType": "num",
        "x": 90,
        "y": 160,
        "wires": [
            [
                "e5fe2cf44070a6f9"
            ]
        ]
    },
    {
        "id": "94773eb50994da1f",
        "type": "change",
        "z": "eb386b679fa0c7d1",
        "name": "Über Schwelle = true",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 160,
        "wires": [
            [
                "0747a1ab7cfaa406"
            ]
        ]
    },
    {
        "id": "baa13c7457b59439",
        "type": "change",
        "z": "eb386b679fa0c7d1",
        "name": "Unter Schwelle = false",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 700,
        "y": 200,
        "wires": [
            [
                "0747a1ab7cfaa406"
            ]
        ]
    },
    {
        "id": "0747a1ab7cfaa406",
        "type": "trigger",
        "z": "eb386b679fa0c7d1",
        "name": "Schwelle für mehr als 5s überschritten",
        "op1": "",
        "op2": "PIN_Einschalten",
        "op1type": "nul",
        "op2type": "str",
        "duration": "5",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "false",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 990,
        "y": 180,
        "wires": [
            [
                "c310147e3fa07e51"
            ]
        ]
    },
    {
        "id": "da781fc5827c82f2",
        "type": "switch",
        "z": "eb386b679fa0c7d1",
        "name": "Bezug Überschritten",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "gt",
                "v": "0",
                "vt": "num"
            },
            {
                "t": "lte",
                "v": "0",
                "vt": "num"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 300,
        "y": 380,
        "wires": [
            [
                "cd417d34b84206e4"
            ],
            [
                "c59fac69ffecb6f2"
            ]
        ]
    },
    {
        "id": "4c6a9a123608fae9",
        "type": "inject",
        "z": "eb386b679fa0c7d1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "0",
        "payloadType": "num",
        "x": 90,
        "y": 400,
        "wires": [
            [
                "da781fc5827c82f2"
            ]
        ]
    },
    {
        "id": "a718e45a2b173860",
        "type": "inject",
        "z": "eb386b679fa0c7d1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "10",
        "payloadType": "num",
        "x": 90,
        "y": 360,
        "wires": [
            [
                "da781fc5827c82f2"
            ]
        ]
    },
    {
        "id": "cd417d34b84206e4",
        "type": "change",
        "z": "eb386b679fa0c7d1",
        "name": "Über Schwelle = true",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "true",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 560,
        "y": 360,
        "wires": [
            [
                "515c04ac0168a86f"
            ]
        ]
    },
    {
        "id": "c59fac69ffecb6f2",
        "type": "change",
        "z": "eb386b679fa0c7d1",
        "name": "Unter Schwelle = false",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "false",
                "tot": "bool"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 560,
        "y": 400,
        "wires": [
            [
                "515c04ac0168a86f"
            ]
        ]
    },
    {
        "id": "515c04ac0168a86f",
        "type": "trigger",
        "z": "eb386b679fa0c7d1",
        "name": "Schwelle für mehr als 3s überschritten",
        "op1": "",
        "op2": "PIN_Abschalten",
        "op1type": "nul",
        "op2type": "str",
        "duration": "3",
        "extend": false,
        "overrideDelay": false,
        "units": "s",
        "reset": "false",
        "bytopic": "all",
        "topic": "topic",
        "outputs": 1,
        "x": 970,
        "y": 380,
        "wires": [
            [
                "a5466e47b8c28d9a"
            ]
        ]
    },
    {
        "id": "a5466e47b8c28d9a",
        "type": "debug",
        "z": "eb386b679fa0c7d1",
        "name": "AUS",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1510,
        "y": 380,
        "wires": []
    },
    {
        "id": "729249d51e624ae1",
        "type": "debug",
        "z": "eb386b679fa0c7d1",
        "name": "EIN",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1550,
        "y": 200,
        "wires": []
    },
    {
        "id": "c310147e3fa07e51",
        "type": "delay",
        "z": "eb386b679fa0c7d1",
        "name": "",
        "pauseType": "rate",
        "timeout": "5",
        "timeoutUnits": "seconds",
        "rate": "1",
        "nbRateUnits": "30",
        "rateUnits": "second",
        "randomFirst": "1",
        "randomLast": "5",
        "randomUnits": "seconds",
        "drop": true,
        "allowrate": false,
        "outputs": 1,
        "x": 1330,
        "y": 260,
        "wires": [
            [
                "729249d51e624ae1"
            ]
        ]
    }
]

```

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/29033edb9fa92b2f746bf511ce07935d2e83dc03.png)

When the upper debug is getting "fired", there should be a 30 seconds runoff where the lower one wont be able to get "fired"  
the same thing do i need vice versa...

is maybe my approach not right?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [7 February 2024 18:54 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/5 "2024-02-07T18:54:02Z")

</div>

Another possible option is the trigger node... set to pass the initial message then after x seconds pass the latest message - without extending the wait time.

---

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [7 February 2024 19:06 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/6 "2024-02-07T19:06:18Z")

</div>

hi dceejay, thanks for your answer, but sadly i dont understand how or where to implement a trigger node..

i will post a picture of the whole flow, maybe am i doing it completely wrong?

thank you for your help 🙂

 ![flow1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef7d419e5ea88be7a1d413b9d95468c4d9e425b8.png)

i have to connect the delay end to somewhere but no matter where, it wont act as i want..

if the "on" debug gets fired, it should lock the lower switch somehow for 30 seconds

---

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [7 February 2024 19:48 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/7 "2024-02-07T19:48:04Z")

</div>

ok i got it working..  
but somehow it feels wrong..  
can someone with experience in node-red tell me if its ok how i do it?

its for avtivating a heating rod, whenever my pv has enough grid feed. as soon as i have to take power from the grid the heating rod should get deactivated. the delay is to prevent the rod from getting switched on and off constantly

 ![flow2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/7875e60edfe838342daec35ff11074fa5181e0c3.png)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [7 February 2024 20:29 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/8 "2024-02-07T20:29:34Z")

</div>

You will probably get more forum assistance, if you present your issue in a simplified flow, especially when your labels are not in English.  
I've created such a flow, just using 2 inject nodes below.  
Please try it, and see it it works for you.

 ![delay](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6f3dabe036c56797058edc9080e9753e1f08e210.jpeg)

```auto
[{"id":"f1c71290bde2ac68","type":"trigger","z":"1326aadbacf36704","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"30","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":330,"y":360,"wires":[["3e330ac26d3c19d7"]]},{"id":"4b2422620c4637bd","type":"inject","z":"1326aadbacf36704","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"ON","payloadType":"str","x":170,"y":360,"wires":[["f1c71290bde2ac68"]]},{"id":"24d30cd2ed50a4e2","type":"inject","z":"1326aadbacf36704","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"OFF","payloadType":"str","x":170,"y":420,"wires":[["3e330ac26d3c19d7","f1c71290bde2ac68"]]},{"id":"f21931e51bd64480","type":"debug","z":"1326aadbacf36704","name":"debug 45","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":720,"y":420,"wires":[]},{"id":"3e330ac26d3c19d7","type":"rbe","z":"1326aadbacf36704","name":"Block unless changes","func":"rbe","gap":"","start":"","inout":"out","septopics":false,"property":"payload","topi":"topic","x":520,"y":420,"wires":[["f21931e51bd64480"]]}]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 February 2024 21:05 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/9 "2024-02-07T21:05:08Z")

</div>

Can you explain the purpose of these two seperate parts of the flow? What do the inject values represent? It might be explained by the labels but unfortunately I speak no German!

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/7/e7092ef1146b893af9693299a426c907662a6f87.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/7/378f371a2b3664d1aea856c7cf4eb36b535c2306.png)

---

<div class="post-metadata">

**Author:** ![Andi](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Andi](https://discourse.nodered.org/u/Andi)\
**Post date:** [8 February 2024 07:31 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/10 "2024-02-08T07:31:10Z")

</div>

Hi, sorry for my confusion... i hope my brain is a bit less confused after sleeping 😃

 ![flow](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/e/bebfcafa39e4503d47c77e6483abdd9f7ea038a2.png)

So i get data from the sma Home manager and convert them.  
This happens till the masuerments function.

Afterwards i check if 1. the grid feed is over 1250W for 5 seconds, if so it sets a GPIO to HIGH  
and 2. if i buy from the grid for over 3 seconds, if so i set the pin to LOW again.  
The delay is to ensure that the pin wont get switched on off constantely..

Is my way generally a good approach?  
I fell like there is something wrong with it..

---

<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:** [8 May 2024 07:31 UTC](https://discourse.nodered.org/t/stop-the-execution-of-a-function-for-30-seconds-after-event/85296/11 "2024-05-08T07:31:22Z")

</div>

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