# S7node - fonction node - Gmail alerts

**URL:** <https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161>\
**Category:** Industrial\
**Created:** [28 August 2020 15:58 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161 "2020-08-28T15:58:40Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 15:58 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/1 "2020-08-28T15:58:40Z")

</div>

any helps me to get this node, am trying to set a fonction node that if my input variable cross 400 send me an email and what i received from email is this any one can help me to fix this flow  
 ![gmailnode](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/9/29beef379e7025fe60ea26f81e5086f8ebad17d0.jpeg)

 ![nodered](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19b7bbad716685487d92389b3cf281acbc683317.jpeg)

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 16:08 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/2 "2020-08-28T16:08:58Z")

</div>

Hi Tadi,

Can you post your flow ? (delete your username and password from email configuration)  
What you have in your function node .. looks correct.

Maybe what you are sending to your function node is a string and the **if statement** fails ?  
Do you get any errors ?

And the configuration of you email must be correct since your received some emails. What is the problem?

---

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 16:17 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/3 "2020-08-28T16:17:57Z")

</div>

[flows.json](https://discourse.nodered.org/uploads/short-url/jhbmaynkPaYwFQzLQXZbsUH25Ha.json) (9.7 KB)  
here is my flow

---

<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:** [28 August 2020 16:18 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/4 "2020-08-28T16:18:27Z")

</div>

If the payload is not greater than 400 your function sends on the original message, is that what you want? If not then move the return statement inside the braces.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 16:22 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/5 "2020-08-28T16:22:24Z")

</div>

nicely spot

---

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 17:44 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/6 "2020-08-28T17:44:52Z")

</div>

I only want from fonction to send message when it cross 400

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 17:49 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/7 "2020-08-28T17:49:56Z")

</div>

Did you try what Colin suggested ?

The way you have your function now. With `return msg;` outside the if statement { } it always sends a message / email. even if the payload is \> than 400  
`return msg;` should be inside the if { }

---

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 18:36 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/8 "2020-08-28T18:36:26Z")

</div>

Its works fine now but still have an issue that i want to send it once how to?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 18:50 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/9 "2020-08-28T18:50:17Z")

</div>

you can use a **Trigger node** between your function and email nodes,  
to stop more messages being sent for x amount of time.

Example 1 minute :

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4037212d8b9329bef613a619ffb381d934e749e.png)

---

<div class="post-metadata">

**Author:** ![machadotiago](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/machadotiago/32/23776_2.png) [@machadotiago](https://discourse.nodered.org/u/machadotiago)\
**Post date:** [28 August 2020 18:51 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/10 "2020-08-28T18:51:45Z")

</div>

Let me try to help you:

1st: I do not recommend you to start using function for such an easy task. You can simply use the standard Node-RED nodes for that. Use the "switch" node to check if the topic is the expected one, and then another "switch" to check the value, like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/f/4fb9111685cdba5239295bdec62a2e45288e34e7.png)

```auto
[{"id":"e4b04056.2c977","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"83e054d5.6dfb98","type":"switch","z":"e4b04056.2c977","name":"Check topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Default : Surtension","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":290,"y":380,"wires":[["9983d239.c3bef"]]},{"id":"9983d239.c3bef","type":"switch","z":"e4b04056.2c977","name":"Check value","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"400","vt":"num"}],"checkall":"true","repair":false,"outputs":1,"x":500,"y":380,"wires":[["9630c2cd.77661"]]},{"id":"9630c2cd.77661","type":"template","z":"e4b04056.2c977","name":"e-mail text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"ALERT! Value is higher than expected: {{payload}} !","output":"str","x":760,"y":380,"wires":[[]]}]

```

2nd If you want to send an alert only when the value reach a number \>= 400 and then stop sending, you will need to store a variable that tells the flow which state you are at the moment, then you will need to use "flow variables" like in this example below

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/8/18a76062f1d0c232018296cfb5b016ccc63c0ace.png)

```auto
[{"id":"e4b04056.2c977","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"83e054d5.6dfb98","type":"switch","z":"e4b04056.2c977","name":"Check topic","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"Default : Surtension","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":130,"y":320,"wires":[["9983d239.c3bef"]]},{"id":"9983d239.c3bef","type":"switch","z":"e4b04056.2c977","name":"Check value","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"400","vt":"num"},{"t":"lt","v":"400","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":310,"y":320,"wires":[["b45a7d15.6c4e3"],["e4931870.ecc448"]]},{"id":"9630c2cd.77661","type":"template","z":"e4b04056.2c977","name":"e-mail text","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"ALERT! Value is higher than expected: {{payload}} !","output":"str","x":710,"y":300,"wires":[[]]},{"id":"ed3db503.09a3b8","type":"change","z":"e4b04056.2c977","name":"Set alert_sent","rules":[{"t":"set","p":"alert_sent","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":260,"wires":[[]]},{"id":"b45a7d15.6c4e3","type":"switch","z":"e4b04056.2c977","name":"Check alert_sent","property":"alert_sent","propertyType":"flow","rules":[{"t":"false"}],"checkall":"true","repair":false,"outputs":1,"x":490,"y":300,"wires":[["ed3db503.09a3b8","9630c2cd.77661"]]},{"id":"e4931870.ecc448","type":"change","z":"e4b04056.2c977","name":"Reset alert_sent","rules":[{"t":"set","p":"alert_sent","pt":"flow","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":340,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 19:01 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/11 "2020-08-28T19:01:24Z")

</div>

thank you so much i have tried the first one but it wont work  
as i see i have compare the value first and if he passed 400 the message wil be sent to email

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 19:05 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/12 "2020-08-28T19:05:18Z")

</div>

This is not what you want ? ... to send an email **at least once** when value is \> 400 and after that no more emails until the Trigger time expires. so your inbox will not be flooded with emails for X amount of time.

---

<div class="post-metadata">

**Author:** ![tadi](https://avatars.discourse-cdn.com/v4/letter/t/9f8e36/32.png) [@tadi](https://discourse.nodered.org/u/tadi)\
**Post date:** [28 August 2020 19:06 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/13 "2020-08-28T19:06:36Z")

</div>

yes this what i want am trying now your fonction and it seems that work

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [28 August 2020 19:07 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/14 "2020-08-28T19:07:19Z")

</div>

by the way i see you have 3 functions .. you have to put Triggers for all 3

---

<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:** [27 October 2020 19:07 UTC](https://discourse.nodered.org/t/s7node-fonction-node-gmail-alerts/32161/15 "2020-10-27T19:07:28Z")

</div>

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