How to queue SMTP and OpenHAB Notifications if services are Offline?

Here is my Rule for sending Notifications via “OpenHAB Notification” service and also SMTP. Works great except if the connection or service is down.

rule "Alarm Alerting"
when
	Member of Alarms changed to "ON" or //  Send a Notification with Details of what Device set the Alarm ON or
    Member of Alarms changed from "ON" to "OFF" // If already in the ON state to OFF (but not UNDEF to OFF etc)

then
	val device = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("_Alarm",""))  // This gets the item details with the same name as the alarm
	logInfo("notifications", "Alarm for " + device.label + " changed to " + triggeringItem.state + ", and the current value is " + device.state)
	sendNotification("xx@xx.xx.xx", "Alarm for " + device.label + " changed to " + triggeringItem.state + ", and the current value is " + device.state, "alarm","High")
	val mailActions = getActions("mail","mail:smtp:3942f001")
	mailActions.sendMail("xx@xx.xx.xx", "Notication", "Alarm for " + device.label + " changed to " + triggeringItem.state + ", and the current value is " + device.state)
end

Any suggestions on how to build in some resilience where the msg check and wait for the SMTP and Notification service is online without blocking each other or additional “Alarm Alerting” rule if multiple are triggered while connectivity is down?

Your first challenge is to determine if the services are available or not. Otherwise you won’t be able to manage any queuing.

Rich’s Gate Keeper design includes an example of queue management

How about testing for an ERROR / WARN post the attempt to send? I see in the logs I get these when the service is not reachable after an attemp to send the notifications:

2020-07-28 08:09:09.542 [ERROR] [io.openhabcloud.internal.CloudClient] - Error connecting to the openHAB Cloud instance: {}
2020-07-28 08:09:16.269 [WARN ] [ab.binding.mail.internal.SMTPHandler] - Sending the email to the following server failed : xx.xx.xx.xx:123

Is there a programmatic response from the SMTP / OpenHAB bindings that can be evaluated? …and if not “OK” then repeat in 1min?

You can try putting the call in your Rule into a try/catch but given that the log statement is a warning level and there is no stack trace I suspect there isn’t an exception thrown.

In which case, you could work around this by using the LogReader binding to watch the log. Set a timer to wait a minute and if the LogReader binding saw that warning, try sending the email again. Using Design Pattern: Looping Timers might be a good idea.

Different answer for SMTP: set up postfix to queue your email and have it send when the outside service is available (which it will do automatically). I have all of my local devices send email notifications to my postfix server, then it sends to my email account. This makes it easier later when I change passwords as well. Just a thought.

Lots of good ideas. Another one would be to simply pre-ping my external mail server and retry every minute if no response. It would not be built proof but would at least rule out loss of local internet connection and would be easy to implement…

Some Actions return a response e.g.
myResults = mailActions.sendMail(" ...

I’ve seen nothing to suggest either of the Actions you are using do, but maybe worth a try?
Even if it does give something, it might well be of limited use and not conform delivery in any way.

A devious thought for mail … you can access mailbox counts through the mail binding. you might get a count for your ‘outbox’, do your send, then in a while check to see if outbox count changed.

EDIT - update for mail actions, I’d quite forgotten this -


Action may already provide a meaningful response, not clear to me.

Hi John,
I would be interested how you made your setup. Did you configure postfix as a smarthost and then using the mail-binding?

Yes, I used the mail binding. I followed the postfix documentation here: http://www.postfix.org/documentation.html, mostly in the basic configuration and small/home office sections. I did not configure postfix to be able to receive mail externally; it only receives from the local subnet and then forwards outbound. This is not too hard to do. I did need to make sure it set the return address and domain acceptably to my ISP. And no need to configure SASL either since I trust everything on my local subnet. There are also many tutorials out there on postfix such as this one: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-postfix-on-ubuntu-20-04

I can post my configuration files if they are of interest.

openHABian comes with the option to install exim as a mail relay.

Thanks for the infos! My goal is to ensure mails are being sent after a possible network disconnect. Currently I send notifications via Line-Messenger and in my rule I have a failback if the Messenger-send fails. As I don’t wanna take care of mail queueing mechanism within OH, I am looking for a mailserver which acts as smarthost to handle this. Does Exim has this mechanism? (Sorry to ask, I am only fimilar with sendmail)

Yes, it has. Exim, Postfix, Sendmail all are MTAs.

1 Like

Yes, and that’s what openHABian generates the config for
(you can also change it if you like)

You a greybeard? Last time I did was in the 90’s …

NoBeard but already old :smiley: Still trying to get Cobol-Programs to work as OH-Scripts :smiley:

I will have a look into Exim tomorrow morning and for the next weekend I want to try you automatic backup :slight_smile:

Use master branch there have been recent changes. Mind it’s still beta testing.

1 Like

I did something like you want . I check for WAN connection issues. I check two sites for my own IP using http binding. If a site is unreachable, timed out, then the Network binding throws an error in the log. I use logreader binding to look for the error or success of the check. I also use the network binding to ping the router to check if thats up or not.
If both sites were unreachable then the next time they become reachable (and router is online) I send an email to let me know what happened

In my case I also get openHAB to send a reboot command to the router as that normal fixes my connectivity issue (SIM card not readable)