From 563caa0762c4cd690242479882c6dc6a32845eae Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Fri, 3 Jan 2025 23:16:28 +0100 Subject: [PATCH] [energidataservice] Improve rule example for calculating totals (#18024) * Improve rule example for calculating totals Signed-off-by: Jacob Laursen Co-authored-by: Jimmy Tanagra --- .../README.md | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/README.md b/bundles/org.openhab.binding.energidataservice/README.md index 11e0a42fcc2..8849a758e30 100644 --- a/bundles/org.openhab.binding.energidataservice/README.md +++ b/bundles/org.openhab.binding.energidataservice/README.md @@ -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 ```