{"id":227,"date":"2019-08-28T00:59:38","date_gmt":"2019-08-27T22:59:38","guid":{"rendered":"https:\/\/petr.maxbox.cz\/?p=227"},"modified":"2019-10-12T03:19:44","modified_gmt":"2019-10-12T01:19:44","slug":"monitoring-power-consumption-kwh-using-node-red-and-influxdb","status":"publish","type":"post","link":"https:\/\/petr.maxbox.cz\/index.php\/2019\/08\/28\/monitoring-power-consumption-kwh-using-node-red-and-influxdb\/","title":{"rendered":"Power consumption (kWh) monitoring using node-red and influxDB"},"content":{"rendered":"\n<p><a href=\"https:\/\/www.maxbox.cz\/nextcloud\/index.php\/s\/bc7mgDi4xRDRJkE\">I have got nice 3phase 30(100)A four wire RS485 MODBUS Din Rail watt meter from Aliexpress (US $35.76) <\/a>(max. baud rate is possible to set to 9600). I am able to read values using USB-RS485 adapter + Raspberry pi and sending data (json) to the mqtt server. So far no problem. Power meter is providing cummulated  value (the same value you can read on the display).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"799\" src=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/08\/powermeter.png\" alt=\"\" class=\"wp-image-228\" srcset=\"https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/08\/powermeter.png 1000w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/08\/powermeter-300x240.png 300w, https:\/\/petr.maxbox.cz\/wp-content\/uploads\/2019\/08\/powermeter-768x614.png 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n\n\n\n<p>At the first beginning I was sending the data in to the sqlite database using sqlite node-red node. The table was very simple:<br> (ID, TIMESTAMP, TOTAL_KWH).<\/p>\n\n\n\n<p>The issue starts with the simple question: How to get daily (hourly, weekly, monthly&#8230;  ) power consumption? Something like:<br>| 22.7.2019  | 450kWh|<br>| 23.7.2019  | 320kWh|<br>| 24.7.2019  | 300kWh|<br>At the first sight it looks as an easy task, but I ended up with this quite complex <a href=\"https:\/\/petr.maxbox.cz\/index.php\/2019\/08\/08\/sqlite-select-for-power-consumption\/?preview=true\">query<\/a>.  And as data was growing, the execution of the SELECT was slowing down.<\/p>\n\n\n\n<p>The guys from the nice <a href=\"https:\/\/joysfera.blogspot.com\/2017\/11\/arduino-elektromer-plynomer-vodomer.html\">JOYSFERA<\/a> server  were pointing me to the influxDB. Installation was an easy part thanks to the:<br><a href=\"https:\/\/www.osradar.com\/install-influxdb-ubuntu-18-04-debian-9\/\">https:\/\/www.osradar.com\/install-influxdb-ubuntu-18-04-debian-9\/<\/a><\/p>\n\n\n\n<p>To import existing data in to the influxDB <a href=\"https:\/\/docs.influxdata.com\/influxdb\/v1.7\/tools\/shell\/#import-data-from-a-file-with-import\">you  need a text file with the following structure:<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># DDL\nCREATE DATABASE mrazirna\n\n# DML\n# CONTEXT-DATABASE: mrazirna\n# CONTEXT-RETENTION-POLICY:autogen\n\npower value=0.0 1564391161\npower value=270.8 1564583065\npower value=272.1 1564583462\npower value=272.1 1564583542\npower value=272.1 1564583621\npower value=272.2 1564583701<\/pre>\n\n\n\n<p>While preparing the file, note the line ending has to be linux style and if you are importing big data-set, the error messages are displayed on the top of the import screen &#8211; you  need to scroll up (which was kind of difficult to me to discover).<br>Now you are ready to import data in to the database:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">> influx -import -path=\/path\/to\/the\/data\/file -precision=s\n2019\/08\/27 22:53:04 Processed 1 commands\n2019\/08\/27 22:53:04 Processed 26934 inserts\n2019\/08\/27 22:53:04 Failed 0 inserts<\/pre>\n\n\n\n<p>Next connect the node-red node to the influx database: <br>https:\/\/diyprojects.io\/node-red-tutorial-saving-mysensors-measurements-on-influxdb\/#.XWWvovxS9B8<\/p>\n\n\n\n<p>When you  are preparing data for the influxDB, remember you need to send the same data-type in  to the database. In my case the value datatype was float<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">> show field keys\nname: power\nfieldKey fieldType\n-------- ---------\nvalue    float<\/pre>\n\n\n\n<p>It was necessary to writhe following (note *1.0 at the end of the first row) in to the node-red function node (otherwise the string was send to the influxDB)<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">msg.payload = msg.payload.POWER.toFixed(1)*1.0;\nreturn msg;<\/pre>\n\n\n\n<p>The best part is the simplicity of extracting data:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">> root@maxbox:\/data\/new# influx -precision rfc3339\nConnected to http:\/\/localhost:8086 version 1.7.7\nInfluxDB shell version: 1.7.7\n\n> use mrazirna\nUsing database mrazirna\n\n> select derivative(max(value)) from \"power\" where time > now()-7d group by time(1d) fill(0) tz('Europe\/Prague')\nname: power\ntime                      kWh\n----                      ---\n2019-08-21T00:00:00+02:00 200.19999999999982\n2019-08-22T00:00:00+02:00 199.5\n2019-08-23T00:00:00+02:00 202.10000000000036\n2019-08-24T00:00:00+02:00 111.80000000000018\n2019-08-25T00:00:00+02:00 309.89999999999964\n2019-08-26T00:00:00+02:00 222.5\n2019-08-27T00:00:00+02:00 278\n2019-08-28T00:00:00+02:00 6.5\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have got nice 3phase 30(100)A four wire RS485 MODBUS Din Rail watt meter from Aliexpress (US $35.76) (max. baud rate is possible to set to 9600). I am able to read values using USB-RS485 adapter + Raspberry pi and sending data (json) to the mqtt server. So far no problem. Power meter is providing<\/p>\n<p class=\"text-right\"><span class=\"screen-reader-text\">Continue Reading&#8230; Power consumption (kWh) monitoring using node-red and influxDB<\/span><a class=\"btn btn-secondary continue-reading\" href=\"https:\/\/petr.maxbox.cz\/index.php\/2019\/08\/28\/monitoring-power-consumption-kwh-using-node-red-and-influxdb\/\">Continue Reading&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-227","post","type-post","status-publish","format-standard","hentry","category-nezarazene"],"_links":{"self":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/comments?post=227"}],"version-history":[{"count":12,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":283,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/posts\/227\/revisions\/283"}],"wp:attachment":[{"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/media?parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/categories?post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/petr.maxbox.cz\/index.php\/wp-json\/wp\/v2\/tags?post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}