Calculating kWh from instantaneous W

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: http://www.aliexpress.com/item/Energy-Monitor-Shield-Monitoring-System-with-Nokia-LCD-Screen-DIY-Maker-Electronic-Seeed-BOOOLE/32284256758.html and SCT-013-000 sensors http://www.aliexpress.com/item/Free-Shipping-New-Non-invasive-0-100A-AC-Sensor-Split-Core-Current-Transformer-SCT-013-000/32280304935.html
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

Gambituk

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”
////////////////////////////////////////////////////////////////////////////////////////////////
when
Item Node_garage1 received update
then
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
end
//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Clear daily consumption”
//////////////////////////////////////////////////////////////////////////////////////////////////
when
Time cron “0 0 0 * * ?” // every day
then
postUpdate(DailyEnergyConsumption, 0)
end
//////////////////////////////////////////////////////////////////////////////////////////////////
rule “Energy in past hour”
when
Time cron “0 0 0/1 1/1 * ? *” // every hour
then
postUpdate(HoursEnergyConsumption, (CumulativeEnergyConsumption.state as DecimalType - CumulativeEnergyConsumption.historicState(now.minusHours(1)).state as DecimalType))
end

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Items
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

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

//////////////------------VARIABLES_ENERGY-------------\\\\\\\\\\
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?
[1]: http://forum.mysensors.org/topic/655/serial-gateway-connection-to-openhab/2
[2]: http://openenergymonitor.org/emon/buildingblocks/how-to-build-an-arduino-energy-monitor?page=7

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

Thanks
Dario

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"
when
System started
then
postUpdate(CumulativeEnergyConsumption, 0 )
postUpdate(DailyEnergyConsumption, 0 )
end

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…

Thanks
Dario

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

Thanks
Dario

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

These are the only includes that i am currently using…

Sob :frowning: what OH version?

And what is binding of
Number CumulativeEnergyConsumption “total energy [%.2f kWh]” { mqtt=“>[mysensor:MyMQTT/55/0/info:state:*:default]” }

Have you got a sensor that give you CumulativeEnergyConsumption???

1.7.1 for me, and the item is Sending > to mqtt topic, not < Receiving . It’s how i’m displaying the output of the calculations…

Maybe try outputting all the values with LogInfo to try to see what is or isn’t happening…

Ok, but problem is in some other place… i don’t have idea to outputting all the values with LogInfo :frowning2:

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

it seems a java error on instance : null … really no idea.

Pheraphs could be openhab version…

After first running of initialising rule, line var long LastUpdate = 0 //// <- At the top of the file
must be always here or not ?

I have it in script…

Ok i’ve understand that is an OPERATOR issue…

error is because he doesn’t like :
currentTime - LastUpdate
power * timeElapsed) / 3600000
CumulativeEnergyConsumption.state as DecimalType + energyConsumption

and all lines with - + * / operand…
why ?