Javascript 'val'? - Mail binding documentation

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.

And why is there val on the second line and not the third line? And if val is ‘final’, why are there two declarations?

If that example chains to be JS the val is incorrect. It should be var, let, or const. It needs to be corrected.

There is a quirk in how scripts get reused in managed rules (i.e. the UI) where you can only use var unless it’s inside a context (i.e. inside { }).

And how would that variable success be used?

By name like any other variable in any of the rules languages.

console.log( success );

Maybe I didn’t understand the question.

if (!success) {
  log something, or
  do something, or
  just ignore it
}

@rlkoshak will you open an issue on GitHub?

I might need a ping on Monday if I forget. I’m traveling in the mountains correctly and have very intermittent network access.

The PR is pretty simple though, just change “val” to “var” and we’re good.

I’ll try to get something no later than Monday morning, but if someone wants to take it on please do.

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

1 Like

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.