In the documentation of the Mail Binding (Mail - Bindings | openHAB), near the bottom I see this as an example of a Javascript rule:
val mailActions = actions.get("mail","mail:smtp:samplesmtp")
val success = mailActions.sendMail("mail@example.com", "Test subject", "This is the mail content.")
success = mailActions.sendMail("mail1@example.com, mail2@example.com", "Test subject", "This is the mail content sent to multiple recipients.")
I’ve never seen val in Javascript… Is this a typo?
‘val’ is equivalent to a java variable being declared as ‘final’ … whereas ‘var’ is equivalent to a java variable declaration without the ‘final’ keyword.
But what’s the point of making an action (I assume that’s not entirely the correct terminology) a variable? Or a boolean? Is it even possible to get a return false without having an error?
Also, is it good practice to redifine the variable success without var?
The ‘success’ variable is not pointing to the action (java method), it is pointing to the return value that the action produces. In this case, in this binding the return value is true if the action method succeeded in sending the email. (In other cases, in other bindings, actions may return other information; this is why it is documented in the read me).
So you can refer to it later. You don’t have to save the action to a variable but it usually makes the code easier to read and follow when you do.
var mailActions = actions.get("mail","mail:smtp:samplesmtp");
This line of code gets access to the “mail” action on the “mail:smtp:smaplesmtp” Thing. You can have more than one Mail Thing. It saves the action to the variable mailActions;
var success = mailActions.sendMail("mail@example.com", "Test subject", "This is the mail content.");
This line of code actually calls the action, in particular the sendMail action. As @AndrewFG describes, if sending the email succeeds the variable success is set to True. Otherwise it’s set to False.
Most actions will return something useful.
success = mailActions.sendMail("mail1@example.com, mail2@example.com", "Test subject", "This is the mail content sent to multiple recipients.")
This is a second call to sendMail.
The example is a little bit contrived because success is never actually used and the result from the first call to sendMail gets overwritten by the call to the second one. But in general examples are by their very nature are contrived so don’t read too much into details like these. No one example is going to show everything.