Calculating totalpower in a rule

  • Platform information:
    • Hardware: CPUArchitecture/RAM/storage
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 2
  • Issue of the topic

I like to calculate the totalpower of my wallplugs. therfore i put in the items

Number Z_way_number_WallPlug8_totalpower "Wallplug8 [%.1f]" {channel="zway:zwayDevice:d5ec35fd:8:sensorMultilevel-ZWayVDev_zway_8-0-50-0"}
Number Z_way_number_WallPlug8_totalpower "Wallplug9 [%.1f]" {channel="zway:zwayDevice:d5ec35fd:9:sensorMultilevel-ZWayVDev_zway_9-0-50-0"}
Number totalpower "[%.2f]"

my rule

rule "totalpower"
    Time cron "0/10 0 0 ? * * *" // Every 10 seconds
    var float vv = Z_way_number_WallPlug8_totalpower.floatValue + Z_way_number_WallPlug9_totalpower.floatValue

But nothing happend what is wrong???

Is there a reason you’re trying to do in a rule rather than as an aggregate group?

On phone else I’d link to the documentation. But you can do sum and average on all items in a group and it stores that value and updates it whenever any group items update

What ist the betterway?

Your Time cron is wrong.

Time cron "0/10 0 0 ? * * *"
//          ^   ^ ^ ^ ^ ^ ^
//          |   | | | | | Every year
//          |   | | | | Every day of Week
//          |   | | | Every Month
//          |   | | Day of Month does not matter
//          |   | Hour has to be 0 (!)
//          |   Minute has to be 0 (1)
//          Every 10 Seconds, beginning with 0

So your rule will trigger each 10 seconds when it’s 12:00 a.m. use asterisks for minute and hour :slight_smile:

When it comes to total power consumption, please be aware that power sensors will do integral math themselves, so the given number is already a measurement for a time segment. Your power calculation has to use the same time segment as the power sensor.

I changed it to

rule "totalpower"
    Time cron "0/10 * * ? * * *"
    var float totalpower = (group_house_totalpower.members[m].state as DecimalType).sum

but error is

'state' is not a member of 'java.util.concurrent.CopyOnWriteArrayList';

try to use a Group like psyciknz recommended:

Group:Number:SUM gWallPlugs "Total Wall Plugs Power [%.1f]"
Number Z_way_number_WallPlug8_totalpower "Wallplug8 [%.1f]" (gWallPlugs) {channel="zway:zwayDevice:d5ec35fd:8:sensorMultilevel-ZWayVDev_zway_8-0-50-0"}
Number Z_way_number_WallPlug9_totalpower "Wallplug9 [%.1f]" (gWallPlugs) {channel="zway:zwayDevice:d5ec35fd:9:sensorMultilevel-ZWayVDev_zway_9-0-50-0"}

I want to calculate the daily, monthly,yearly consumption therefore I need it as variable in a rule. Can anybody help me?

doing these kind of calculations within a rule that fires upon a cron schedule is somewhat inefficient
the best method is to use a persistence service.
You can then use the persisted states within a rule.

Also: #7

you have the same name in item (Wallplug9 not existence )

1 Like

If you want to calculate the comsumtion this is not the way.
You need to calculate the energy in kwh or wh
It is quite impossible to calculate it just measuring the power at a defined period but if you want a very rough energy value by measuring the istant power every 10 second you need to use the following formula:
Energi10s=totalpower*10/3600 this will be a very rough value of the energy comsubtion each 10 sec.
I also suggest to measure the total energy each 10 sec as totalenergy=totalenergy+energy10s
Later I suggest to use persistance so you can calculate the daily, month and year energy
Take care that this energy measurement is very very rough …

1 Like

Can you please help me a little bit with the persistence in a rule?

I want to calculate a sum of all values every minute of a persistence instance.

This is my persistence line

group_house_actualpower* : strategy = everyMinute, restoreOnStartup


don’t mix the topics
Persistence Service configuration tells openHAB to store on predefined intervals the state of the Items
Your persistence config will store everyMinute the state of the Items which are members of the group_house_actualpower Group (and will also perform restoreOnStartup).

To use the stored data within a rule, read the link that I already gave you:
and search the forum for examples.

Which Persistence Service Add-on did you install?

I got a little bit closer now it`s time for you to optimise it

rule "totalpower"
    Time cron "0 * * ? * * *"
    val String currentTime = String::format( "%1$tY-%1$tm-%1$tdT00:00:00.0000", new java.util.Date )
    var float totalpower =  (number_totalpower.state as DecimalType) + Z_way_number_WallPlug2_actualpower.sumSince(now.minusMinutes(1)) + Z_way_number_WallPlug3_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug4_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug5_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug6_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug7_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug8_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug9_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug10_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug11_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug12_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug13_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug20_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug21_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug22_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug23_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug24_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug25_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug26_actualpower.sumSince(now.minusMinutes(1))+ Z_way_number_WallPlug27_actualpower.sumSince(now.minusMinutes(1))
    logInfo("Rules", "Test: currentTime=[{}]",currentTime)

evry actual power value is in the group group_house_actualpower

1 Like