Calculating power usage based on ON time of appliance

Hi,

I want to calculate the power usage of an appliance based on how long it is turned on for in a nominal period (let’s say the last 24 hours). The appliance in question has a constant power draw of 500W.

The best way I can think of to do this is to update a number item every minute based on the on state of the appliance. In this case the item will have value 0 if the appliance is off and 500 if it is on. I would then use sumSince to calculate the power usage in ‘watt-minutes’. Dividing by 60 would give me the watt-hour value.

The downsides of this approach are:

  • Requires me to persist power data for each appliance on a minute by minute basis
  • Not very granular - if the state changes within a minute then this will not be reflected.

If I could work out the total ON time of the appliance over my time range, then calculating the power usage should be trivial, but I can’t see how to do this from the persistence extensions.

Anyone have any ideas?

I haven’t done this myself, so the code below may not run as is. But the point is to use items to hold the running watt hours and the last time the light was turned on, and increase the watt hours item as the switch’s state changes between ON and OFF.

items

DateTime FanOnTime "[%1$tm/%1$td %1$tH:%1$tM]" <calendar>
Switch FanSwitch { binding to turn fan off and on }
Number FanWattHours "%.2f" (Persist)

rules

import org.joda.time.*
import org.openhab.core.library.types.*

rule FanOn
when
  Item FanSwitch changed to ON
then
  FanOnTime.postUpdate(new DateTimeType())
end

rule FanWatts
when
  Item FanSwitch changed to OFF
then
  if (FanOnTime.state instanceof DateTimeType) {
    var Instant onTime = new Instant((FridgeOnTime.state as DateTimeType).calendar)
    var int secondsOn = Seconds.secondsBetween(onTime, now).seconds
    var DecimalType wattHours = (FridgeWattHours.state as DecimalType) + 
((secondsOn/3600.0) * 500)
    FanWattHours.postUpdate(wattHours)
  }
end
2 Likes

That approach looks good, although I agree that you code looks a little shaky - I’ll be converting it to Jython, so it doesn’t matter.

Combining this with once daily persistence and reset of the watt hours item would give me what I ultimately want which is a daily value of power used.

Thanks!

In that case you will need a rule that triggers at midnight to do bookkeeping:

  • if the switch is ON at midnight, go through the same steps as when the switch is turned OFF, to get usage until midnight.
  • FanOnTime.postUpdate(now)
  • FanDailyWattHours.postUpdate(FanWattHours.state)
  • FanWattHours.postUpdate(0)

Let us know how it goes!

Nice, I like it. I’ll be sure to update this topic when I have something working.

1 Like

Rich helped me with the same problem in the link below. last post has a complete working config:
https://community.openhab.org/t/calculate-time-on-and-energy-use-for-an-item-switch-rule/4608/7