Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[energidataservice] Add UoM support for energy prices #16070

Merged
merged 1 commit into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions bundles/org.openhab.binding.energidataservice/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ It will not impact channels, see [Electricity Tax](#electricity-tax) for further

### Channel Group `electricity`

| Channel | Type | Description | Advanced |
|--------------------------|--------|----------------------------------------------------------------------------------------|----------|
| spot-price | Number | Current spot price in DKK or EUR per kWh | no |
| grid-tariff | Number | Current grid tariff in DKK per kWh. Only available when `gridCompanyGLN` is configured | no |
| system-tariff | Number | Current system tariff in DKK per kWh | no |
| transmission-grid-tariff | Number | Current transmission grid tariff in DKK per kWh | no |
| electricity-tax | Number | Current electricity tax in DKK per kWh | no |
| reduced-electricity-tax | Number | Current reduced electricity tax in DKK per kWh. For electric heating customers only | no |
| hourly-prices | String | JSON array with hourly prices from 24 hours ago and onward | yes |
| Channel | Type | Description | Advanced |
|--------------------------|--------------------|--------------------------------------------------------------------------------|----------|
| spot-price | Number:EnergyPrice | Spot price in DKK or EUR per kWh | no |
| grid-tariff | Number:EnergyPrice | Grid tariff in DKK per kWh. Only available when `gridCompanyGLN` is configured | no |
Comment on lines +52 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably add that - with the current implementations of CurrencyProvider - only one currency can be used.

Copy link
Contributor Author

@jlaur jlaur Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it seems I still don't fully understand even after your explanations in openhab/openhab-core#3503 (comment).

I ran some more extensive tests now:

State Provider Configuration Result
0.33126001 kr./kWh LocaleBasedCurrencyProvider Denmark 0.33126001 kr./kWh
0.33126001 DKK/kWh LocaleBasedCurrencyProvider Denmark java.lang.IllegalArgumentException: Invalid Quantity value: 0.33126001 DKK/kWh
0.33126001 kr./kWh LocaleBasedCurrencyProvider Sweden java.lang.IllegalArgumentException: Invalid Quantity value: 0.33126001 kr./kWh
0.33126001 kr./kWh FixedCurrencyProvider DKK 0.33126001 kr./kWh
0.33126001 kr./kWh FixedCurrencyProvider EUR 0.33126001 €./kWh
0.33126001 DKK/kWh FixedCurrencyProvider DKK java.lang.IllegalArgumentException: Invalid Quantity value: 0.33126001 DKK/kWh
0.044770 €/kWh FixedCurrencyProvider DKK java.lang.IllegalArgumentException: Invalid Quantity value: 0.044770 €/kWh
0.044770 €/kWh FixedCurrencyProvider EUR 0.044770 €/kWh

I have one channel which can be configured as either DKK or EUR, and all other channels are DKK. I guess this cannot work with UoM because it's not possible mix currencies? Also it seems that it's not possible to use any currency other than the configured one, although I'm a bit confused why case 3 and 5 have different results?

Perhaps you can advise how to avoid IllegalArgumentException? Can/should the binding check something before trying to update state with a specific currency that won't be accepted?

EDIT: @kaikreuzer - tagging you also since you have reviewed openhab/openhab-core#3503 - in case you'd be interested in this topic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@J-N-K - I don't know if you are receiving notifications without being tagged explicitly, so just to be sure, please see above when you find time.

| system-tariff | Number:EnergyPrice | System tariff in DKK per kWh | no |
| transmission-grid-tariff | Number:EnergyPrice | Transmission grid tariff in DKK per kWh | no |
| electricity-tax | Number:EnergyPrice | Electricity tax in DKK per kWh | no |
| reduced-electricity-tax | Number:EnergyPrice | Reduced electricity tax in DKK per kWh. For electric heating customers only | no |
| hourly-prices | String | JSON array with hourly prices from 24 hours ago and onward | yes |

_Please note:_ There is no channel providing the total price.
Instead, create a group item with `SUM` as aggregate function and add the individual price items as children.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
import org.openhab.binding.energidataservice.internal.retry.RetryPolicyFactory;
import org.openhab.binding.energidataservice.internal.retry.RetryStrategy;
import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.library.types.DecimalType;
import org.openhab.core.library.dimension.EnergyPrice;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.library.types.StringType;
import org.openhab.core.thing.Channel;
import org.openhab.core.thing.ChannelUID;
Expand Down Expand Up @@ -320,14 +321,19 @@ private void updateCurrentSpotPrice() {
return;
}
BigDecimal spotPrice = cacheManager.getSpotPrice();
updateState(CHANNEL_SPOT_PRICE, spotPrice != null ? new DecimalType(spotPrice) : UnDefType.UNDEF);
updateState(CHANNEL_SPOT_PRICE,
spotPrice != null ? getEnergyPrice(spotPrice, config.getCurrency()) : UnDefType.UNDEF);
}

private void updateCurrentTariff(String channelId, @Nullable BigDecimal tariff) {
if (!isLinked(channelId)) {
return;
}
updateState(channelId, tariff != null ? new DecimalType(tariff) : UnDefType.UNDEF);
updateState(channelId, tariff != null ? getEnergyPrice(tariff, CURRENCY_DKK) : UnDefType.UNDEF);
}

private QuantityType<EnergyPrice> getEnergyPrice(BigDecimal price, Currency currency) {
return new QuantityType<>(price + " " + currency.getSymbol() + "/kWh");
}

