Extend and simplify date query offset support (#15246)

Fixes #15245

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-07-16 14:36:32 +02:00 committed by GitHub
parent 8701b86a37
commit 4c3b398398
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 28 deletions

View File

@ -71,11 +71,12 @@ The tariffs are downloaded using pre-configured filters for the different [Grid
If your company is not in the list, or the filters are not working, they can be manually overridden. If your company is not in the list, or the filters are not working, they can be manually overridden.
To override filters, the channel `net-tariff` has the following configuration parameters: To override filters, the channel `net-tariff` has the following configuration parameters:
| Name | Type | Description | Default | Required | Advanced | | Name | Type | Description | Default | Required | Advanced |
|-----------------|---------|----------------------------------------------------------------------------------------------------------------------------|---------|----------|----------| |-----------------|---------|----------------------------------------------------------------------------------------------------------------------------------|---------|----------|----------|
| chargeTypeCodes | text | Comma-separated list of charge type codes | | no | yes | | chargeTypeCodes | text | Comma-separated list of charge type codes | | no | yes |
| notes | text | Comma-separated list of notes | | no | yes | | notes | text | Comma-separated list of notes | | no | yes |
| start | text | Query start date parameter expressed as either YYYY-MM-DD or dynamically as one of StartOfDay, StartOfMonth or StartOfYear | | no | yes | | start | text | Query start date parameter expressed as either YYYY-MM-DD or dynamically as one of `StartOfDay`, `StartOfMonth` or `StartOfYear` | | no | yes |
| offset | text | Query start date offset expressed as an ISO 8601 duration | | no | yes |
The parameters `chargeTypeCodes` and `notes` are logically combined with "AND", so if only one parameter is needed for the filter, only provide this parameter and leave the other one empty. The parameters `chargeTypeCodes` and `notes` are logically combined with "AND", so if only one parameter is needed for the filter, only provide this parameter and leave the other one empty.
Using any of these parameters will override the pre-configured filter entirely. Using any of these parameters will override the pre-configured filter entirely.
@ -83,6 +84,10 @@ Using any of these parameters will override the pre-configured filter entirely.
The parameter `start` can be used independently to override the query start date parameter. The parameter `start` can be used independently to override the query start date parameter.
If used while leaving `chargeTypeCodes` and `notes` empty, only the date will be overridden. If used while leaving `chargeTypeCodes` and `notes` empty, only the date will be overridden.
The parameter `offset` can be used in combination with `start` to provide an offset to a dynamic start date parameter, i.e. `StartOfDay`, `StartOfMonth` or `StartOfYear`.
The needed amount of historic hours is automatically taken into consideration.
This parameter is ignored when start date is supplied as YYYY-MM-DD.
Determining the right filters can be tricky, so if in doubt ask in the community forum. Determining the right filters can be tricky, so if in doubt ask in the community forum.
See also [Datahub Price List](https://www.energidataservice.dk/tso-electricity/DatahubPricelist). See also [Datahub Price List](https://www.energidataservice.dk/tso-electricity/DatahubPricelist).
@ -97,8 +102,10 @@ _N1:_
_Nord Energi Net:_ _Nord Energi Net:_
| Parameter | Value | | Parameter | Value |
|-----------------|------------| |-----------------|------------|
| chargeTypeCodes | TA031U200 | | chargeTypeCodes | TAC |
| notes | Nettarif C | | notes | Nettarif C |
| start | StartOfDay |
| offset | -P1D |
#### Hourly Prices #### Hourly Prices

View File

@ -14,7 +14,6 @@ package org.openhab.binding.energidataservice.internal.api;
import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*; import static org.openhab.binding.energidataservice.internal.EnergiDataServiceBindingConstants.*;
import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Set; import java.util.Set;
@ -78,21 +77,21 @@ public class DatahubTariffFilterFactory {
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TCL>100_02")), Set.of(NOTE_NET_TARIFF_C_HOUR)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TCL>100_02")), Set.of(NOTE_NET_TARIFF_C_HOUR));
case GLN_ELEKTRUS: case GLN_ELEKTRUS:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("6000091")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("6000091")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ELINORD: case GLN_ELINORD:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43300")), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43300")),
Set.of("Transportbetaling, eget net C")); Set.of("Transportbetaling, eget net C"));
case GLN_ELNET_MIDT: case GLN_ELNET_MIDT:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("T3002")), Set.of(NOTE_NET_TARIFF_C), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("T3002")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ELNET_KONGERSLEV: case GLN_ELNET_KONGERSLEV:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("K_22100")), Set.of(NOTE_NET_TARIFF_C)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("K_22100")), Set.of(NOTE_NET_TARIFF_C));
case GLN_FLOW_ELNET: case GLN_FLOW_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("FE2 NT-01")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("FE2 NT-01")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_HAMMEL_ELFORSYNING_NET: case GLN_HAMMEL_ELFORSYNING_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("50001")), Set.of("Overliggende net"), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("50001")), Set.of("Overliggende net"),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_HURUP_ELVAERK_NET: case GLN_HURUP_ELVAERK_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("HEV-NT-01")), Set.of(NOTE_NET_TARIFF)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("HEV-NT-01")), Set.of(NOTE_NET_TARIFF));
case GLN_IKAST_E1_NET: case GLN_IKAST_E1_NET:
@ -105,19 +104,19 @@ public class DatahubTariffFilterFactory {
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("4010")), Set.of(NOTE_NET_TARIFF_C_HOUR)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("4010")), Set.of(NOTE_NET_TARIFF_C_HOUR));
case GLN_MIDTFYNS_ELFORSYNING: case GLN_MIDTFYNS_ELFORSYNING:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT15000")), Set.of(NOTE_NET_TARIFF_C_FLEX), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT15000")), Set.of(NOTE_NET_TARIFF_C_FLEX),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_N1: case GLN_N1:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("CD"), ChargeTypeCode.of("CD R")), Set.of(), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("CD"), ChargeTypeCode.of("CD R")), Set.of(),
DateQueryParameter.of(N1_CUTOFF_DATE)); DateQueryParameter.of(N1_CUTOFF_DATE));
case GLN_NETSELSKABET_ELVAERK: case GLN_NETSELSKABET_ELVAERK:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("0NCFF")), Set.of(NOTE_NET_TARIFF_C + " Flex"), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("0NCFF")), Set.of(NOTE_NET_TARIFF_C + " Flex"),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NKE_ELNET: case GLN_NKE_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("94TR_C_ET")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("94TR_C_ET")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NORD_ENERGI_NET: case GLN_NORD_ENERGI_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TAC")), Set.of(NOTE_NET_TARIFF_C), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TAC")), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_NORDVESTJYSK_ELFORSYNING_NOE_NET: case GLN_NORDVESTJYSK_ELFORSYNING_NOE_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("Net C")), Set.of(NOTE_NET_TARIFF_C)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("Net C")), Set.of(NOTE_NET_TARIFF_C));
case GLN_RADIUS: case GLN_RADIUS:
@ -125,31 +124,31 @@ public class DatahubTariffFilterFactory {
DateQueryParameter.of(RADIUS_CUTOFF_DATE)); DateQueryParameter.of(RADIUS_CUTOFF_DATE));
case GLN_RAH_NET: case GLN_RAH_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("RAH-C")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("RAH-C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_RAVDEX: case GLN_RAVDEX:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-C")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_SUNDS_NET: case GLN_SUNDS_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("SEF-NT-05")), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("SEF-NT-05")),
Set.of(NOTE_NET_TARIFF_C_FLEX_HOUR), Set.of(NOTE_NET_TARIFF_C_FLEX_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_TARM_ELVAERK_NET: case GLN_TARM_ELVAERK_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TEV-NT-01")), Set.of(NOTE_NET_TARIFF_C)); return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TEV-NT-01")), Set.of(NOTE_NET_TARIFF_C));
case GLN_TREFOR_EL_NET: case GLN_TREFOR_EL_NET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("C")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("C")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_TREFOR_EL_NET_OEST: case GLN_TREFOR_EL_NET_OEST:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("46")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("46")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_VEKSEL: case GLN_VEKSEL:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-10")), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("NT-10")),
Set.of(NOTE_NET_TARIFF_C_HOUR + " NT-10")); Set.of(NOTE_NET_TARIFF_C_HOUR + " NT-10"));
case GLN_VORES_ELNET: case GLN_VORES_ELNET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT1009")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("TNT1009")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
case GLN_ZEANET: case GLN_ZEANET:
return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43110")), Set.of(NOTE_NET_TARIFF_C_HOUR), return new DatahubTariffFilter(Set.of(ChargeTypeCode.of("43110")), Set.of(NOTE_NET_TARIFF_C_HOUR),
DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, Duration.ofDays(-1))); DateQueryParameter.of(DateQueryParameterType.START_OF_DAY));
default: default:
return new DatahubTariffFilter(Set.of(), Set.of(NOTE_NET_TARIFF_C), return new DatahubTariffFilter(Set.of(), Set.of(NOTE_NET_TARIFF_C),
DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR)); DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR));

