Monitoring temperatures using FHEM, RRDTool, PHP & JavaScript

My Household Temperature Graphs page displays various statistics from my heating system. In this post, I explain how I produce these graphs using a combination of FHEM, PHP and RRDTool.

The heating system is based on a number of these Conrad FHT 80 BTFN sets. The programmer controls the actuator via 868Mhz RF and the signal is received by an FHT 1300 PC.

The RF received is interpreted by FHEM and it is this system that I query using my code. The following script is started on the FHEM server every five minutes using the linux Cron daemon. It logs the values into a Round Robin Database which I later pull the stats out of to graph.

After changing the working directory to the directory the script lives in (line 2), two additional PHP scripts are loaded. config.php and fhem.php (lines 4 and 5).

…and finally an XML configuration file is loaded (line 7):

Lines 9 to 19 instantiate all the PHP classes that will be used later in the script, load and parse the configuration file, obtains a list of FHT devices from the configuration file, locates where the Round Robin Databases will be stored then obtains the output from the FHEM XML interface. All of this is wrapped in a try/catch block in case any exceptions are thrown by the code which actually does all of this.

On line 21, we iterate over the array of heat devices and pull out the XML attribute “ref”…

We then search the XML returned from FHEM for the “ref” devices and pull out the values we are interested in monitoring. Unhelpfully, FHEM appends a string to the measured temperature so we need to process that and pull out the actual numerical value (by splitting it into an array and pulling out the first value)…

Next, the RRD file name is derived from the FHEM device name (ref) and is checked for readability (does it exist?)…

If it doesn’t exist, it is created by lines 32 thru 57. The PHP uses the module RRDs and the function calls closely resemble the RRDTool command line. I won’t go into details on how that works in this post suffice to say that the RRD created holds the measured temperature, the desired temperature (setpoint) and the actuator percentage. The RRD holds 600 five minute readings (just over two days worth), 700 30 minute readings (just over two weeks worth), 775 two-hourly readings (just over two months worth) and 797 daily readings (just over two years worth) and all of this in a file which is just 212Kb’s in size! The RRD Tool website, PHP manual and this tutorial are helpful in explaining more about how the RRD tool and PHP interface works.

Assuming the RRD file was found (or has now been created), updating it is simply a case of calling the rrd_update() function with the correctly constructed string (“N” means “NOW”)…

In part two of this post (coming soon), I will document the code which is used to pull the readings out of the RRD and present them as HTML5 graphs on my website here: http://www.cpmills.com/household-temperature-graphs/

About the Author

chrisChris works as an IT Security Specialist for a large FTSE 100 company. He is a geek at heart and loves nothing more than trying to automate his home. The problem is, his wife simply doesn't get it and can't understand why they can't have 'normal' lights like everyone else! Chris is dedicated therefore to implementing automation in a family friendly way.View all posts by chris →

Leave a Reply