# Using a function to run a code once until the input value changes

**URL:** https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432
**Category:** General
**Created:** [25 August 2024 16:21 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432 "2024-08-25T16:21:40Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Desmond-Zeh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/desmond-zeh/32/92216_2.png) [@Desmond-Zeh](https://discourse.nodered.org/u/Desmond-Zeh)
#### Post date: [25 August 2024 16:21 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432/1 "2024-08-25T16:21:40Z")

</div>

Hello!  
I am trying to build a function in node red that runs a code and output the result only once until its input value changes. When the function is triggered, its will receive a global value and assign it to a variable, and then base on the nature of the value received, it will run a code once until the received value changes from its previous state. So far my code keeps on running and producing output even when the received value hasn't changed state. Here below is an image of what I’ve done so far

 ![IMG_0072](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04f2422bc2d4747e41ab5cec0f68e81fbcae1b6d.jpeg)

---

<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: [25 August 2024 16:42 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432/2 "2024-08-25T16:42:26Z")

</div>

Get rid of the function, use a change node to add the global variable into the message. Pass the message through a `filter` node set to block unless changed.

---

<div class="post-metadata">

### Author: ![Desmond-Zeh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/desmond-zeh/32/92216_2.png) [@Desmond-Zeh](https://discourse.nodered.org/u/Desmond-Zeh)
#### Post date: [25 August 2024 18:45 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432/3 "2024-08-25T18:45:28Z")

</div>

Okay Steve, thank you. Let me give it a shot

---

<div class="post-metadata">

### Author: ![Desmond-Zeh](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/desmond-zeh/32/92216_2.png) [@Desmond-Zeh](https://discourse.nodered.org/u/Desmond-Zeh)
#### Post date: [25 August 2024 19:50 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432/4 "2024-08-25T19:50:23Z")

</div>

It works. Thank you very much

---

<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 September 2024 19:50 UTC](https://discourse.nodered.org/t/using-a-function-to-run-a-code-once-until-the-input-value-changes/90432/5 "2024-09-08T19:50:59Z")

</div>

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