{"id":264,"date":"2016-03-12T09:41:21","date_gmt":"2016-03-12T14:41:21","guid":{"rendered":"http:\/\/lisa.rushworth.us\/?p=264"},"modified":"2016-03-12T10:08:51","modified_gmt":"2016-03-12T15:08:51","slug":"tracking-energy-usage-with-smartthings-and-aeonlabs-home-energy-monitors","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=264","title":{"rendered":"Tracking Electrical Usage With SmartThings and AeonLabs Home Energy Meters"},"content":{"rendered":"<p>When we started shopping for solar generation installations, how much electricity we can consume was a challenging question. We were replacing our HVAC system, so &#8220;look at the last 12 months of electric bills&#8221; wasn&#8217;t an approach that would yield valid data. What we needed was a way to see electrical consumption for the next two or three weeks once our new HVAC system was installed.<\/p>\n<p>We purchased several<a href=\"http:\/\/aeotec.com\/z-wave-home-energy-measure\" target=\"_blank\">\u00a0AeonLabs Home Energy Meters\u00a0(HEM)<\/a>\u00a0and have been using them to track our power consumption for almost six months now. The HEM&#8217;s are set up in SmartThings &amp; have a <a href=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2016\/03\/SmartApp-HEMLogger.txt\" rel=\"\">SmartApp-HEMLogger<\/a>\u00a0&#8220;SmartApp&#8221; attached to them that posts data to a MySQL table on our server via a web form (myURL needs to be &#8230; well, <em>your<\/em> URL).<\/p>\n<p>Install\u00a0a quick MySQL server (does not need to be Internet accessible) and a web server \/ programming language of your choice combination (*does* need to be Internet accessible &#8211; we are using Apache and PHP).<\/p>\n<p>Create the database and a table to hold your energy data:<\/p>\n<pre>mysql&gt; describe EnergyMonitors;\r\n+-----------+-------------+------+-----+-------------------+----------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+-----------+-------------+------+-----+-------------------+----------------+\r\n| keyID | int(11) | NO | PRI | NULL | auto_increment |\r\n| monitorID | varchar(50) | YES | | NULL | |\r\n| clampID | varchar(50) | YES | | NULL | |\r\n| eventTime | timestamp | NO | | CURRENT_TIMESTAMP | |\r\n| kwatts | double | YES | | NULL | |\r\n| kwhours | double | YES | | NULL | |\r\n+-----------+-------------+------+-----+-------------------+----------------+\r\n6 rows in set (0.00 sec)<\/pre>\n<p>Create an ID within your database that\u00a0has read\/write permission to this table. I create another ID that has read-only access (pages displaying data use this ID, the page to post data uses the read\/write ID).<\/p>\n<p>We also track temperature &#8212;\u00a0ideally, we&#8217;d be able to compare power consumption based on temperature *and* sunlight (we use a lot less power on a cold sunny day than I expect &#8230; just don&#8217;t know\u00a0<em>how<\/em> much less) &#8230; but I&#8217;m not there yet. Currently, the weather database only holds temperature:<\/p>\n<pre>mysql&gt; describe weather;\r\n+--------------+-----------+------+-----+-------------------+-----------------------------+\r\n| Field | Type | Null | Key | Default | Extra |\r\n+--------------+-----------+------+-----+-------------------+-----------------------------+\r\n| recordedTime | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |\r\n| temperature | int(11) | YES | | NULL | |\r\n| id | int(11) | NO | PRI | NULL | auto_increment |\r\n+--------------+-----------+------+-----+-------------------+-----------------------------+\r\n3 rows in set (0.01 sec)<\/pre>\n<p>Since we brought <a href=\"https:\/\/map.bloomsky.com\/weather-stations\/eaB1rJytnZSmm5y3\" target=\"_blank\">our Bloomsky <\/a>online, I use the Bloomsky API to record\u00a0temperature. Prior to that, I was parsing the XML from NWS&#8217;s closest reporting station&#8217;s &#8211; for Cleveland, that is\u00a0\u00a0<a href=\"http:\/\/w1.weather.gov\/xml\/current_obs\/KCLE.xml\" target=\"_blank\">http:\/\/w1.weather.gov\/xml\/current_obs\/KCLE.xml<\/a> &#8230; there&#8217;s\u00a0probably one near you. I grabbed both\u00a0observation_time_rfc822 and temp_f in case observations were not updated regularly\u00a0&#8211; the observation time was stored as the recordedTime. The temperature recording script is\u00a0in cron as an hourly task.<\/p>\n<p>You also need a small\u00a0web page where SmartThings can post\u00a0data\u00a0&#8212; in PHP, I have<\/p>\n<pre>&lt;?php\r\nif(!$_POST[\"monitorID\"] || !$_POST[\"clampID\"] ){\r\necho \"&lt;html&gt;&lt;body&gt;\\n\";\r\necho \"&lt;form action=\\\"\";\r\necho $_SERVER['PHP_SELF'];\r\necho \"\\\" method=\\\"post\\\"&gt;\\n\";\r\necho \"Monitor ID: &lt;input type=\\\"text\\\" name=\\\"monitorID\\\"&gt;&lt;br&gt;\\n\";\r\necho \"Clamp ID: &lt;input type=\\\"text\\\" name=\\\"clampID\\\"&gt;&lt;br&gt;\\n\";\r\necho \"KW Value: &lt;input type=\\\"text\\\" name=\\\"kwatts\\\"&gt;&lt;br&gt;\\n\";\r\necho \"KWH Value: &lt;input type=\\\"text\\\" name=\\\"kWHours\\\"&gt;&lt;br&gt;\\n\";\r\necho \"&lt;input type=\\\"submit\\\"&gt;\\n\";\r\necho \"&lt;\/form&gt;\\n\";\r\necho \"&lt;\/body&gt;&lt;\/html&gt;\\n\";\r\n}\r\nelse{\r\n$strMeter = $_POST[\"monitorID\"];\r\n$strClamp = $_POST[\"clampID\"];\r\n$strKWValue = $_POST[\"kwatts\"];\r\n$strKWHValue = $_POST[\"kWHours\"];\r\nprint \"&lt;pre&gt;Meter: $strMeter\\nClamp: $strClamp\\nKW: $strKWValue\\nKWH: $strKWHValue\\n&lt;\/pre&gt;\\n\";\r\n$servername = \"DatabaseHostname\";\r\n$username = \"MySQLUID\";\r\n$password = 'MySQLPassword';\r\n$dbname = \"HomeAutomation\";\r\n$conn = new mysqli($servername, $username, $password, $dbname);\r\nif ($conn-&gt;connect_error) {\r\ndie(\"Connection failed: \" . $conn-&gt;connect_error);\r\n}\r\nif($strKWHValue &amp;&amp; $$strKWValue){\r\n$sql = \"INSERT INTO EnergyMonitors (monitorID, clampID, kwatts, kwhours) VALUES ('$strMeter', '$strClamp', '$strKWValue', '$strKWHValue')\";\r\n}\r\nelseif($strKWHValue){\r\n$sql = \"INSERT INTO EnergyMonitors (monitorID, clampID, kwhours) VALUES ('$strMeter', '$strClamp', '$strKWHValue')\";\r\n}\r\nelse{\r\n$sql = \"INSERT INTO EnergyMonitors (monitorID, clampID, kwatts) VALUES ('$strMeter', '$strClamp', '$strKWValue')\";\r\n}\r\nif ($conn-&gt;query($sql) === TRUE) {\r\necho \"New record created successfully\";\r\n}\r\nelse {\r\necho \"Error: \" . $sql . \"&lt;br&gt;\" . $conn-&gt;error;\r\n}\r\n\r\n$conn-&gt;close();\r\n}\r\n?&gt;<\/pre>\n<p>DatabaseHostname, MySQLUID and MySQLPassword need to be yours too. Since posted data is meant to come from a trusted source (a source where I&#8217;ve supplied the URL) and in a known good format, these are quick INSERT statements\u00a0*not* the safest.<\/p>\n<p>Check in your database that you are getting data, then let it run for a few hours. Once you have a little bit of history, you can start viewing your energy usage. Link it into Excel\/Access using <a href=\"https:\/\/dev.mysql.com\/downloads\/connector\/odbc\/\" target=\"_blank\">MyODBC<\/a>\u00a0for ad-hoc reporting, write your own code to do exactly what you want, use a generic charting package capable of reading MySQL data &#8230;<\/p>\n<p>I am using <a href=\"http:\/\/www.pchart.net\/download\" target=\"_blank\">pChart<\/a> to display data &#8211; the\u00a0chart I use most frequently is the\u00a0stacked bar chart for kWH and a line chart for temperature.\u00a0Here is the PHP code which generates this\u00a0PNG:\u00a0<a href=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2016\/03\/energyUsage-StackedBarChart-Flat.txt\" rel=\"\">energyUsage-StackedBarChart-Flat<\/a>\u00a0&#8211; again,\u00a0DatabaseHostname, MySQLUID, and MySQLPassword need to be yours.<\/p>\n<p><a href=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp.png\" rel=\"attachment wp-att-265\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-265\" src=\"http:\/\/lisa.rushworth.us\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp-1024x538.png\" alt=\"StackedBarChart-KWHAndTemp\" width=\"620\" height=\"326\" srcset=\"https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp-1024x538.png 1024w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp-300x158.png 300w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp-768x403.png 768w, https:\/\/www.rushworth.us\/lisa\/wp-content\/uploads\/2016\/03\/StackedBarChart-KWHAndTemp.png 1200w\" sizes=\"auto, (max-width: 620px) 100vw, 620px\" \/><\/a><\/p>\n<p>We no longer have our heat pump, air handler, and heat strips being monitored &#8211; but\u00a0for periods where there\u00a0<em>is<\/em> data from the other sources, we had several segments to our energy usage report (&#8220;other&#8221; is the report from the HEM on our mains MINUS all of the other reporting segments).\u00a0You\u00a0can yank all of the non-mains\u00a0segments (or change them to be whatever sub-segments <em>you<\/em> are monitoring. The monitor ID comes from the HEM name in SmartThings &#8211; since those are user-configured, I have the names hard coded. You *could* hard code the Mains and then use &#8220;select distinct&#8221; to get a list of all the others and make the code more flexible.).<\/p>\n<p>Short term charts (past 24 hours or so) can render out real-time, but longer term views take a long time to load. Since we&#8217;re looking more for trends and totals &#8211; being\u00a0<em>up to the second<\/em> isn&#8217;t critical.\u00a0I&#8217;ve got cron tasks that generate out PNG files of the charts.<\/p>\n<p>Two enhancements for a nothing-doing rainy day &#8211; adding authentication to the HEM Logger (SmartThings posts from multiple\u00a0netblocks, so there isn&#8217;t a good way to IP source restrict access to\u00a0the post data page. Anyone with your URL\u00a0<em>could<\/em> post rogue energy usage info into your database &#8211; or more likely try to hack your servers.) and add lumen to the weather data \/ graph displays.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we started shopping for solar generation installations, how much electricity we can consume was a challenging question. We were replacing our HVAC system, so &#8220;look at the last 12 months of electric bills&#8221; wasn&#8217;t an approach that would yield valid data. What we needed was a way to see electrical consumption for the next &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,29],"tags":[35,34,36,32],"class_list":["post-264","post","type-post","status-publish","format-standard","hentry","category-coding","category-technology","tag-php","tag-smartthings","tag-solar","tag-technology"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/264","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=264"}],"version-history":[{"count":6,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions"}],"predecessor-version":[{"id":275,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/264\/revisions\/275"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=264"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=264"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=264"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}