Calculating kWh from instantaneous W

@crightonsetchfield what hardware are you using? I would like to measure each of the circuits in my house, but havn’t found a reasonable solution to do so yet. Your proposed solution using MQTT and openhab is exactly what I want, I just need the hardware now…

Well, @joe_barneson i made my setup with arduino’s and sct-013-100, (with the help of the library from , are you into hardware/making? or more looking for something off the shelf?

I basically made a really simplified version of the opensource device that they sell.

I have used as a way of wirelesly getting my data from arduino to ethernet using my own modified sketch, and then mqtt from there on to openhab. I would like to skip a few steps and use a ESP8266 to replace all of the stuff in the middle but haven’t really got my head round whats needed to get that to replace the arduino.

At the moment i’m only measuring 1 out of 2 main circuits in my house, but i would like to change that when i get the above working reliably.

1 Like

I’m using a Brultech GEM which has 32 channels + Temperature inputs. It’s not cheap but I’m using it, along with it’s baby brother (a Brultech ECM-1240) to measure every circuit in the house (incl Solar), as well as temperature in the Garage.

It’s sending data to (SEG), for graphing/tuning, and to (for solar) via a Raspberry Pi that’s running I’m not yet pushing into openHAB, which I did previously with my Vera, but the URL form of the data is easy to parse out… When I get spare cycles I plan to MQTT enable so the data can flow to everywhere.

It’s a very reliable box, and provides a lot of the kWh & instantaneous W information directly.

an arduino / raspberry pi solution with the sct-013-100 would be perfect IMO. I looked at openenergymonitor, but it seemed to overcomplicate what I really wanted… Using their sensor, MQTT, and a set of OH rules would be perfect.

But at this point you don’t have a simple solution you’d recommend yet?

well to be clear, there are several different levels of oepnenergymonitor hardware, what i have is based on this: its the most basic version they provide instructions for, from there it goes upwards with adding transformers to factor in the accurate voltage to get a more accurate reading, and then you can step up to one of their designed boards, but i am really only looking for a rough comparison, i dont care so much about the accuracy, i just want to see if i am using a lot or a little.

I have seen the other options counting pulses from power meter, but i have a long driveway, so it’s not so easy to implement, and other devices like this,201444_5,201409_1 which also look interesting, it has a lcd counter, but also provides a pulse output that can be measure quite easily i imagine…

Hope this is useful

WOW! I love it, I am building a new house and have two 200 amp sub panels two Brultech GEMs would work great!, any thoughts on writing a binding for Brultech GEM? I would be willing to help fund the work.

You can check out Curb on Indigogo, it will do 18 circuits, but I think I like what @guessed is using better.

If only the things like Smappee would have a full API… It looks like they are getting started on it: Smappee API

A couple nice things about this type of solution: 1) # of circuits does not matter 2) per device not per circuit power metrics

I’m using arduino with this shield: and SCT-013-000 sensors
I’m planning to modify a little bit this shield because it don’t allow me to measure real power (no measuring of voltage)

Hi Gambituk,

Are your rules working well now? I’m asking on MySensors for this solution and by now nobody answered me.
Can you please share your OH settings (rules, items, sitemap)? I’m trying to do the same as you do: reading total kwh and kwh/day from instant watt read by a SCT-013-00. Not using yet a AC/AC transformer 220/9V.
What is the last Arduino sketch that you are using? is that posted on MySensors?

Thanks in advance for any answer!

Hi. I will try to post it later when I return home. If I dont reply, please
remind me on here


Thanks man, looking forward for your answer.

Mysensors sketch (i think this is the one i am using)

Ok, so that should be the sketch i am using,

so as for the rules :-

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

var long LastUpdate = 0 //// <- At the top of the file
rule “Energy consumption calculation”
Item Node_garage1 received update
var long currentTime = now.millis

