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.
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