Add UoM support for energy prices (#16070)

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2023-12-16 17:29:06 +01:00 committed by GitHub
parent 0228f43004
commit 413ce0898c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 25 deletions

View File

@ -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 |
| 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.

View File

@ -56,7 +56,8 @@ import org.openhab.binding.energidataservice.internal.exception.DataServiceExcep
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;
@ -320,14 +321,19 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
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() {
@ -367,7 +373,7 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
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();
@ -378,7 +384,7 @@ public class EnergiDataServiceHandler extends BaseThingHandler {
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));
}
}
}

View File

@ -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>

View File

@ -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>

View File

@ -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"/>

View File

@ -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>