View File

@ -58,7 +58,7 @@ public class DateQueryParameter {
DateQueryParameterType dateType = this.dateType; DateQueryParameterType dateType = this.dateType;
if (dateType != null) { if (dateType != null) {
Duration offset = this.offset; Duration offset = this.offset;
if (offset == null) { if (offset == null || offset.isZero()) {
return dateType.toString(); return dateType.toString();
} else { } else {
return dateType.toString() return dateType.toString()
@ -84,6 +84,18 @@ public class DateQueryParameter {
} }
} }
public static DateQueryParameter of(DateQueryParameter parameter, Duration offset) {
DateQueryParameterType parameterType = parameter.dateType;
if (parameterType == null) {
return parameter;
}
Duration parameterOffset = parameter.offset;
if (parameterOffset == null || parameterOffset.isZero()) {
return of(parameterType, offset);
}
return of(parameterType, parameterOffset.plus(offset));
}
public static DateQueryParameter of(DateQueryParameterType dateType) { public static DateQueryParameter of(DateQueryParameterType dateType) {
return new DateQueryParameter(dateType); return new DateQueryParameter(dateType);
} }

View File

@ -12,6 +12,7 @@
*/ */
package org.openhab.binding.energidataservice.internal.config; package org.openhab.binding.energidataservice.internal.config;
import java.time.Duration;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.format.DateTimeParseException; import java.time.format.DateTimeParseException;
import java.util.Arrays; import java.util.Arrays;
@ -47,6 +48,11 @@ public class DatahubPriceConfiguration {
*/ */
public String start = ""; public String start = "";
/**
* Query start date offset expressed as an ISO 8601 duration.
*/
public String offset = "";
/** /**
* Check if any filter values are provided. * Check if any filter values are provided.
* *
@ -85,14 +91,22 @@ public class DatahubPriceConfiguration {
if (start.isBlank()) { if (start.isBlank()) {
return DateQueryParameter.EMPTY; return DateQueryParameter.EMPTY;
} }
Duration durationOffset = Duration.ZERO;
if (!offset.isBlank()) {
try {
durationOffset = Duration.parse(offset);
} catch (DateTimeParseException e) {
return null;
}
}
if (start.equals(DateQueryParameterType.START_OF_DAY.toString())) { if (start.equals(DateQueryParameterType.START_OF_DAY.toString())) {
return DateQueryParameter.of(DateQueryParameterType.START_OF_DAY); return DateQueryParameter.of(DateQueryParameterType.START_OF_DAY, durationOffset);
} }
if (start.equals(DateQueryParameterType.START_OF_MONTH.toString())) { if (start.equals(DateQueryParameterType.START_OF_MONTH.toString())) {
return DateQueryParameter.of(DateQueryParameterType.START_OF_MONTH); return DateQueryParameter.of(DateQueryParameterType.START_OF_MONTH, durationOffset);
} }
if (start.equals(DateQueryParameterType.START_OF_YEAR.toString())) { if (start.equals(DateQueryParameterType.START_OF_YEAR.toString())) {
return DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR); return DateQueryParameter.of(DateQueryParameterType.START_OF_YEAR, durationOffset);
} }
try { try {
return DateQueryParameter.of(LocalDate.parse(start)); return DateQueryParameter.of(LocalDate.parse(start));

View File

@ -246,7 +246,10 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
logger.debug("Cached net tariffs still valid, skipping download."); logger.debug("Cached net tariffs still valid, skipping download.");
cacheManager.updateNetTariffs(); cacheManager.updateNetTariffs();
} else { } else {
cacheManager.putNetTariffs(downloadPriceLists(config.getGridCompanyGLN(), getNetTariffFilter())); DatahubTariffFilter filter = getNetTariffFilter();
cacheManager.putNetTariffs(downloadPriceLists(config.getGridCompanyGLN(),
new DatahubTariffFilter(filter, DateQueryParameter.of(filter.getDateQueryParameter(),
Duration.ofHours(-CacheManager.NUMBER_OF_HISTORIC_HOURS)))));
} }
} }
@ -317,7 +320,8 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
DateQueryParameter start = datahubPriceConfiguration.getStart(); DateQueryParameter start = datahubPriceConfiguration.getStart();
if (start == null) { if (start == null) {
logger.warn("Invalid channel configuration parameter 'start': {}", datahubPriceConfiguration.start); logger.warn("Invalid channel configuration parameter 'start' or 'offset': {} (offset: {})",
datahubPriceConfiguration.start, datahubPriceConfiguration.offset);
return DatahubTariffFilterFactory.getNetTariffByGLN(config.gridCompanyGLN); return DatahubTariffFilterFactory.getNetTariffByGLN(config.gridCompanyGLN);
} }

View File

@ -89,6 +89,11 @@
</options> </options>
<advanced>true</advanced> <advanced>true</advanced>
</parameter> </parameter>
<parameter name="offset" type="text">
<label>Date Offset</label>
<description>Query start date offset expressed as an ISO 8601 duration.</description>
<advanced>true</advanced>
</parameter>
</config-description> </config-description>
</config-description:config-descriptions> </config-description:config-descriptions>

View File

@ -81,6 +81,8 @@ channel-type.config.energidataservice.datahub-price.chargeTypeCodes.label = Char
channel-type.config.energidataservice.datahub-price.chargeTypeCodes.description = Comma-separated list of charge type codes. channel-type.config.energidataservice.datahub-price.chargeTypeCodes.description = Comma-separated list of charge type codes.
channel-type.config.energidataservice.datahub-price.notes.label = Note Filters channel-type.config.energidataservice.datahub-price.notes.label = Note Filters
channel-type.config.energidataservice.datahub-price.notes.description = Comma-separated list of notes. channel-type.config.energidataservice.datahub-price.notes.description = Comma-separated list of notes.
channel-type.config.energidataservice.datahub-price.offset.label = Date Offset
channel-type.config.energidataservice.datahub-price.offset.description = Query start date offset expressed as an ISO 8601 duration.
channel-type.config.energidataservice.datahub-price.start.label = Query Start Date channel-type.config.energidataservice.datahub-price.start.label = Query Start Date
channel-type.config.energidataservice.datahub-price.start.description = Query start date parameter expressed as either YYYY-MM-DD or dynamically as one of StartOfDay, StartOfMonth or StartOfYear. channel-type.config.energidataservice.datahub-price.start.description = Query start date parameter expressed as either YYYY-MM-DD or dynamically as one of StartOfDay, StartOfMonth or StartOfYear.
channel-type.config.energidataservice.datahub-price.start.option.StartOfDay = Start of day channel-type.config.energidataservice.datahub-price.start.option.StartOfDay = Start of day