Email alert with function node

#1

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.


max%20function
min%20function

0 Likes

#2

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.

0 Likes

#3

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.

0 Likes

#4

It is not necessarily if else

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.

0 Likes

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

0 Likes

#6

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

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

#7

Who said it was always boolean?

0 Likes

#8

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

0 Likes

#9
[{"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.

0 Likes

#10

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

0 Likes

#11

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.

1 Like