|
Post by papa on Jan 24, 2017 14:42:10 GMT
Introduction & Preliminaries Updated April 22, 2020
OpenHAB's User Interface seen on a computer browser or a smart phone app is informative. However, one can also email openHAB data updates to oneself. This is another way to get attention for useful, even critical, information from the openHAB network. Such information also gets a time stamp in the email. I have used this approach to email me important changes in garage door, room humidity, sump level, freezer temperature, & node-controlled lights. That's very helpful when I'm away from home. I will document this approach for Gmail. Below computourist & I help document using the sendBroadcast method. This documentation assumes openHAB 2.5.x & a MQTT Broker are installed on a computer connected (by Ethernet cable) to your local network. For help with openHAB 2.5.x & MQTT Broker, see this thread. This documentation also assumes you've built a working computourist-style gateway & at least one end node. See this thread for help with that.
|
|
|
Post by papa on Jan 24, 2017 14:42:57 GMT
Tweak a Gmail Account Setting What demondreamer recommended at the top of this post worked for me: I believe you can find this Gmail setting to change near the bottom of this page. Look for "Less secure app access." Click the link "Turn on access (not recommended)"
Note: Gmail will warn you (more than once) that this is a security risk. Also Google will automatically turn this setting OFF if it’s not being used. I have a fairly robust email password & when I've used this, I've not had a problem so far.
|
|
|
Post by papa on Jan 24, 2017 14:48:13 GMT
Add to OpenHAB 2.5.x's Configurations: Install the Mail Binding
Updated April 22, 2020
Formerly Mail was a OH version 1 Action. Now it's also a version 2 Binding. Go to openHAB's PaperUI/Add-ons/Bindings & install Mail Binding
The indicator may spin a while until the install is complete.
Continues below
|
|
|
Post by papa on Jan 24, 2017 14:50:28 GMT
More Additions to OpenHAB's Configurations Updated April 25, 2020
The following settings are for Gmail. For an email service other than Gmail, research its settings & apply them in the parameter fields indicated just below.
Go to \PaperUI\Inbox, & click on SEARCH FOR THINGS
In the next window, click on Mail Binding.
In next window, click on SMTP SERVER, "Used for sending emails via rule actions" (Not IMAP)
Enter the parameters: Leave the default name or rename (I used Gmail SMTP.)
You can leave the assigned Thing ID code, but copy & record it for use below.
Sender: [Your email address] Server Hostname: smtp.gmail.com SMTP Server Security Protocol: SSL/TLS SMTP Server Username: [Your email address] SMTP Server Password: [Your email password] Click "SHOW MORE" Server Port: 587 465 To Save, click the checkmark in a blue disc.
===============================
The above works but the log will warn (does not seem to hurt): "javax.mail - expected resource not found: /META-INF/javamail.default.address.map"
|
|
|
Post by papa on Jan 24, 2017 14:56:18 GMT
Updated, April 27, 2020
Again the following assumes a computourist style gateway which communicates with a DHT end node 05. Node 05 has a DHT sensor which supplies temperature via device 48 & humidity percent via device 49.
The following requires creating a Node05 Thing with a Node05Channel49.
items file example (subscribes openHAB to receiving the humidity data via the gateway): Number Humidity_Node05 "Humidity_Node05 [%d %%]" <humidity> { channel="mqtt:embedded-mqtt-broker:topic:mything:Node05Channel49" } Given the following rule, OpenHAB sends an email when humidity is 50% or more, that is when humidity is above the sweet spot.
Note: Edit your info into the rule: Your Gmail address & use the SMTP Server Thing ID you copied above OR Go to PaperUI/Configurations/Things. Under your SMTP Server Thing, see the needed code after "mail:smtp:"
rules file example:
Note: According to official OH docs, after mailActions.sendMail(, one can put more than one email address (enclose in double quotes & separate with a commas). With other items & rules using them, OpenHAB can send emails for other key data.
|
|
|
Post by computourist on Jan 25, 2017 11:05:30 GMT
Hi papa , nice writeup. I use the notification function of the openHAB App. By including a rule like: rule "notify OUT2" // send notification of node state change when Item STAT2 changed then df = new SimpleDateFormat( "dd-MM-YYYY HH:mm:ss" ) state = "Node 2 changed: " + df.format( new Date() ) sendBroadcastNotification(state) end I get an alert on my mobile device on every state change. Easy to implement and no further configuration needed. Regards, Computourist ====================================== papa, April 25, 2020: computourist's version above did not work for me. This version did:
|
|
|
Post by papa on Jan 25, 2017 12:45:34 GMT
Thanks, computourist, for the kudos & documenting another way to do notifications, which I intend to do something with down the road. I believe a case could be made for using both notification approaches. Generally speaking, a sendBroadcast notification could get more immediate attention for a critical change in an item state, like a sump cistern too high or too low (due to a pump stuck on). It could also get annoying for often-changing, not-so-critical state changes pinging one's mobile device. I believe sendBroadcast depends on a connection to my.openhab.org cloud, which is down some times. It also depends on one's registering at my.openhab.org & installing openHAB Cloud Connector at PaperUI/Addons/Misc. Updated, April 25, 2020: In this thread, I document " Access Your OpenHAB via myopenhab.org / openHAB Cloud Connector" By default, openHAB Cloud Connector is installed with remote access, which includes the ability to send notices to the openHAB mobile app. You may check the openhabcloud.config file in openHAB's \userdata\config\org\openhab folder. Look for the line: mode="remote"
Email notifications should work whether or not the my.openhab.org connection is working or not. When I just want to see if certain things happened or did not happen during a day, the emails are sufficient.
|
|
|
Post by papa on Apr 30, 2020 20:19:23 GMT
Send Multiple State Updates in One Email Introduction
Previously, I've had openHAB email me one status change at a time. Typically these were two status changes (ON & OFF) per day. All I needed to see (including the email timestamp) was visible without opening each email. I could check & delete a group of emails. Sending several emails a day is not normally a problem. However, sometimes when a node had many state changes, openHAB overflowed my Inbox & the Gmail service objected. A very critical update may warrant its own email or sendBroadcastNotification (see above). However, I often just want updates every several hours or even once per day.
So the following documents "Sending Multiple State Updates in One Email."
The following assumes using the April, 2020 updates found above in this thread. Next, OpenHAB Items Used to Send Multiple State Updates in One Email
|
|
|
Post by papa on Apr 30, 2020 20:19:59 GMT
Send Multiple State Updates in One Email Adding OpenHAB Items
Adapt the following to your nodes & the states you want reported.
The following assumes having a Node05 Thing with a Node05Channel16 & a Node59 Thing with a Node59Channel16. These represent two nodes whose Channel 16 can toggle a relay & report ON/OFF states. The Switch Item Act_Node05 links to Node05Channel16 & Switch Item Act_Node59 links to Node59Channel16. Added entries in .items file(s), two per node (timestamp last On & last OFF for each node): String Node 05Last On String Node 05Last Off String Node 59Last On String Node 59Last Off Next, OpenHAB Rules to Send Multiple State Updates in One Email
|
|
|
Post by papa on Apr 30, 2020 20:45:49 GMT
OpenHAB Rules to Send Multiple State Updates in One Email email-Key-States-pub.rules (1.71 KB) << Download this .rules file to openHAB's /conf/rules folder. Edit the file so the Item references match your nodes. Each of the file's first two rules timestamp a node item's change of state. Into the variable "eUpDate" the last rule compiles the timestamps & a brief label for each timestamp. Using a Mail Binding installation (see above), the rule emails eUpDate.
Using the model of the first two rules, you can create more rules to timestamp other state changes.
In the last rule, follow the pattern in the eUpDate compilation to add reports of more state updates.
The last rule's time cron statement determines when the rule triggers. As is, the rule fires "at second :07, at minute :05, every 3 hours starting at 00am, every month between April and October." For now, it fires basically every 3 hours (8 times per day). I avoid the exact beginning of a 3-hour period because other rules might trigger then. Later, I may schedule one or two updates per day. With some study, you can use this cron expression generator to create the timed rule trigger you want. Next, An Example of Multiple State Updates in One Email
|
|
|
Post by papa on Apr 30, 2020 21:05:25 GMT
An Example of Multiple State Updates in One Email ^^ an email's header (click on pic for larger view)
Perhaps the above will give you a start that you can adapt. Official openHAB docs also have a command for sending HTML formatted emails. Moreover, more than one email address can receive an email.
|
|
|
Post by papa on Nov 8, 2020 21:22:50 GMT
TROUBLESHOOTING At some point, Gmailing myself updates of openHAB states stopped working. Once in a while I'd do some research & try things, but had no success. I rechecked related settings in the OH Dashboard: The Mail Binding was still installed.
The SMTP Server Thing that I named Gmail SMTP was online & seemed to have the right parameters. Yet no Gmail sending happened & openhab.log recorded:
Error during the execution of rule 'eMail Key States': 'sendMail' is not a member of 'org.eclipse.smarthome.core.thing.binding.ThingActions' Online, others expressed frustration with this. I researched the above error more & tried suggestions, particularly clearing the openhab cache & rebooting several times. No change. Then I checked my Gmail account settings, & "allow less secure apps" was turned off as Gmail promised to do if the feature was not being used. I turned the feature back on ( see here), but doing that did not yet solve the overall problem. Then I saw a solution that said restart openHAB, but do NOT clear the cache. I tried that & Gmailing updates seemed to be working.
I did get a openhab.log warning ( see here: seems to be a bug that does not interfere with the sending): [javax.mail ] - expected resource not found: /META-INF/javamail.default.address.map Hmmm, several Windows updates would have restarted openHAB without clearing the OH cache, so I'm skeptical that ONLY restarting OH without clearing the cache fixed the failure of the sending. Perhaps multiple restarts WITH cache clearing & then a restart WITHOUT cache clearing did the trick. Then one may need to try various OH restarts until things resume working.
How to clear the OH cache: Stop openHAB. Then in openHAB's \userdata folder, delete the \cache & \tmp folders & restart openHAB. That causes openHAB to start fresh with the current configuration.
Next: Email OpenHAB States on Demand
|
|
|
Post by papa on Nov 9, 2020 17:00:16 GMT
Email OpenHAB States on DemandGenerally I prefer to get a few emailed summaries of key states during a 24 hour period. However, I realized I also want the ability to send the states on demand. Here's how. In an . items file, add: Switch emailStates "Email States" -------------------------------------
In a .sitemap file, add: Switch item=emailStates
OR
In HABPanel, add a new switch widget that uses the Switch item emailStates
Continues ...
|
|
|
Post by papa on Nov 9, 2020 17:29:22 GMT
OpenHAB Rules to
Send Multiple State Updates in One Email Now Including On Demand
email-Key-States-pub2.rules (2.08 KB) << Download this .rules file (updated from this post) to openHAB's /conf/rules folder. Edit the file so the Item references match your nodes. Each of the file's first two rules timestamp a node item's change of state. Into the variable "eUpDate" the last rule compiles the timestamps & a brief label for each timestamp. Using a Mail Binding installation (see above), the rule emails eUpDate. Using the model of the first two rules, you can create more rules to timestamp other state changes. I added the rule "avoid NULL email switch" to ensure the emailStates item is initialized.
In the last rule, follow the pattern in the eUpDate compilation to add reports of more state updates.
The last rule's "when" statements determine when the rule triggers. I added a new when statement (line 38) which triggers when the Item emailStates changed to ON. (Line 43, postUpdate( emailStates, OFF) turns off the switch so the on demand emailing also turns off.)
As is, the time cron statement (line 40) causes the rule to trigger "at second :07, at minute :05, every 3 hours starting at 00am, every month between April and October." For now, it fires basically every 3 hours (8 times per day). I avoid the exact beginning of a 3-hour period because other rules might trigger then. Later, I may schedule one or two updates per day.
Now when one clicks the emailStates virtual switch on a browser sitemap or in HABPanel, the email summary should be sent on demand as well as when the time cron statement dictates.
|
|
|
Post by papa on Nov 9, 2020 21:36:23 GMT
Update
I just updated to openHAB 2.5.10.
The first time I started OH 2.5.10 (cache would be empty in the new install), sending emails to myself did not work.
Then I stopped & restarted OH 2.5.10 (once) WITHOUT clearing the cache & sending emails to myself was working.
So if sending emails is not working, perhaps we can fix it by restarting OH with or without clearing the cache.
The ON DEMAND feature added above allows us (conveniently) to test the emailing.
I'll watch what happens when the OH PC next restarts due to a Windows update (OH restart without clearing the cache).
|
|