From fc68df7a6ec7318d23e92562b550dd8ceb4d618b Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Fri, 10 May 2024 17:09:27 +0200 Subject: [PATCH] Introduce a trigger channel to announce the availability of day-ahead prices (#16704) Signed-off-by: Jacob Laursen --- .../README.md | 41 +++++++++++++++++++ .../EnergiDataServiceBindingConstants.java | 4 ++ .../handler/EnergiDataServiceHandler.java | 11 +++-- .../OH-INF/i18n/energidataservice.properties | 2 + .../resources/OH-INF/thing/channel-groups.xml | 1 + .../resources/OH-INF/thing/channel-types.xml | 11 +++++ .../resources/OH-INF/thing/thing-service.xml | 2 +- .../resources/OH-INF/update/instructions.xml | 6 +++ 8 files changed, 74 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.energidataservice/README.md b/bundles/org.openhab.binding.energidataservice/README.md index 01cad5a0aa1..b7130ade9c5 100644 --- a/bundles/org.openhab.binding.energidataservice/README.md +++ b/bundles/org.openhab.binding.energidataservice/README.md @@ -170,6 +170,14 @@ A persistence configuration is required for this channel. Please note that the CO₂ emission channels only apply to Denmark. These channels will not be updated when the configured price area is not DK1 or DK2. +#### Trigger Channels + +Advanced channel `event` can trigger the following events: + +| Event | Description | +|----------------------|--------------------------------| +| DAY_AHEAD_AVAILABLE | Day-ahead prices are available | + ## Thing Actions Thing actions can be used to perform calculations as well as import prices directly into rules without relying on persistence. @@ -561,3 +569,36 @@ console.log("Spot price two hours from now: " + price); ::: :::: + +### Trigger Channel Example + +:::: tabs + +::: tab DSL + +```javascript +rule "Day-ahead event" +when + Channel 'energidataservice:service:energidataservice:electricity#event' triggered 'DAY_AHEAD_AVAILABLE' +then + logInfo("Day-ahead", "Day-ahead prices for the next day are now available") +end +``` + +::: + +::: tab JavaScript + +```javascript +rules.when() + .channel('energidataservice:service:energidataservice:electricity#event').triggered('DAY_AHEAD_AVAILABLE') + .then(event => + { + console.log('Day-ahead prices for the next day are now available'); + }) + .build("Day-ahead event"); +``` + +::: + +:::: diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/EnergiDataServiceBindingConstants.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/EnergiDataServiceBindingConstants.java index 2b2ed0e3183..82eeb8f58ce 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/EnergiDataServiceBindingConstants.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/EnergiDataServiceBindingConstants.java @@ -56,6 +56,7 @@ public class EnergiDataServiceBindingConstants { + ChannelUID.CHANNEL_GROUP_SEPARATOR + "co2-emission-prognosis"; public static final String CHANNEL_CO2_EMISSION_REALTIME = CHANNEL_GROUP_ELECTRICITY + ChannelUID.CHANNEL_GROUP_SEPARATOR + "co2-emission-realtime"; + public static final String CHANNEL_EVENT = CHANNEL_GROUP_ELECTRICITY + ChannelUID.CHANNEL_GROUP_SEPARATOR + "event"; public static final Set ELECTRICITY_CHANNELS = Set.of(CHANNEL_SPOT_PRICE, CHANNEL_GRID_TARIFF, CHANNEL_SYSTEM_TARIFF, CHANNEL_TRANSMISSION_GRID_TARIFF, CHANNEL_ELECTRICITY_TAX, @@ -67,6 +68,9 @@ public class EnergiDataServiceBindingConstants { public static final String PROPERTY_LAST_CALL = "lastCall"; public static final String PROPERTY_NEXT_CALL = "nextCall"; + // List of all events + public static final String EVENT_DAY_AHEAD_AVAILABLE = "DAY_AHEAD_AVAILABLE"; + // List of supported currencies public static final Currency CURRENCY_DKK = Currency.getInstance("DKK"); public static final Currency CURRENCY_EUR = Currency.getInstance("EUR"); diff --git a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java index 8964cd4cb88..785b6553646 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java +++ b/bundles/org.openhab.binding.energidataservice/src/main/java/org/openhab/binding/energidataservice/internal/handler/EnergiDataServiceHandler.java @@ -239,8 +239,9 @@ public class EnergiDataServiceHandler extends BaseThingHandler { private void refreshElectricityPrices() { RetryStrategy retryPolicy; try { + boolean spotPricesDownloaded = false; if (isLinked(CHANNEL_SPOT_PRICE)) { - downloadSpotPrices(); + spotPricesDownloaded = downloadSpotPrices(); } for (DatahubTariff datahubTariff : DatahubTariff.values()) { @@ -259,6 +260,9 @@ public class EnergiDataServiceHandler extends BaseThingHandler { if (numberOfFutureSpotPrices >= 13 || (numberOfFutureSpotPrices == 12 && now.isAfter(DAILY_REFRESH_TIME_CET.minusHours(1)) && now.isBefore(DAILY_REFRESH_TIME_CET))) { + if (spotPricesDownloaded) { + triggerChannel(CHANNEL_EVENT, EVENT_DAY_AHEAD_AVAILABLE); + } retryPolicy = RetryPolicyFactory.atFixedTime(DAILY_REFRESH_TIME_CET, NORD_POOL_TIMEZONE); } else { logger.warn("Spot prices are not available, retry scheduled (see details in Thing properties)"); @@ -287,10 +291,10 @@ public class EnergiDataServiceHandler extends BaseThingHandler { reschedulePriceRefreshJob(retryPolicy); } - private void downloadSpotPrices() throws InterruptedException, DataServiceException { + private boolean downloadSpotPrices() throws InterruptedException, DataServiceException { if (cacheManager.areSpotPricesFullyCached()) { logger.debug("Cached spot prices still valid, skipping download."); - return; + return false; } DateQueryParameter start; if (cacheManager.areHistoricSpotPricesCached()) { @@ -307,6 +311,7 @@ public class EnergiDataServiceHandler extends BaseThingHandler { } finally { updateProperties(properties); } + return true; } private void downloadTariffs(DatahubTariff datahubTariff) throws InterruptedException, DataServiceException { diff --git a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/i18n/energidataservice.properties b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/i18n/energidataservice.properties index 516b6752fe6..51bb80f6dff 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/i18n/energidataservice.properties +++ b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/i18n/energidataservice.properties @@ -81,6 +81,8 @@ channel-type.energidataservice.co2-emission.label = CO₂ Emission channel-type.energidataservice.co2-emission.description = CO₂ emission in g/kWh. channel-type.energidataservice.datahub-price.label = Datahub Price channel-type.energidataservice.datahub-price.description = Datahub price. +channel-type.energidataservice.event.label = Event +channel-type.energidataservice.event.description = Event triggered channel-type.energidataservice.spot-price.label = Spot Price channel-type.energidataservice.spot-price.description = Spot price. diff --git a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-groups.xml b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-groups.xml index add4ee51c21..8786dcd08ee 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-groups.xml +++ b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-groups.xml @@ -40,6 +40,7 @@ Near up-to-date history for CO₂ emission from electricity consumed in Denmark in g/kWh. + diff --git a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-types.xml b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-types.xml index 1509d177dbb..92822b28bf9 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-types.xml +++ b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/channel-types.xml @@ -29,4 +29,15 @@ + + trigger + + Event triggered + + + + + + + diff --git a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/thing-service.xml b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/thing-service.xml index fca5f24f6a9..fd893596bfd 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/thing-service.xml +++ b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/thing/thing-service.xml @@ -14,7 +14,7 @@ - 5 + 6 diff --git a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/update/instructions.xml b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/update/instructions.xml index 1366e1898b5..80c730dd2a7 100644 --- a/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/update/instructions.xml +++ b/bundles/org.openhab.binding.energidataservice/src/main/resources/OH-INF/update/instructions.xml @@ -78,6 +78,12 @@ + + + energidataservice:event + + +