Script Example-Apply Rate to Use and Demand
TASK: Apply a rate to usage and demand (that would come from submeter readings/channel):
Getting usage from a reading channel is supported via the ReadingDelta function. If there is a Demand channel, the Max demand value for the date range determined by the bill start and end dates can be retrieved from there.
Getting those values into a Rate and calculating the cost is where things get tricky. As long as you know the names of the rate inputs for the Usage and Demand for the rate and know the names of the rate outputs that will have the values you want, it can be done. Since these values are available to the person writing the script, it's not a huge task and once it is done for the first one, most of it becomes a cut/paste for any others using the same rate and bill template. And since most of the rates will be using the standard names from the Wizard, it's mostly a matter of choosing the right values.
First you need a way to do the rate calculation:
Function DoRateCalculation(RateTariffID, StartDate, Usage, Demand)
Dim Tariff
Set Tariff = CreateObject("EnergySense.Tariff")
Tariff.Load(RateTariffID)
' Load the rate
Dim Rate
Set Rate = Tariff.GetRate(StartDate)
' create a bill that matches the rate
Dim RateBill
Set RateBill = Rate.CreateBill
' populate its use (and dem)
For Each Line in RateBill.Lines
If Line.Caption="Usage" Then
Line.Value = Usage
ElseIf Line.Caption="BilledDemand" Then
Line.Value = Demand
ElseIf Line.Caption="ActualDemand" Then
Line.Value = Demand
End If
Next
Set DoRateCalculation = Rate.Calculate(RateBill)
End Function
If that is done, the return value from this Function is a Collection of Rate Outputs so the whole thing looks something like this:
Dim outputs
Set outputs = DoRateCalculation(1024, Dest.StartDate, Usage, Demand)
If IsObject(outputs) Then
Dim RateEnergyCost
RateEnergyCost = 0.0
Dim RateDemandCost
RateDemandCost = 0.0
Dim RateTotalCost
RateTotalCost = 0.0
For Each Element in Outputs
If Element.Name = "TotalCost" Then
RateTotalCost = Element.Value
ElseIf Element.Name = "EnergyCost" Then
RateEnergyCost = Element.Value
ElseIf Element.Name = "DemandCost" Then
RateDemandCost = Element.Value
End If
Next
' put values into created bill (Dest)
If RateEnergyCost <> 0.0 Then
For Each Line In Dest.Lines
If Line.Caption="EnergyCost" Then
Line.Cost = RateEnergyCost
ElseIf Line.Caption = "TotalCost" Then
Line.Cost = RateTotalCost
ElseIf Line.Caption = "DemandCost" Then
Line.Cost = RateDemandCost
End If
Next
End If
End If

