[energidataservice] Improve rule example for calculating totals (#18024)

* Improve rule example for calculating totals

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Co-authored-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
Jacob Laursen 2025-01-03 23:16:28 +01:00 committed by GitHub
parent 705feffd54
commit 563caa0762
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -87,19 +87,22 @@ In this example file-based using Rule Builder:
rules.when()
.channel('energidataservice:service:energidataservice:electricity#event').triggered('DAY_AHEAD_AVAILABLE')
.then(event => {
var timeSeries = new items.TimeSeries('REPLACE');
var start = time.LocalDate.now().atStartOfDay().atZone(time.ZoneId.systemDefault());
var spotPrices = items.SpotPrice.persistence.getAllStatesBetween(start, start.plusDays(2));
for (var spotPrice of spotPrices) {
var totalPrice = spotPrice.quantityState
.add(items.GridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.SystemTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.TransmissionGridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.ElectricityTax.persistence.persistedState(spotPrice.timestamp).quantityState);
// Short delay because persistence is asynchronous.
setTimeout(() => {
var timeSeries = new items.TimeSeries('REPLACE');
var start = time.LocalDate.now().atStartOfDay().atZone(time.ZoneId.systemDefault());
var spotPrices = items.SpotPrice.persistence.getAllStatesBetween(start, start.plusDays(2));
for (var spotPrice of spotPrices) {
var totalPrice = spotPrice.quantityState
.add(items.GridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.SystemTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.TransmissionGridTariff.persistence.persistedState(spotPrice.timestamp).quantityState)
.add(items.ElectricityTax.persistence.persistedState(spotPrice.timestamp).quantityState);
timeSeries.add(spotPrice.timestamp, totalPrice);
}
items.TotalPrice.persistence.persist(timeSeries);
timeSeries.add(spotPrice.timestamp, totalPrice);
}
items.TotalPrice.persistence.persist(timeSeries);
}, 5000);
})
.build("Calculate total price");
```
@ -112,23 +115,25 @@ rules.when()
rule "Calculate total price" do
channel "energidataservice:service:energidataservice:electricity#event", triggered: "DAY_AHEAD_AVAILABLE"
run do
# Persistence methods will call LocalDate#to_zoned_date_time which converts it
# to a ZonedDateTime in the default system zone, with 00:00 as its time portion
start = LocalDate.now
spot_prices = SpotPrice.all_states_between(start, start + 2.days)
after 5.seconds do # Short delay because persistence is asynchronous.
# Persistence methods will call LocalDate#to_zoned_date_time which converts it
# to a ZonedDateTime in the default system zone, with 00:00 as its time portion
start = LocalDate.now
spot_prices = SpotPrice.all_states_between(start, start + 2.days)
next unless spot_prices # don't proceed if the persistence result is nil
next unless spot_prices # don't proceed if the persistence result is nil
time_series = TimeSeries.new # the default policy is replace
spot_prices.each do |spot_price|
total_price = spot_price +
GridTariff.persisted_state(spot_price.timestamp) +
SystemTariff.persisted_state(spot_price.timestamp) +
TransmissionGridTariff.persisted_state(spot_price.timestamp) +
ElectricityTax.persisted_state(spot_price.timestamp)
time_series.add(spot_price.timestamp, total_price)
time_series = TimeSeries.new # the default policy is replace
spot_prices.each do |spot_price|
total_price = spot_price +
GridTariff.persisted_state(spot_price.timestamp) +
SystemTariff.persisted_state(spot_price.timestamp) +
TransmissionGridTariff.persisted_state(spot_price.timestamp) +
ElectricityTax.persisted_state(spot_price.timestamp)
time_series.add(spot_price.timestamp, total_price)
end
TotalPrice.persist(time_series)
end
TotalPrice.persist(time_series)
end
end
```