Calculating kWh from instantaneous W

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 ?

I’ve studied loginfo so :

logInfo(“currentTime”,“this is currentTime:” + currentTime)
respond me : this is currentTime:1457787706860

logInfo(“LastUpdate”,“this is lastupdate:” + LastUpdate)
this is lastupdate:LastUpdate (Type=DateTimeItem, State=2016-03-12T13:58:20)

So pheraphs is impossible to do currentTime - LastUpdate ???

Ok i suppose that i’ve fixed…

14:17:53.696 [INFO ] [enhab.model.script.currentTime:53 ] - this is currentTime:1457788673689
14:17:53.705 [INFO ] [nhab.model.script.myLastUpdate:53 ] - this is myLastUpdate:1457788667554
14:17:53.741 [INFO ] [enhab.model.script.timeElapsed:53 ] - this is timeElapsed:6135
14:17:53.768 [INFO ] [g.openhab.model.script.potenza:53 ] - this is potenza:420.25
14:17:53.786 [INFO ] [model.script.energyConsumption:53 ] - this is energyConsumption:0.00071618

power is a switch item :frowning:
and lastupdate i don’t know from he take it…

changing name works fine … i hope !

Thanks for help

Please explain me only one issue that i don’t know…

Where this data are stored?
Why if rule is not executed for some reason - for example if i move it or delete it - and then i put it online again all items must be initialised again and old values doesn’t exist anymore ?

And same issue if i restart openhab ???

Thanks
Dario

I think that i must setup a parallel database because rrd is not easily quereable… so a parallel mysql could be right… (graph doesn’t work well with mysql)…

Sorry for my stupid question… just to put in rrd4 persistence all field and he restore correctly at startup…
Thanks for helping !
Dario

Hi guys
I managed to create a fake instant Watts sum as follows: i have a fgs 222 which controls a lamp with 3 bulbs (2+1) and knowing that 1 bulb has 9 w power consumption i have 2 items in a sum group and using a rule i can display the instant power consumption.
I would like to convert this instant power consumption in kwh like today’s power consumption and total power consumption.
I also have a Fibaro dimmer 2 that reports the instant power consumption and the total kwh and i have a rule that reports the instant cost and the total cost according to total power consumption.

Right now i would like to make a rule that calculate the kwh and using the eg above i made this rule but its not displaying anything in the sitemap.
this is the rule:
when
Item Bedroom_power1 received update
then
var long currentTime = now.millis
if (LastUpdate != 0) {
var long timeElapsed = currentTime - LastUpdate
if (timeElapsed > 0) {
var Number power = Bedroom_power1.state as DecimalType
var Number energyConsumption = (power * timeElapsed) / 3600000 / 1000 // kWh
postUpdate(Bedroom_energy, Bedroom_energy.state as DecimalType + energyConsumption) //increment
}
}

LastUpdate = currentTime
end

The item Bedroom_power1 displays the instant power consumption for the 1st light and its one of the two items in the group Group:Number:SUM LightsWatts “Lights Watts [%d w]”. The Bedroom_energy item should display the kWh but unfortunately i need to use the group LightsWatts which contains both lights and displays the sum and i think i need the sum value in this rule.
Anyhow trying both the group item or one of the two W items its not showing anything in the sitemap…
Could anyone please take a look o the rule and maybe point me the right direction?
Thanks!

1 Like