# Email alert with function node

**URL:** <https://discourse.nodered.org/t/email-alert-with-function-node/9951>\
**Category:** General\
**Created:** [9 April 2019 14:17 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951 "2019-04-09T14:17:58Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JokinSegu](https://avatars.discourse-cdn.com/v4/letter/j/ed655f/32.png) [@JokinSegu](https://discourse.nodered.org/u/JokinSegu)\
**Post date:** [9 April 2019 14:17 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/1 "2019-04-09T14:17:58Z")

</div>

Hi, I am trying to send a notification when I receive a true signal from the PLC. I get the email correctly when it is in true whith thext right text, but it also sends an email when it is false just with the false word and I have trying tio correct that but I don´t know. Here I attached the nodes of the program.  
 ![counter%20email](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0bed9b3ae36a86df2f29dfc55ade93a29311de99.png)  
 ![max%20function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4df087e77f73fd27219ba0f159d7f055ec2508ae.png)  
 ![min%20function](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/f/fff6ade61ec49f2dc96f1fa80b7c7364b42dcb1c.png)

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [9 April 2019 14:29 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/2 "2019-04-09T14:29:20Z")

</div>

Take a look at your function.

If it is true you create the msg.payload you want  
else  
you do nothing

Then you return every message and when your if statement is not true you will send the msg object containing the msg.payload that was sent to the node i.e. false

If you only want to send if true then move your return into your if statement. Or use a switch and a change node instead.

---

<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:** [9 April 2019 14:33 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/3 "2019-04-09T14:33:10Z")

</div>

I think you meant  
`msg = null`  
in the `else` clause. I prefer that way rather than moving the return into the `if` clause. Having returns in the middle of a function will bite you at some point.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 April 2019 15:21 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/4 "2019-04-09T15:21:21Z")

</div>

It is not necessarily `if else`

```auto
if (something === true){
   msg.payload ="it's true!"
   return msg
}

if (something === false){
   msg.payload ="it's false!"
   return msg
}

```

note `===` as the exclamation mark indicates.

---

<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:** [9 April 2019 15:38 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/5 "2019-04-09T15:38:40Z")

</div>

```auto
if (something === true){
   msg.payload ="it's true!"
} else if (something === false){
   msg.payload ="it's false!"
} else {
  msg = null // or whatever is required
}
return msg

```

Simpler to understand I think.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 April 2019 15:51 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/6 "2019-04-09T15:51:15Z")

</div>

To check boolean value, the else case can't be reached. Dead code. Not nice.

```auto
if (something === true){
   msg.payload ="it's true!"
}
else{
   msg.payload ="it's false!"  
}
return msg

```

---

<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:** [9 April 2019 15:53 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/7 "2019-04-09T15:53:40Z")

</div>

> [@hotNipi](#):
>
> o check boolean value, the else case can't be reached. Dead code. Not nice

Who said it was always boolean?

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 April 2019 15:55 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/8 "2019-04-09T15:55:38Z")

</div>

Nobody but code. Its not "true" (type string), strict equality, it seems to be JavaScript, can't see any more options 🙄

---

<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:** [9 April 2019 16:15 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/9 "2019-04-09T16:15:50Z")

</div>

```auto
[{"id":"66851433.bc9ffc","type":"inject","z":"d7ff0732.2f81d8","name":"","topic":"","payload":"false","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":118.5,"y":135,"wires":[["99c57442.a68158","9a889010.30402"]]},{"id":"9df07bb.11ece88","type":"inject","z":"d7ff0732.2f81d8","name":"","topic":"","payload":"Something else","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":143.5,"y":178,"wires":[["99c57442.a68158","9a889010.30402"]]},{"id":"99c57442.a68158","type":"function","z":"d7ff0732.2f81d8","name":"Colin's test it","func":"let something = msg.payload\nif (something === true){\n msg.payload =\"it's true!\"\n} else if (something === false){\n msg.payload =\"it's false!\"\n} else {\n msg.payload = \"it's \" + msg.payload\n}\nreturn msg","outputs":1,"noerr":0,"x":366.5,"y":86,"wires":[["5344b480.9a8294"]]},{"id":"698fdc9e.d7f924","type":"inject","z":"d7ff0732.2f81d8","name":"","topic":"","payload":"true","payloadType":"bool","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":117,"y":92,"wires":[["99c57442.a68158","9a889010.30402"]]},{"id":"5344b480.9a8294","type":"debug","z":"d7ff0732.2f81d8","name":"Colin's","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":531.5,"y":86,"wires":[]},{"id":"9a889010.30402","type":"function","z":"d7ff0732.2f81d8","name":"Alternative test it","func":"let something = msg.payload\nif (something === true){\n msg.payload =\"it's true!\"\n}\nelse{\n msg.payload =\"it's false!\" \n}\nreturn msg","outputs":1,"noerr":0,"x":387,"y":164,"wires":[["7ee07744.958798"]]},{"id":"7ee07744.958798","type":"debug","z":"d7ff0732.2f81d8","name":"Alternative","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":600,"y":164,"wires":[]}]

```

We are discussing my suggestion that it is better to avoid embedded returns, at least I think that is what we are discussing, rather than an actual problem.

---

<div class="post-metadata">

**Author:** ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)\
**Post date:** [9 April 2019 16:37 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/10 "2019-04-09T16:37:08Z")

</div>

Yes I know that but it is bad practice to expect `boolean` value to be something else but `true` or `false`. This is definition of `boolean`. Some talk about this as weakness of JavaScript, who am I to judge. Luckily there isn't `trilean` type of thing to make experiences way more confusing. 🙃

---

<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:** [9 April 2019 20:02 UTC](https://discourse.nodered.org/t/email-alert-with-function-node/9951/11 "2019-04-09T20:02:08Z")

</div>

> [@hotNipi](#):
>
> it is bad practice to expect `boolean` value to be something else but `true` or `false`

I agree entirely, if the code is entirely under your control. However there are times when it is necessary to write code like this. Perhaps in a contrib node or a sample flow where you can't be sure what is coming in at the front and have to allow for anything. But my starting point was some code with embedded returns that appeared to require the true, false or something else test and I was showing how the code to be restructured to allow for that.  
In any case we seem to have wantonly hijacked this thread which we shouldn't do really.
