]> git.basschouten.com Git - openhab-addons.git/commitdiff
Persist future prices (#15864)
authorJacob Laursen <jacob-github@vindvejr.dk>
Sat, 11 Nov 2023 12:39:05 +0000 (13:39 +0100)
committerGitHub <noreply@github.com>
Sat, 11 Nov 2023 12:39:05 +0000 (13:39 +0100)
Resolves #15863

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.binding.energidataservice/README.md
bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java

index 7f43adf2cf5a2e3bc76c373bdd2f2e0ce19f587c..8b717e19d9fc451e013d066dfe67b5836f784c9d 100644 (file)
@@ -75,6 +75,13 @@ To include VAT for items linked to the `Number` channels, the [VAT profile](http
 This must be installed separately.
 Once installed, simply select "Value-Added Tax" as Profile when linking an item.
 
+#### Persisting Time Series
+
+The binding offers support for persisting both historical and upcoming prices.
+The recommended persistence strategy is `forecast`, as it ensures a clean history without redundancy.
+Prices from the past 24 hours and all forthcoming prices will be stored.
+Any changes that impact published prices (e.g. selecting or deselecting VAT Profile) will result in the replacement of persisted prices within this period.
+
 #### Net Tariff
 
 Discounts are automatically taken into account for channel `net-tariff` so that it represents the actual price.
index 3241685f3d9c3d8b141c247a06e340c9476a80f0..2a71212972c21f0fba5ed38f3c3f7659b255886a 100644 (file)
@@ -13,6 +13,7 @@
 package org.openhab.binding.energidataservice.internal.handler;
 
 import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*;
+import static org.openhab.core.types.TimeSeries.Policy.REPLACE;
 
 import java.math.BigDecimal;
 import java.time.Duration;
@@ -24,6 +25,7 @@ import java.time.temporal.ChronoUnit;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Currency;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -65,6 +67,7 @@ import org.openhab.core.thing.binding.BaseThingHandler;
 import org.openhab.core.thing.binding.ThingHandlerService;
 import org.openhab.core.types.Command;
 import org.openhab.core.types.RefreshType;
+import org.openhab.core.types.TimeSeries;
 import org.openhab.core.types.UnDefType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -180,6 +183,7 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
 
             updateStatus(ThingStatus.ONLINE);
             updatePrices();
+            updateTimeSeries();
 
             if (isLinked(CHANNEL_SPOT_PRICE) || isLinked(CHANNEL_HOURLY_PRICES)) {
                 if (cacheManager.getNumberOfFutureSpotPrices() < 13) {
@@ -348,6 +352,50 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
         updateState(CHANNEL_HOURLY_PRICES, new StringType(gson.toJson(targetPrices)));
     }
 
+    private void updateTimeSeries() {
+        TimeSeries spotPriceTimeSeries = new TimeSeries(REPLACE);
+        Map<DatahubTariff, TimeSeries> datahubTimeSeriesMap = new HashMap<>();
+        for (DatahubTariff datahubTariff : DatahubTariff.values()) {
+            datahubTimeSeriesMap.put(datahubTariff, new TimeSeries(REPLACE));
+        }
+
+        Map<Instant, BigDecimal> spotPriceMap = cacheManager.getSpotPrices();
+        List<Entry<Instant, BigDecimal>> spotPrices = spotPriceMap.entrySet().stream()
+                .sorted(Map.Entry.comparingByKey()).toList();
+        for (Entry<Instant, BigDecimal> spotPrice : spotPrices) {
+            Instant hourStart = spotPrice.getKey();
+            if (isLinked(CHANNEL_SPOT_PRICE)) {
+                spotPriceTimeSeries.add(hourStart, new DecimalType(spotPrice.getValue()));
+            }
+            for (Map.Entry<DatahubTariff, TimeSeries> entry : datahubTimeSeriesMap.entrySet()) {
+                DatahubTariff datahubTariff = entry.getKey();
+                String channelId = datahubTariff.getChannelId();
+                if (!isLinked(channelId)) {
+                    continue;
+                }
+                BigDecimal tariff = cacheManager.getTariff(datahubTariff, hourStart);
+                if (tariff != null) {
+                    TimeSeries timeSeries = entry.getValue();
+                    timeSeries.add(hourStart, new DecimalType(tariff));
+                }
+            }
+        }
+        if (spotPriceTimeSeries.size() > 0) {
+            sendTimeSeries(CHANNEL_SPOT_PRICE, spotPriceTimeSeries);
+        }
+        for (Map.Entry<DatahubTariff, TimeSeries> entry : datahubTimeSeriesMap.entrySet()) {
+            DatahubTariff datahubTariff = entry.getKey();
+            String channelId = datahubTariff.getChannelId();
+            if (!isLinked(channelId)) {
+                continue;
+            }
+            TimeSeries timeSeries = entry.getValue();
+            if (timeSeries.size() > 0) {
+                sendTimeSeries(channelId, timeSeries);
+            }
+        }
+    }
+
     /**
      * Get the configured {@link Currency} for spot prices.
      *