# Write file only when button is triggered

**URL:** <https://discourse.nodered.org/t/write-file-only-when-button-is-triggered/43370>\
**Category:** General\
**Created:** [29 March 2021 16:22 UTC](https://discourse.nodered.org/t/write-file-only-when-button-is-triggered/43370 "2021-03-29T16:22:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![limpstar](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@limpstar](https://discourse.nodered.org/u/limpstar)\
**Post date:** [29 March 2021 16:22 UTC](https://discourse.nodered.org/t/write-file-only-when-button-is-triggered/43370/1 "2021-03-29T16:22:02Z")

</div>

hello guys

I am reading data from an Arduino. This data is being written into a file on the Raspi. The way it works now is that as soon as the Arduino sends anything, the Raspi immediatly writes the file. What I would like to have is that the file is only written, when a button was pushed before. So as long as the button is not pressed, no file will be written, altough data is being sent / received.  
On the attached image are the basic nodes (without the buttons). Somehow the button(s) need to interfere with the connection marked in green on the image, but I don't know how to do this. I cannot just insert a button, because in fact I have two buttons and the user has to click one of them before the file should be created.

![write](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/e/2e561cc2af9a2f53c686c24a6a47c2e900cb90f0.jpeg)

see code below:

```auto
[{"id":"182658c8.e32607","type":"file","z":"89d348aa.ccebc8","name":"S0Zaehler","filename":"","appendNewline":true,"createDir":true,"overwriteFile":"false","encoding":"none","x":790,"y":180,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 March 2021 16:46 UTC](https://discourse.nodered.org/t/write-file-only-when-button-is-triggered/43370/2 "2021-03-29T16:46:28Z")

</div>

You could use a node-red-contrib-queue-gate set to max queue size of 1 and to Keep Newest Message, with default state Closed. Make the button send it a Flush command. The node will buffer within itself the most recent message and when the button sends the flush it will release that last message.

It would be more efficient to put it earlier in flow so it is not wasting its time preparing the data only for it to be thrown away.

---

<div class="post-metadata">

**Author:** ![limpstar](https://avatars.discourse-cdn.com/v4/letter/l/71e660/32.png) [@limpstar](https://discourse.nodered.org/u/limpstar)\
**Post date:** [29 March 2021 18:58 UTC](https://discourse.nodered.org/t/write-file-only-when-button-is-triggered/43370/3 "2021-03-29T18:58:12Z")

</div>

thx, I will try this.
