By measuring our electricity consumption with a Raspberry Pi

Following documents curious things we can do with a Raspberry Pi, something very simple to achieve is for example to measure the power consumption of what we want, in this document we will see how to connect to a Raspberry Pi a non-invasive sensors and measuring current spending our home, since the electrical box to connect our house! Metrics will export a MySQL database and then Grafana create a panel where we see a very intuitive way and how much we spend!


Good, What do we need to continue to monitor or adding a touch of home automation to our home? Personally I found the most economical and interesting, It is to put a small HAT to Raspberry, I recommend the manufacturer LeChacal because it has different HAT that will meet whatever our needs, because to them we can connect them from 1 nonintrusive sensor to 8, we want it depends on the ETS, Light sensors accept increasingly more expensive, but I think the price is more than acceptable, go… And if we put more nonintrusive sensors assemble as many as we need HAT over one another! So:


I recommend reviewing this list of sensors each a wobbly which interests you:

The cheapest, for 3 sensors, nail 12 pounds: Remember when buying that output sea en formato CSV, and you will see it when you buy it, but take this into account!

Then we would acquire as many as we need non-intrusive sensors: SCT-013-000. We have them on the same site or on Amazon or eBay for about 4 € each.


Since the HAT connect the Raspberry Pi and sensors that have; in the clamps of the sensors will consider spending only one cable, eh! And nothing, with that we can know the consumption in Watts (W) It makes every 'thing’ we have nipped. We can measure electrical appliance cualquer, and I said, put this in the home electrical panel and measure consumption from there, if we have well-labeling, It will not cost us to measure consumption General or the lighting, consumption outlets, the fridge, oven, washing machine, Aquarius… I do not know!


With this command we can by connecting series ETS and see what spits. HAT see that depending on which you have acquired you can see the current, voltage or temperature, which it is what I spit this separate command commas.

stty -F /dev/ttyAMA0 raw speed 38400
cat /dev/ttyAMA0


Now, what will be treating outputs esos (a formato CSV), in my example if you look watt returns 7 non-intrusive sensors, each of them have a cable connected to my electrical panel, to see the code you will see that I measure currents (General, Plugs a stoves, the dishwasher and washing machine, lighting, Oven and Hob, and plugs. Then exported this info to a table that have previously created in MySQL, so We need to have MySQL installed on a machine (or install it on Pi itself) and then we create the database and table we want, I spend my example having a column for each sensor:

CREATE TABLE `corriente` (
`lavavajillas_lavadora` FLOAT NULL DEFAULT NULL,


And nothing, Now we only have this wonderful Python script that we will be filling Table 'current’ of our database! Create said script:

import serial
import urllib
import json
import MySQLdb

ser = serial.Serial('/dev/ttyAMA0', 38400)

response = ser.readline()
z = response.split(",")
if len(z)>=7:
 print "General: %s Watts" % z[0]
 print "Estufas: %s Watts" % z[1]
 print "Lavavajillas y Lavadora: %s Watts" % z[2]
 print "Alumbrado: %s Watts" % z[3]
 print "Horno y vitro: %s Watts" % z[4]
 print "Enchufes: %s Watts" % z[5]


db = MySQLdb.connect("localhost","root","xxxxxx","NOMBRE_BASE_DATOS")
cursor = db.cursor()

cursor.execute("""INSERT INTO corriente (general,estufas,lavavajillas_lavadora,alumbrado,horno_vitro,enchufes) VALUES (%s,%s,%s,%s,%s,%s) """, (general_valor,estufas_valor,lavavajillas_lavadora_valor,alumbrado_valor,horno_vitro_valor,enchufes_valor))


And ready! What remains is to schedule this script to run with the interval that interests us, run ‘crontab -e‘ and add the following to run for example every minute:

* * * * * python /home/pi/


It would the Ginde to finish? Well as usual, thanks to Grafana, We can of super simple and fast way to work any data, as in this example a MySQL table. If you have mounted reviewing Grafana this document, and after that, You can create a Dashboard where you'll add whatever you want. You step examples:

Graph where we add the metric and the paint, in this case the consumption outlets:

SELECT enchufes as value, "Enchufes" as metric, UNIX_TIMESTAMP(fecha) as time_sec FROM corriente  WHERE $__timeFilter(fecha) order by time_sec asc

Add graph where metrics lighting consumption:

SELECT alumbrado as value, "Alumbrado" as metric, UNIX_TIMESTAMP(fecha) as time_sec FROM corriente  WHERE $__timeFilter(fecha) order by time_sec asc

With the Singlestat plugin we can show, for example, Current expenditure General:

SELECT general FROM corriente order by fecha desc limit 1


I hope you have been helpful, be free to modify and personalizaroslo as necesitéis! As I'm saying in other posts, these ditties of'm using a mirror of those magical or MagicMirror, where I get a handsome morning and have some info, one day I'll tell you everything, But for now, little by little! If you've come this far, thanks for reading! and to all who share in social networks!


Latest posts by Hector Herrero (see all)