private void updateHourlyPrices() {
Expand Down Expand Up @@ -367,7 +373,7 @@ private void updateTimeSeries() {
for (Entry<Instant, BigDecimal> spotPrice : spotPrices) {
Instant hourStart = spotPrice.getKey();
if (isLinked(CHANNEL_SPOT_PRICE)) {
spotPriceTimeSeries.add(hourStart, new DecimalType(spotPrice.getValue()));
spotPriceTimeSeries.add(hourStart, getEnergyPrice(spotPrice.getValue(), config.getCurrency()));
}
for (Map.Entry<DatahubTariff, TimeSeries> entry : datahubTimeSeriesMap.entrySet()) {
DatahubTariff datahubTariff = entry.getKey();
Expand All @@ -378,7 +384,7 @@ private void updateTimeSeries() {
BigDecimal tariff = cacheManager.getTariff(datahubTariff, hourStart);
if (tariff != null) {
TimeSeries timeSeries = entry.getValue();
timeSeries.add(hourStart, new DecimalType(tariff));
timeSeries.add(hourStart, getEnergyPrice(tariff, CURRENCY_DKK));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
<channels>
<channel id="spot-price" typeId="spot-price">
<label>Spot Price</label>
<description>Current spot price in DKK or EUR per kWh.</description>
<description>Spot price in DKK or EUR per kWh.</description>
</channel>
<channel id="grid-tariff" typeId="datahub-price">
<label>Grid Tariff</label>
<description>Current grid tariff in DKK per kWh.</description>
<description>Grid tariff in DKK per kWh.</description>
</channel>
<channel id="system-tariff" typeId="datahub-price">
<label>System Tariff</label>
<description>Current system tariff in DKK per kWh.</description>
<description>System tariff in DKK per kWh.</description>
</channel>
<channel id="transmission-grid-tariff" typeId="datahub-price">
<label>Transmission Grid Tariff</label>
<description>Current transmission grid tariff in DKK per kWh.</description>
<description>Transmission grid tariff in DKK per kWh.</description>
</channel>
<channel id="electricity-tax" typeId="datahub-price">
<label>Electricity Tax</label>
<description>Current electricity tax in DKK per kWh.</description>
<description>Electricity tax in DKK per kWh.</description>
</channel>
<channel id="reduced-electricity-tax" typeId="datahub-price">
<label>Reduced Electricity Tax</label>
<description>Current reduced electricity tax in DKK per kWh. For electric heating customers only.</description>
<description>Reduced electricity tax in DKK per kWh. For electric heating customers only.</description>
</channel>
<channel id="hourly-prices" typeId="hourly-prices"/>
</channels>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
xsi:schemaLocation="https://openhab.org/schemas/thing-description/v1.0.0 https://openhab.org/schemas/thing-description-1.0.0.xsd">

<channel-type id="spot-price">
<item-type>Number</item-type>
<item-type>Number:EnergyPrice</item-type>
<label>Spot Price</label>
<description>Spot price.</description>
<category>Price</category>
<state readOnly="true" pattern="%.9f"></state>
<state readOnly="true" pattern="%.9f %unit%"></state>
</channel-type>

<channel-type id="datahub-price">
<item-type>Number</item-type>
<item-type>Number:EnergyPrice</item-type>
<label>Datahub Price</label>
<description>Datahub price.</description>
<category>Price</category>
<state readOnly="true" pattern="%.6f"></state>
<state readOnly="true" pattern="%.6f %unit%"></state>
<config-description-ref uri="channel-type:energidataservice:datahub-price"/>
</channel-type>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</channel-groups>

<properties>
<property name="thingTypeVersion">2</property>
<property name="thingTypeVersion">3</property>
</properties>

<config-description-ref uri="thing-type:energidataservice:service"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,39 @@
<remove-channel id="transmission-net-tariff" groupIds="electricity"/>
</instruction-set>

<instruction-set targetVersion="3">
<update-channel id="spot-price" groupIds="electricity">
<type>energidataservice:spot-price</type>
<label>Spot Price</label>
<description>Spot price in DKK or EUR per kWh.</description>
</update-channel>
<update-channel id="grid-tariff" groupIds="electricity">
<type>energidataservice:datahub-price</type>
<label>Grid Tariff</label>
<description>Grid tariff in DKK per kWh.</description>
</update-channel>
<update-channel id="system-tariff" groupIds="electricity">
<type>energidataservice:datahub-price</type>
<label>System Tariff</label>
<description>System tariff in DKK per kWh.</description>
</update-channel>
<update-channel id="transmission-grid-tariff" groupIds="electricity">
<type>energidataservice:datahub-price</type>
<label>Transmission Grid Tariff</label>
<description>Transmission grid tariff in DKK per kWh.</description>
</update-channel>
<update-channel id="electricity-tax" groupIds="electricity">
<type>energidataservice:datahub-price</type>
<label>Electricity Tax</label>
<description>Electricity tax in DKK per kWh.</description>
</update-channel>
<update-channel id="reduced-electricity-tax" groupIds="electricity">
<type>energidataservice:datahub-price</type>
<label>Reduced Electricity Tax</label>
<description>Reduced electricity tax in DKK per kWh. For electric heating customers only.</description>
</update-channel>
</instruction-set>

</thing-type>

</update:update-descriptions>