# Basic function help

**URL:** <https://discourse.nodered.org/t/basic-function-help/20952>\
**Category:** General\
**Created:** [28 January 2020 10:07 UTC](https://discourse.nodered.org/t/basic-function-help/20952 "2020-01-28T10:07:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [28 January 2020 10:07 UTC](https://discourse.nodered.org/t/basic-function-help/20952/1 "2020-01-28T10:07:43Z")

</div>

Basic programming question (sorry)

I am making a rpi-zero that glows a LED when there is a new mail.

I am such a noob when it comes to coding (which is why I love node-red). But I need a basic function that does

checks a mail account through the email-node and if there is a new mail then send a 1 out through output 1 and if not then send a 0.

Flow (without the function) here

```auto
[{"id":"954b31de.53c21","type":"e-mail in","z":"e87f6d57.f477f","name":"","protocol":"IMAP","server":"imap.gmail.com","useSSL":true,"port":"993","box":"INBOX","disposition":"None","criteria":"UNSEEN","repeat":"30","fetch":"auto","inputs":0,"x":90,"y":380,"wires":[["30e3a125.0b5afe"]]},{"id":"30e3a125.0b5afe","type":"function","z":"e87f6d57.f477f","name":"","func":"What to write here","outputs":2,"noerr":9,"x":270,"y":380,"wires":[["61a48cea.d931d4"],["61a48cea.d931d4"]]},{"id":"61a48cea.d931d4","type":"rpi-gpio out","z":"e87f6d57.f477f","name":"","pin":"11","set":"","level":"0","freq":"","out":"out","x":470,"y":380,"wires":[]}]

```

Thanks  
Mathias

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 January 2020 10:25 UTC](https://discourse.nodered.org/t/basic-function-help/20952/2 "2020-01-28T10:25:30Z")

</div>

Since the `e-mail` node only returns the latest email and it can mark that mail as read so you won't see it again, you should be able to remove the `function` node and add a `change` node to set msg.payload to boolean true and have that connect to the `gpio` node.

Then you probably want a way to shut off the lit LED. You could add a `delay` node also attached to the output of the `email` node set to some value. Connect that to a `change` node that sets a boolean false and connect that to the `gpio` node also.

When a email arrives, the light will go on and after N amount of time, the light will go off. Of course if you are away from your computer for N+1 time, you won't see that there was mail so you might want to use a dashboard button to shut off the LED...

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [28 January 2020 11:13 UTC](https://discourse.nodered.org/t/basic-function-help/20952/3 "2020-01-28T11:13:53Z")

</div>

Hi Paul,  
Thanks for your quick reply and suggestion. This was actually similar to how I have approach it to start with but I wanted to try and do it with a function.

Attached a screenshot of that flow with the change node (and using a LED with PWM setting on the rpi).

![Screenshot 2020-01-28 at 12.10.36](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcd74d6eeaaf5badf3b7abd15a8439dea64136fd.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [28 January 2020 11:42 UTC](https://discourse.nodered.org/t/basic-function-help/20952/4 "2020-01-28T11:42:20Z")

</div>

Well have you looked at the documentation about [writing functions](https://nodered.org/docs/user-guide/writing-functions)?  
It will be the same logic, set msg payload to a boolean true (or the number 1) and send that to the `gpio` node, then wait a bit of time and send false. If you google 'javascript delay' you should find how to do a 'delay' and in the 'Writing Functions' documention, you will find out how to `send` multiple messages.

:

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [28 January 2020 12:36 UTC](https://discourse.nodered.org/t/basic-function-help/20952/5 "2020-01-28T12:36:36Z")

</div>

Paul. Your original reply has made a better solution. Thanks. I now have a email notifier that stays on for a 1 minute and then turns off. And checks every hour for unread mail.

Thanks again,

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [28 January 2020 12:40 UTC](https://discourse.nodered.org/t/basic-function-help/20952/6 "2020-01-28T12:40:16Z")

</div>

If you share your solution you could could help others trying to do something similar.

---

<div class="post-metadata">

**Author:** ![matisok](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/matisok/32/3445_2.png) [@matisok](https://discourse.nodered.org/u/matisok)\
**Post date:** [28 January 2020 16:50 UTC](https://discourse.nodered.org/t/basic-function-help/20952/7 "2020-01-28T16:50:27Z")

</div>

Good point.

Here's the flow

[https://flows.nodered.org/flow/01f8c724111531ca7fbd5938e2db1005](https://flows.nodered.org/flow/01f8c724111531ca7fbd5938e2db1005)

And a video of the thing in use (pretty basic) 🙂

Mathias

---

<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:** [28 March 2020 16:50 UTC](https://discourse.nodered.org/t/basic-function-help/20952/8 "2020-03-28T16:50:28Z")

</div>

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