if (LastUpdate != 0) {

var long timeElapsed = currentTime - LastUpdate

if (timeElapsed > 0) {
var Number power = Node_garage1.state as DecimalType
var Number energyConsumption = (power * timeElapsed) / 3600000 / 1000 // kWh
postUpdate(CumulativeEnergyConsumption, CumulativeEnergyConsumption.state as DecimalType + energyConsumption) //increment
postUpdate(DailyEnergyConsumption, DailyEnergyConsumption.state as DecimalType + energyConsumption)

LastUpdate = currentTime
rule “Clear daily consumption”
Time cron “0 0 0 * * ?” // every day
postUpdate(DailyEnergyConsumption, 0)
rule “Energy in past hour”
Time cron “0 0 0/1 1/1 * ? *” // every hour
postUpdate(HoursEnergyConsumption, (CumulativeEnergyConsumption.state as DecimalType - CumulativeEnergyConsumption.historicState(now.minusHours(1)).state as DecimalType))


// //////////////////-------------ENERGY AND POWER input----------\\\\\\\\\
Number Node_garage1 “Current Energy [%d W]” (Power_Chart) { mqtt="<[mysensor:MyMQTT/22/0/V_WATT:state:default]" }

Number CumulativeEnergyConsumption “total energy [%.2f kWh]” { mqtt=">[mysensor:MyMQTT/55/0/info:state:*:default]" }
Number DailyEnergyConsumption “todays energy [%.8f kWh]”
Number HoursEnergyConsumption “hour energy [%.8f kWh]” (Power_Chart2)

Sitemap (or just the relavant part of it.)
Frame {
Group item=Node_garage1 { //Current Energy/////
Text item=Node_garage1
Text item=DailyEnergyConsumption
Text item=CumulativeEnergyConsumption
Text item=HoursEnergyConsumption
Chart item=Power_Chart2 period=3D refresh=30000
Chart item=Power_Chart period=2h refresh=30000
Chart item=Power_Chart period=4h refresh=30000
Chart item=Power_Chart period=3D refresh=30000

Hope this makes sense.

1 Like

Thank you very much for the shared files.
I would like to ask you if these files are working for you because i have a different case, I’m using the serial gateway between my pc and arduino gateway and i modified the items because i saw you use mqtt replacing this:

Number	Node_garage1	"Current Energy [%d W]"	(Power_Chart)	{ mqtt="<[mysensor:MyMQTT/22/0/V_WATT:state:default]" }

with this:

Number	Node_garage1	"Current Energy [%d W]"	(Power_Chart)	{ serial="COM4@115200" }

and this:

Number	CumulativeEnergyConsumption	"total energy [%.2f kWh]" { mqtt=">[mysensor:MyMQTT/55/0/info:state:*:default]" }

with this:

Number	CumulativeEnergyConsumption	"total energy [%.2f kWh]" { serial="COM4@115200" }

For the gateway i’m using this sketch from MySensors forum [Serial gateway communication][1]
The connection made on the breadboard are like this [OpenEnergyMonitor][2] except that i have connected only the CT sensor without the transformer.
On local host i can load the sitemap but it is not displayin any value for the watts, kwh or anything.

In the same time in the node point serial monitor of my arduino mega i get this:

send: 22-22-0-0 s=0,c=1,t=17,pt=2,l=2,st=ok:2768

and its refreshing with different values like 2580, 2580, 2721 even the sensor is connected or not on the live wire or if i turn on/off my lamp.

Did you make any calibration for your CT sensor? how many volts do you have on the wall socket? I have 220V.

I dont know where to check or what to check! Could you please help me?

Thanks a lot for all the help you gave me.

UPDATE: you were right about the connection, they were wrong and so i was getting that strange value. Actually the pin of the CT was connected in the wrong Arduino pin.
Now with some small modifications i read on the serial monitor 42 watts (i think!) and if i turn off the lamp i get 17! if you dont connect the CT sensor to the alive wire what do you read on serial monitor for the watts?

I never really used the serial setup, i was using mqtt gateway from the start, so i don’t really know about adapting from my mqtt to the serial setup you are using, but i can tell you that you can probably ignore the output on the CumulativeEnergyConsumption

Number CumulativeEnergyConsumption “total energy [%.2f kWh]”

and just leave it like this, i was using that mqtt reference just to feedback data to my mqtt broker to help me to see what was happening for troubleshooting.

i don;t have voltage measurement, i am happy enough with a broad approximation, and i did a rough calibration with an electric heater and also a cheap “owl energy meter”

as for the values, it sounds like the wiring isn’t correct somewhere

i was basing my circuit off this link rather than the with voltage, but i don’t know if its really any different.

I think it’s a little more complicate using Mqtt because you have to have the broker and to change things in the OH config so I choose the serial gateway.
Finally it’s important to have the right addon and it has to work.

As far as I can see I have two problems:

  1. The node point reads some wrong values from the CT sensor
  2. The node point is not sending the values on the server so that’s why I can’t see them in the OH web page.

Strange thing is that using the emonlib example in arduino I get different values turning the lamp on or off butt still isn’t ok because even the lamp is off I get some small values for the watts!
I asked you how exactly did u calibrate the CT because to calibrate I read you need to make 14 turns with the alive wire on the sensor. On the other hand, I think there are some values that needs to be changed inside the arduino sketch depending on how many volts you have on the wall socket.
I don’t know where to start :frowning: but I think it’s more important to see that OH web is displaying the values even they are wrong.
Thanks anyway for your replay.

Excuse me, i’m trying to use your script (oh 1.8.1) but gives me error, can you check your actual includes? what oH version are you using ? error is :

Error during the execution of rule ‘Energy consumption calculation’: Cannot cast org.openhab.core.types.UnDefType to org.openhab.core.library.types.DecimalType
10:33:32.647 [ERROR] [o.o.c.s.ScriptExecutionThread :50 ] - Error during the execution of rule ‘Energy consumption calculation’: Could not invoke method: org.eclipse.xtext.xbase.lib.LongExtensions.operator_minus(long,byte) on instance: null


Hello. I think you need to initialise your values… I just run 1 time a
rule to set the items used to = 0 (zero) then disable rule. And I think it
should work. Let me know if not and I will check when I get home

No, i’ve run for one time :

*rule "Energy init"
System started
postUpdate(CumulativeEnergyConsumption, 0 )
postUpdate(DailyEnergyConsumption, 0 )

and gives me 0.000000 and 0.00 as value in openhab

Now when i open page i see in the logs :
12:33:06.973 [ERROR] [o.o.c.s.ScriptExecutionThread :50 ] - Error during the execution of rule ‘Energy consumption calculation’: Could not invoke method: org.eclipse.xtext.xbase.lib.LongExtensions.operator_minus(long,byte) on instance: null
and no value in openhab…

i’m using only :
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*

and oh designer gives me a red X error on some var…


Also tried to remove all includes and no red X on openhab designer… but always :
Error during the execution of rule ‘Energy consumption calculation’: Could not invoke method: org.eclipse.xtext.xbase.lib.LongExtensions.operator_minus(long,byte) on instance: null