OH3 Rule error for email

Guys i just can’t figure out this mail problem at all. Thought i was close with this post, but no luck :frowning:

Error

2021-02-26 21:55:41.133 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'AlarmIssues.rules', using it anyway:
The value of the local variable mailActions is not used
2021-02-26 22:20:23.912 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'AlarmIssues-1' failed: 'sendMail' is not a member of 'org.openhab.core.thing.binding.ThingActions'; line 13, column 7, length 182 in AlarmIssues```


Things

<code>  Thing mail:smtp:Mysmtp [ hostname="smtp.gmail.com", sender="txx", security="STARTTLS",  username="xx.com", password="xx" ] <code>

Rule
 <code>   
rule "The alarm system has a problem"

    when
    //Item SerialConnectionToTheAlarmSystem_TroubleDetected changed
    Item LoungeCabinetZWaveNode12 changed
    then

      val mailActions = getActions("mail","mail:smtp:Mysmtp")
      mailActions.sendMail("colin@xx.com", "HOUSE ALARM TROUBLE ALERT", " The house alarm is detecting a problem, please log on and take a look" )

end
**<code>**

comparing your setup to the documentation the only difference that I see and is also reported as INFO in the log is that the left part - the return value of mailActions is missing:

val success = mailActions.sendMail("colin@xx.com", "HOUSE ALARM TROUBLE ALERT", " The house alarm is detecting a problem, please log on and take a look" )

when i add the 'val success = ’ i get this additional error

2021-02-26 23:13:55.236 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘AlarmIssues.rules’, using it anyway:
The value of the local variable success is not used

Can you try using the mainUI to set it up?

Have you created a app password for email? Sign in with App Passwords - Google Account Help

Add a MAIL thing SMTP server like pic


The password is the APP password you created NOT your main password for your account

Then Create a rule

Select trigger

Then Add Action / Run Script / Rule DSL /

Add code but change the identifier to the one you used.

val mailActions = getActions("mail","mail:smtp:denominator")
val success = mailActions.sendMail("j.g.bowler@gmail.com", "Test subject", "This is the mail content.")

You have installed the mail binding?

There are some hoops to jump through for gmail I think, but would not produce this message.

Thanks, This account works fine with OH2.

I’ll try styling up using the UI, but I prefer the config files

Yes installed mail binding :slight_smile:

Your mail Thing is online ?
Which version of OH3 ?

I checked that the binding was installed again and it wasn’t. not sure how but i was probably tired last night. thanks all for your support so far.

Can i ask though what the purpoise of ‘val success =’ at the start of the rule line ’ mailActions.sendMail’

Also, why do i get these validation errors?
2021-02-27 08:50:25.776 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model ‘AlarmIssues.rules’, using it anyway:
The value of the local variable success is not used
The value of the local variable success is not used
The value of the local variable mailActions is not used

The action can return some text, I dunno what - “OK!” , “Attachment too big”, blah.
If you just use
mailActions.sendMail(" ....
that message is ignored and discarded invisibly.
If you use
val myresults = mailActions.sendMail(" ....
the message is captured in the variable that you create.

Okay, here
val success = mailActions.sendMail(" ..
you’ve created a variable called success but not used it in any way. System is smart enough to see that, recognise it’s a perhaps unintentional nonsense, and warn you.

So, how could you use that variable?

val success = mailActions.sendMail(" ..
logInfo("myrule", "Mailactions says -" + success)

So, knowing that, what’s going on here?
What you showed us creates mailActions and uses it.
I’m guessing it’s something we haven’t been shown - elsewhere in this rule, or outside of any rule, there’s another declaration of mailActions?

As also in another thread nearly the same behavior was reported I was curious if this could be a bug. So I created a setup to send a mail via the binding and it works flawlessly.

Hmm I think I will ignore saving the value.

You are right when you say I didn’t share everything, I have a few rules all causing the same line in the log file hence the multiple rows.

Thanks for your support :+1::+1:

why in my log file do i see this error:

2021-03-01 12:31:23.636 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘AlarmIssues-1’ failed: ‘sendMail’ is not a member of ‘org.openhab.core.thing.binding.ThingActions’; line 12, column 21, length 154 in AlarmIssues

for this rule:

rule “The alarm system has a problem”
when
Item LoungeCabinetZWaveNode12 changed
then
{val mailActions = getActions(“mail”,“mail:smtp:Mysmtp”)
val success = mailActions.sendMail(“colin@xx.com”, “HOUSE ALARM TROUBLE ALERT”, " The house alarm is detecting a problem, please log on and take a look" )
logInfo(“Thermostat - battery level”, “Mailactions says -” + success)}
end

but not get an error with this rule:

rule “4in1 sensor - battery level”

when

Item ZWaveN22_4InOne_BatteryLevel changed

then

if (ZWaveN22_4InOne_BatteryLevel.state <= 10){

     val mailActions = getActions("mail","mail:smtp:Mysmtp")

  val success = mailActions.sendMail("colin@xx.com", "SENSOR BATTERY ALERT", "A sensor in the house is reporting a low battery.")

  logInfo("Thermostat - battery level", "Mailactions says -" + success)

}

end

This one has the ‘wrong kind’ of quotemarks, though that may be an artifact of copy/pasting to this forum.

There’s no reason to have the { curly braces though, which might give odd effects.

Noted, thanks :slight_smile:
Actually the curly brackets were added as a test as they appear in the 2nd rule and not the 1st.
I get the same results now that i’ve removed the curlies…

Still cant figure this out, any suggestions guys?
Thanks
Colin

Not sure what you’re expecting. The error shown does not indicate either of the rules shown (line 12?). You’ve not confirmed whether you’ve got any declarations outside of any rules, or if you’re using the correct quotemarks.

Thanks for taking the time to reply :slight_smile:

Here’s my current config, starting with the error.

Log file error:

2021-03-03 13:13:46.731 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘AlarmIssues-1’ failed: ‘sendMail’ is not a member of ‘org.openhab.core.thing.binding.ThingActions’; line 12, column 21, length 154 in AlarmIssues

Rule from including line 12 which starts ‘val success…’

rule “The alarm system has a problem”

when

//Item SerialConnectionToTheAlarmSystem_TroubleDetected changed

Item LoungeCabinetZWaveNode12 changed

then

  val mailActions = getActions("mail","mail:smtp:Mysmtp")

  val success = mailActions.sendMail("colin@xx.com", "HOUSE ALARM TROUBLE ALERT", " The house alarm is detecting a problem, please log on and take a look" )

logInfo(“Thermostat - battery level”, “Mailactions says -” + success)

end

Okay, for whatever reason the getActions() did not return you a list containing sendMail() at that moment.

That could happen because -
There is no “mail” binding, or its not running?
There is no “mail:smtp:Mysmtp” Thing known to the mail binding?
Perhaps there is a Thing, but its uninitialized?
Perhaps the Thing is fine, but cannot send mail (e.g.not SMTP type)?

Those would all be legitimate errors, but of course there may be a bug kicking around. At the moment that doesn’t look too likely, because this works for others.

If it works sometimes, there might be a timing issue - for example in OH2 startup rules you could do getActions() before a binding was fully ready, and get this kind of result. Later, it works. You haven’t said if you ever get mails this way.

Logging out the actions object doesn’t tell you much,but at least you can see you got one.

val mailActions = getActions("mail","mail:smtp:Mysmtp")
logInfo("test", "object {}", mailActions)
val success = mailActions.sendMail("colin@xx.com", "HOUSE ALARM TROUBLE ALERT", " The house alarm is detecting a problem" )
logInfo("test", "result " + success)

A non-existent Thing gives logs

2021-03-03 14:17:27.687 [INFO ] [.eclipse.smarthome.model.script.test] - object {}
2021-03-03 14:17:27.693 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test mail action': actions cannot be null

A valid smtp Thing (with non-working settings) gives

2021-03-03 14:20:29.757 [INFO ] [.eclipse.smarthome.model.script.test] - object org.openhab.binding.mail.action.SendMailActions@133fbe0
2021-03-03 14:20:30.119 [WARN ] [ab.binding.mail.internal.SMTPHandler] - Sending the email to the following server failed : smtp.gmail.com:25
2021-03-03 14:20:30.120 [WARN ] [ab.binding.mail.internal.SMTPHandler] - com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. f16sm31432698wrt.21 - gsmtp
2021-03-03 14:20:30.122 [INFO ] [.eclipse.smarthome.model.script.test] - result false

A valid smtpThing, but set OFFLINE, gives

2021-03-03 14:24:42.319 [INFO ] [.eclipse.smarthome.model.script.test] - object {}
2021-03-03 14:24:42.320 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'test mail action': actions cannot be null

None of which look quite like yours.

Make very sure you have installed Mail binding v2, and also make sure that you do NOT have Mail Action v1 or Mail binding v1 as well. That should not be possible in OH3, but I don’t think we’ve ever found out what version you are on.

I’ve noticed that of my rules within the same rules file, those that have an ‘if’ statement work, and those that do not give an error.

here are the 2 identical rules but the 2nd version has the ‘if’ statement added, the 1st rule always produces the error (2021-03-04 10:20:18.366 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘AlarmIssues-3’ failed: ‘sendMail’ is not a member of ‘org.openhab.core.thing.binding.ThingActions’; line 33, column 19, length 154 in AlarmIssues)

rule "The alarm system has a problem"
    when
    Item LoungeCabinetZWaveNode12 changed
    then
    val mailActions = getActions("mail","mail:smtp:Mysmtp")
    val success = mailActions.sendMail("colin@blancabeachhouse.com", "HOUSE ALARM TROUBLE ALERT", " The house alarm is detecting a problem, please log on and take a look" )
    logInfo("Thermostat - battery level", "Mailactions says -" + success)
    end


    rule "the alarm system has a problem1"
    when
    Item LoungeCabinetZWaveNode12 changed
    then
    if (ZWaveN22_4InOne_BatteryLevel.state <= 10){
    val mailActions = getActions("mail","mail:smtp:Mysmtp")
    val success = mailActions.sendMail("colin@blancabeachhouse.com","house alarm trouble alert", " The house alarm is detecting a problem, please log on and take a look")
    logInfo("Thermostat - battery level", "Mailactions says -" + success)
    }
    end