# Creating rules for energy counting need help

Hello all,
I try to count my total energy need in my house by using zwave sensors for the domestic plugs. The sensors are all called
Z_way_number_WallPlug(x)_totalpower [(x) = the number of the plug] which gave me a total energy flown through the plug back.

In my example I use only 2 plugs but t should aterwards easy to extend to more plugs.
My idea is first to calculate the daily total energy by subtraction the actual value by the value of yesterday:

``````rule "set number_wallplug2dailytotalenergy"
when
Item Z_way_number_WallPlug2_totalpower changed
then
try{
if (Z_way_number_WallPlug2_totalpower != NULL && Z_way_number_WallPlug2_totalpower != UNDEF) {
if (Z_way_number_WallPlug2_totalpower.state != NULL && Z_way_number_WallPlug2_totalpower.state != UNDEF)
{
var local_time_yesterday = now.with(LocalTime.of(0, 0, 0, 0)).minusMinutes(1)
var Number local_number_wallplug2dailytotalenergy = (Z_way_number_WallPlug2_totalpower.state as Number) - (Z_way_number_WallPlug2_totalpower.historicState(local_time_yesterday).state as Number)
number_wallplug2dailytotalenergy.postUpdate(local_number_wallplug2dailytotalenergy)
}
else
{
number_wallplug2dailytotalenergy.postUpdate(0)
}
}
else
{
number_wallplug2dailytotalenergy.postUpdate(0)
}
}
catch(Throwable T)
{
logError("Error", "Some bad stuff happened in \"set number_wallplug2dailytotalenergy\": " + T.toString)
}
finally
{

}
end
``````
``````rule "set number_wallplug3dailytotalenergy"
when
Item Z_way_number_WallPlug3_totalpower changed
then
try{
if (Z_way_number_WallPlug3_totalpower != NULL && Z_way_number_WallPlug3_totalpower != UNDEF) {
if (Z_way_number_WallPlug3_totalpower.state != NULL && Z_way_number_WallPlug3_totalpower.state != UNDEF)
{
var local_time_yesterday = now.with(LocalTime.of(0, 0, 0, 0)).minusMinutes(1)
var Number local_number_wallplug2dailytotalenergy = (Z_way_number_WallPlug2_totalpower.state as Number) - (Z_way_number_WallPlug2_totalpower.historicState(local_time_yesterday).state as Number)
number_wallplug3dailytotalenergy.postUpdate(local_number_wallplug3dailytotalenergy)
}
else
{
number_wallplug3dailytotalenergy.postUpdate(0)
}
}
else
{
number_wallplug3dailytotalenergy.postUpdate(0)
}
}
catch(Throwable T)
{
logError("Error", "Some bad stuff happened in \"set number_wallplug3dailytotalenergy\": " + T.toString)
}
finally
{

}
end
``````

Then calculate the sum of all plugs for the totaly daily energy

``````rule "set number_dailytotalenergy"
when
Item number_wallplug2dailytotalenergy changed OR
Item number_wallplug3dailytotalenergy changed
then
try{
if (number_wallplug2dailytotalenergy != NULL && number_wallplug2dailytotalenergy  != UNDEF &&
number_wallplug3dailytotalenergy != NULL && number_wallplug3dailytotalenergy  != UNDEF) {
if (number_wallplug2dailytotalenergy.state != NULL && number_wallplug2dailytotalenergy.state != UNDEF &&
number_wallplug3dailytotalenergy.state != NULL && number_wallplug3dailytotalenergy.state != UNDEF){
var Number local_number_dailytotalenergy = (number_wallplug2dailytotalenergy.state as Number) + (number_wallplug3dailytotalenergy.state as Number)
number_dailytotalenergy.postUpdate(local_number_dailytotalenergy)
}
else
{
number_dailytotalenergy.postUpdate(0)
}
}
else
{
number_dailytotalenergy.postUpdate(0)
}
}
catch(Throwable T)
{
logError("Error", "Some bad stuff happened in \"set number_dailytotalenergy\": " + T.toString)
}
finally
{

}
end
``````

and on midnight reset the daily energy and caluclate the monthly total energy by add the old monthly value with the new daily value

``````rule "set local_number_monthlytotalenergy reset number_wallplugdailytotalenergy"
when
Time is midnight
then
try{
if(number_monthlytotalenergy.state != NULL && number_monthlytotalenergy.state != UNDEF &&
number_dailytotalenergy.state != NULL && number_dailytotalenergy.state != UNDEF)
{
var Number local_number_monthlytotalenergy = (number_monthlytotalenergy.state as Number) + (number_dailytotalenergy.state as Number)
number_monthlytotalenergy.postUpdate(local_number_monthlytotalenergy)
number_wallplug2dailytotalenergy.postUpdate(0)
number_wallplug3dailytotalenergy.postUpdate(0)
}
else
{
number_monthlytotalenergy.postUpdate(0)

}
}
catch(Throwable T)
{
logError("Error", "Some bad stuff happened in \"set local_number_monthlytotalenergy reset number_wallplugdailytotalenergy\": " + T.toString)
}
finally
{

}
end
``````

and on 2minutes after midnight 1st january every year calculating the energy over the past years:

``````rule "set number_overalltotalenergy reset number_yearlytotalenergy"
when
Time cron "0 2 0 1 1 ? * ?"
then
try{
if(number_overalltotalenergy.state != NULL && number_overalltotalenergy.state != UNDEF &&
number_yearlytotalenergy.state != NULL && number_yearlytotalenergy.state != UNDEF)
{
var Number local_number_overalltotalenergy = (number_overalltotalenergy.state as Number) + (number_yearlytotalenergy.state as Number)
number_overalltotalenergy.postUpdate(local_number_overalltotalenergy)
number_yearlytotalenergy.postUpdate(0)
}
else
{
number_overalltotalenergy.postUpdate(0)
number_yearlytotalenergy.postUpdate(0)
}
}
catch(Throwable T)
{
logError("Error", "Some bad stuff happened in \"set number_overalltotalenergy reset number_yearlytotalenergy\": " + T.toString)
}
finally
{

}
end
``````

How do you thing about my soultion do you have any help for me to make it better? Or any better solution?

Instead of doing your own calculation you can try to use the deltaSince function for daily, monthly, yearly items, that’s also available based on persistence service.

Assuming you’re items state will update frequently at least every few minutes you can use event based triggers instead of having time based rules (e.g. on every update of your totalpower item, like in first rule) even for monthly and yearly numbers.

To get the sum of all plugs maybe use a group and aggregate function, then you don’t need a rule at all