Basic function help

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

[{"id":"954b31de.53c21","type":"e-mail in","z":"e87f6d57.f477f","name":"","protocol":"IMAP","server":"","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":[]}]


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...

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

Well have you looked at the documentation about 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.


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,

1 Like

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

Good point.

Here's the flow

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



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