[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,6 +87,8 @@ In this example file-based using Rule Builder:
rules.when()
.channel('energidataservice:service:energidataservice:electricity#event').triggered('DAY_AHEAD_AVAILABLE')
.then(event => {
// 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));
@ -100,6 +102,7 @@ rules.when()
timeSeries.add(spotPrice.timestamp, totalPrice);
}
items.TotalPrice.persistence.persist(timeSeries);
}, 5000);
})
.build("Calculate total price");
```
@ -112,6 +115,7 @@ rules.when()
rule "Calculate total price" do
channel "energidataservice:service:energidataservice:electricity#event", triggered: "DAY_AHEAD_AVAILABLE"
run do
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
@ -130,6 +134,7 @@ rule "Calculate total price" do
end
TotalPrice.persist(time_series)
end
end
end
```