|
Post by papa on Aug 22, 2021 18:27:29 GMT
Introduction & Preliminaries 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 helpful & reassuring when I'm away from home. I will document this approach for Gmail. Below computourist & I help document using the sendBroadcast / sendNotification method. This documentation assumes openHAB 3.x & a MQTT Broker are installed on a computer connected (by Ethernet cable) to your local network. For help with openHAB 3 & Mosquitto MQTT Broker, see this thread. This documentation also assumes you've built a working computourist-style RFM69 gateway & at least one RFM69 end node. See this thread for help with building that. This method will also report data from a working ESP WiFi node. See this board of threads for help creating those nodes. Next: Tweak a Gmail Account Setting
|
|
|
Post by papa on Aug 22, 2021 18:52:34 GMT
Tweak a Gmail Account Setting For current reality, in the following, I've adjusted what demondreamer recommended at the top of this post: (While logged in to your Gmail account) Go into your Gmail [account] Security settings tab (https://myaccount.google.com/u/0/security
At the bottom, click "Less secure app access".
Then click "Turn on access (not recommended [by Gmail])" Then click ON.
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 feature, I've not had a problem so far.
Next: Add to OpenHAB 3's Configurations (Mail Binding)
|
|
|
Post by papa on Aug 22, 2021 19:05:22 GMT
Add to OpenHAB 3's Configurations: Install the Mail Binding
Go to openHAB's localhost:8080/settings/addons/binding
To Add Mail Binding, click the blue plus icon disk at the bottom. Then click [Install]
Next: Create a Thing for the Mail Binding
|
|
|
Post by papa on Aug 22, 2021 19:15:28 GMT
Create a (Gmail) Thing for the Mail Binding
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 localhost:8080/settings/things/
To add a Thing, click the Blue plus disc at the bottom. In the next window, click on Mail Binding.
In next window, click on SMTP SERVER, "Used for sending emails via rule actions" (Not IMAP) In the fields, enter the parameters: Leave the default name or rename (I used Gmail SMTP.) You can leave the assigned SMTP Thing UID 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 Advanced" Server Port: 465 Do NOT use server port 587 that is sometimes recommended. Use port 465.
To Save, click the "Create Thing" at the bottom.
=============================== The above works but the logs may warn (does not seem to hurt): "javax.mail - expected resource not found: /META-INF/javamail.default.address.map"
Next: An Example Item Whose Data Will be Emailed
|
|
|
Post by papa on Aug 23, 2021 18:47:11 GMT
An Example Item Whose Data Will be Emailed
Again the following assumes a computourist style gateway which communicates with a DHT end node 05. That 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. See this post & this post. In an .items file, you also need an Item entry to subscribe openHAB to receiving the humidity data via the gateway/Thing/Channel). See about the Humidity Item in this post. If this current post is followed correctly the Node05 Humidity Item should automatically link to Node05Channel05. Next: An Example Rule to Email Node05 Humidity Data
|
|
|
Post by papa on Aug 23, 2021 18:56:56 GMT
An Example Rule to Email Node05 Humidity Data 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 example rule below: Your Gmail address & use the SMTP Server Thing ID you copied above OR Go to localhost:8080/settings/things. Under your SMTP Server Thing, see the needed code after "mail:smtp:"
Create, edit, & save the .rules file example (Linux /etc/openhab/rules/) :
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 papa on Aug 23, 2021 19:15:19 GMT
An Alternative for Sending Data On Jan 25, 2017, computourist wrote: 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 Next: Papa Updates Send Notifications
|
|
|
Post by papa on Aug 25, 2021 0:40:27 GMT
Next: Using Notifications AND Emails
|
|
|
Post by papa on Aug 25, 2021 1:18:27 GMT
Using Notifications AND EmailsMy Thanks to computourist, for the kudos & suggesting another way to do notifications. I believe a case could be made for using notifications AND emails. 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. In this thread, I document " Access OpenHAB 3 via myopenhab.org/(openHAB Cloud Connector) or Local Network"
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. A Notification message only reports on one item. One email can report on more than one Item state. (See next posts.)
Next: Send Multiple State Updates in One Email
|
|
|
Post by papa on Aug 25, 2021 17:58:00 GMT
Send Multiple State Updates in One Email Introduction
Previously, I 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 configurations found above in this thread. Next, OpenHAB Items Used to Send Multiple State Updates in One Email
|
|
|
Post by papa on Aug 25, 2021 18:00:26 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 Aug 25, 2021 18:16:36 GMT
OpenHAB Rules to Send Multiple State Updates in One Email email-Key-States-pub.rules (1.69 KB) << Download this .rules file to openHAB's rules folder (Linux apt get install: /etc/openhab/rules). 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 install (see above), the rule emails eUpDate. Using the model of the first two rules, you can create more rules to timestamp other state changes. Near the end of the last rule, replace 4331bde61f with your SMTP Thing UID. ( See this post.) 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 Aug 25, 2021 18:25:58 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. Next: Some SendMail Troubleshooting
|
|
|
Post by papa on Aug 26, 2021 15:05:21 GMT
Some SendMail 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, & "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 (Linux apt get install: /var/lib/openhab/), 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 Aug 26, 2021 15:19:51 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 Aug 26, 2021 15:32:23 GMT
OpenHAB Rules to
Send Multiple State Updates in One Email Now Including On Demand
email-Key-States-pub2.rules (2.13 KB) << Download this .rules file (updated from this post) to openHAB's /rules folder (Linux apt get install: /etc/openhab/rules). 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 32, 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 Aug 26, 2021 15:42:24 GMT
Update I recently updated from openHAB 2.5.10 on Windows 10 to OH 3 on Linux.
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 on Windows, perhaps we can fix it by restarting OH with or without clearing the cache.
On Linux OH 3, I added sending emails to myself after the initial install & it's worked fine so far.
I'll watch what happens when I next upgrade Linux OH. The ON DEMAND feature added above allows me (conveniently) to test the emailing & to get an immediate report of certain states.
|
|