Remote Logging and Email Notification | |
Syslog Server on Raspbian and Remote Logging of TASMOTA Messages |
Perhaps undeservedly, the Orange Pi Zero has the reputation of running hot. I wanted to use the Domoticz data logging facilities to record the core temperature of the single board computer which is on all the time running HA-Bridge. And while setting that up, I decided that I should also log any errors occurring during the execution of the script that updates the temperature data on a regular basis and look into sending some sort of notification if the Orange Pi Zero seems to be getting too hot.
Table of contents
- Reading the CPU Temperature
- Improving the Script
- Logging Errors to
syslog
- Notification Using Domoticz
- Notification with
DzVents
in Domoticz - Temperature Alert in Domoticz
- Back to Python
- Conclusion
Reading the CPU Temperature
The first step is to create a virtual temperature sensor in Domoticz. It is not complicated at all and I have covered
this in detail before: Installing a Temperature Sensor.... Just remember to
select a temperature-only sensor, Temperature
, and not a
temperature and humidity sensor, Temp+Hum
, as explained in step
2 of To add a virtual sensor
.
Here is a simple Python 3 script that reads the CPU temperature
from the sys/class
thermal zone file and sends it to
Domoticz using a properly JSON formatted
HTTP request. This is a slightly modified version of the script
used to read and report the CPU temperature of the Raspberry Pi, updated
to run with version 3 of Python.
Here is the downloadable version of the script: opiz_temps.py.
#!/usr/bin/python
. Also because of changes to
urllib
, replace urllib.request
with
urllib2
in the import urlopen
line. It is
also possible to write from urllib import urlopen
but the original urlopen
in the original urllib
module does not have a timeout parameter that will be used later.
If you want a version of the script that works with both versions of
Python then import and use the sys
module to test for the
version of Python executing the script and import urlopen
from
the appropriate module.
Thank you to a reader who wishes to remain anonymous for providing this useful snippet.
To complete the setup, the script should be placed in a specific
directory, made executable and be executed regularly by the cron
daemon. Here is one way of going about this. I am assuming the script will be place a
directory named bin
in the home directory.
Then edit the script, setting the proper Domoticiz index number of the virtual temperature sensor and the
IP address of its web sever at the start of the script file. You may want to
change the messages shown on the console if verbose
is defined
as greater than 0
. After that save the changes and continue.
Add a line in the schedule of tasks so that the script is executed every five minutes.
Of course, zero
must be replaced by the correct user name,
and by the same token, the full path to temps.py
should be
corrected if it was not copied to the bin
directory.
It should not be necessary to reload or restart cron
. But
if you want to be safe, here is one way of doing it.
Within 5 minutes, the temperature sensor in Domoticz should be displaying the CPU temperature as well as the time the value was read.
This script has been working very well since June 2017 on the Raspberry Pi and it has worked on the Orange Pi Zero for a few days as can be seen from the Domoticz temperature chart.
The obvious dips are easily explained. Very late on the 3rd of March, the Orange Pi Zero was shutdown for a while as I moved it to a new location. On March 5th, I replaced a dimmer with a "smart dimmer" and turned the power off twice in the process.
To be frank, this is probably well enough as it is and I see no reason why the script would not work in the future. Nevertheless, I decided to try "improving" it and in the process gain more experience with Python which I obviously need.
Improving the Script
What could go wrong with that first script? Here is a list of things that I can envision.
- The wrong Domoticz index is specified.
- The IP address is incorrect.
- The JSON query is wrong.
- The temperature file is incorrectly named.
- The temperature file could not be read.
- Something goes horribly wrong and the URL request blocks.
The last problem can be mitigated to a certain extent. A timeout value
can be inserted into the call to urlopen
. If the request is not
handled within that interval, an exception will be raised. This is also what
happens if the IP address is wrong or if the Domoticz
web server is down. When such an exception occurs, it would be useful to
report the situation. And exactly the same thing should be done for the other
errors. As I would typically do in a Pascal program in this situation, I
added an exception handler.
Here is the downloadable version of the script: opiz_temps2.py.
It may be necessary to modify the interval before a timeout occurs. The usual delay between sending the HTTP request and getting a response from Domoticz on my network was about 5 seconds. Doubling that time seemed like a reasonable timeout delay.
Catching all exceptions in this manner is considered bad form, but I hope
to justify this later on. As it is, the exception handler does not do much.
It swallows the exception and does nothing unless console messages have been
turned on by setting verbose
to a value other than 0. In that
case it just writes the exception to the console. I assume that this
translates to printing error messages to stdout
instead of
stderr
.
There is an obvious problem. All error messages will be lost even if
verbose=1
because in actual use, the script will be run as a
background task without being connected to a console. The obvious solution is
to use the system log.
Logging Errors to syslog
To send messages to the system log, I opted for the syslog
Python module, but some might prefer a pure
Python library, in which case see logging.handles.
Here is the downloadable version of the script: opiz_temps3.py.
The openlog()
function call is not necessary. What the
ident
option does is to assign a specific program name for log
messages that will be sent by the script instead of using the default name. I
explained the interest of assigning such a name in a previous post: Remote Logging and Email
Notification. System log messages are sent along with a severity level.
There are 8 levels, defined as follows in the Python module, starting with
the highest severity.
The setlogmask(LOG_UPTO(LOG_ERR))
means that only messages
with a severity level equal to LOG_ERR
or higher (i.e.
LOG_EMERG
, LOG_ALERT
, LOG_CRIT
or
LOG_ERR
) will be actually be sent. One would want to write
LOG_DOWNTO(LOG_ERR)
, but UPTO
does make sense
because the values assigned to the symbols actually start at 0 for
LOG_EMERG
and increase up to 7 for LOG_EMERG
. By
default, all messages are sent.
Advantage has been taken of the different severity levels that are
available. Some of the information shown on the console when
verbose
was set is now sent to syslog
with the
lowest severity level, LOG_DEBUG
, to help in debugging the script.
And when the temperature of the CPU exceeds the specified threshold,
alertTemp
, then a message with severity LOG_ALERT
is
sent to syslog
.
Note how sending logging messages and displaying them on the console
has been centralized in a single function: log
. This makes
for cleaner code.
To test out the logging function, set the loglevel
to LOG_DEBUG
in the script, open a second session
on the Orange Pi Zero to display the content of the user log as new
messages are received, and then execute the script.
Restore the loglevel
to LOG_ERR
and mangle
the Domoticz temperature sensor idx or the
IP address to see the results when running the script. Try lowering
the value of alertTemp
also.
Notification Using Domoticz
To be honest, I am not a big reader of log files, even if they should be centralized as I have done (again see: Remote Logging and Email Notification). Nor do I look at the Domoticz web interface on a regular basis. On the other hand, the latter provides a very simple mechanism to send a notification when the temperature reaches a certain level. Click on the button on the virtual temperature sensor in the Domoticz web interface. Below is a part of the page that will appear as a consequence.
The conditions for sending out a notification have already been entered: Domoticz will send, enter the desired message in the field. Once the options have been entered, click on the button at the bottom.
set to and set to . Whenever the CPU temperature goes beyond the set threshold (which is independent of the one used in the Python script reading and reporting the Orange Pi Zero core temperature), notification is sent to all the enabled notification systems. The notification will be sent once when the threshold temperature has been crossed. By default it will not be sent again (for a long while at least) even if the Orange Pi Zero continues to update the temperature. If you want to receive a notification each time the temperature is updated and remains too hot, slide the option to the right until it goes green. If you want a different notification than whatWhile there are numerous notification systems, I only have experience with email notification. It requires that the Email tab displayed after clicking on Setup top menu bar and then on the Settings menu choice. Presumably, other notification systems can be enabled on the Notifications tab in a similar manner.
be correctly filled out. It can be found in theClick on the Apply Settings button.
button to send a message to the specified destination and once everything works, do not forget to press on theThe email notification will not be sent unless both the
and the options are enabled. That is a bit of a problem for me because I prefer to disable email notification unless the house will be empty for more than a couple of hours.The default notification message sent by Domoticz is terse but to the point:
HA-Bridge Temperature is 72.0 degrees [>= 63.0 degrees]
Both the subject and body of the email will be the same. That will also be true if you entered a custom message.
Notification with DzVents
in Domoticz
Up to now, I have not looked at the events model used in Domoticz except for writing a couple of scripts long ago to handle some legacy hardware. It was pleasing to find it that DzVents version 2.4 included in version 4.9700 of Domoticz made it very easy to send an email message formated as I wanted.
Here is the downloadable version of the script: opiztemp.lua.
I saved the script as
~/domoticz/scripts/dzVents/scripts/opiztemp.lua
where, as usual,
~/
denotes the default user's home directory on the Raspberry Pi
hosting the home automation server. The script file could have any name as
long as it ends with the .lua
extension. The email account has
to be correctly defined in the Domoticz
Setup/Settings/Email
tab as shown above. Alert emails will be
sent even if the option is not
enabled. However, email must be enabled in .
If you wanted the alert message pushed to all notification engines
enabled in the Domoticz
Setup/Settings/Notifications
tab then replace the
domoticz.email...
line with
In that case, if Send Email Notification
is
enabled then an email will be sent to the address given i the
To:
field of the Email tab. Actually
that is the default behaviour, it depends on the inclusion of email in the
list of along with other
notifications systems to use in the sensor definition.
This is a very simple-minded script and it will work. But an alert message will be sent every five minutes as long as the Orange Pi Zero is running too hot. It looks like it would be simple in the "new" DzVents to save the time at which the alert is sent out and to skip sending messages until a decent interval has elapsed. I leave that for a future exercise if I find it necessary.
Temperature Alert in Domoticz
Domoticz provides alert sensors as another way of signalling that the CPU is running too hot. Alert sensors are meant to imitate road hazard warning signs and their colour can be adjusted to correspond to the severity of the problem. A red alert sensor will stand out especially if it is placed on the dashboard which is first displayed when the standard Domoticz web page is opened.
An alert sensor is a virtual device which is easy to create in Domoticz is simple enough. Follow the instructions found in
a previous post selecting Alert
for the
virtual device type. A virtual alert should now appear in the Utility tab.
If that tab is not visible, go to the bottom of the Setup/Setting/ page and enable the Utility tab and then press the Apply Settings button at the top of the page.
The alert sensor has a text which can be modified and it can be one of five colours which should be related to the severity of the alert.
colour | nvalue | Domoticz object constant |
---|---|---|
grey | 0 | dz.ALERTLEVEL_GREY |
green | 1 | dz.ALERTLEVEL_GREEN |
yellow | 2 | dz.ALERTLEVEL_YELLOW |
orange | 3 | dz.ALERTLEVEL_ORANGE |
red | 4 | dz.ALERTLEVEL_RED |
In dzVents
Lua scripts, replace dz.
by the
actual name given to the domoticz object. It is not too complicated to modify
the above Lua
script to update the alert sensor when the
temperature sensor is itself updated
Here is the downloadable version of the script: cputemp.lua.
Back to Python
My initial approach was not to use the Domoticz notification system but to do everything in Python on the Orange Pi Zero and only update sensors in the home automation system. For the sake of completeness, here is the script.
This only works in Python 3.x as it is, because of differences in the
smtplib
module. Here is the source code:
cputemp.
Conclusion
While the above has shown only one way of using the Domoticz data logging capability, there are numerous methods to raise the alert when the temperature goes beyond a certain level. I have shown three.
- A
syslog
message with the appropriate priority level. - An alert sensor in the Domoticz dashboard.
- Notification via email or another system.
The syslog
message will normally be handled by the script
that does the actual reading of the system temperature on the Orange Pi Zero.
But it could be handled by a script running on the Domoticz, although I have not shown that. The email notification
could be done by the rsyslog
daemon on either machine in
response to the alert message as shown in the previous post, it can be done
by the same Python script that reads the system temperature on the Orange Pi
Zero, or it can be done within Domoticz. In the latter
case, the built-in notification system can be used or a dzVents
script (or maybe a Python script) can be used to send the notification.
With all these possibilities, I have chosen the solution shown in the last section which means that the script responsible for reading the Orange Pi Zero core temperature also handles the details of deciding when an alert should be sent and what colour the Domoticz alert sensor should display. To me this makes sense because this is very much related to one device: the SOC on the board. And as outlined above, the Orange Pi Zero is not doing much work and yet with its four cores it is in principle much more powerful than the Raspberry Pi hosting the home automation system.
There are arguments for unloading as much work onto the Raspberry Pi as possible. Presumably, if the Orange Pi Zero is too hot, then it might not be the best strategy to have it take on the further responsibilities of sending an email and so on. Furthermore, email messages is just one way of notifying the user of a problem and this seems to be very much in the purview of a home automation system.
Perhaps time will tell which is the best approach.