Skip to content

Commit

Permalink
add median sensor
Browse files Browse the repository at this point in the history
fix #39
  • Loading branch information
mampfes committed Sep 16, 2023
1 parent 3edf57d commit 47f65e4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ This integration provides the following sensors:
1. Net market price
2. Market price
3. Average market price during the day
4. Lowest market price during the day
5. Highest market price during the day
6. Current market price quantile during the day
7. Rank of the current market price during the day
4. Median market price during the day
5. Lowest market price during the day
6. Highest market price during the day
7. Current market price quantile during the day
8. Rank of the current market price during the day

The *EPEX Spot Web Scraper* provides some additional sensors:

Expand Down Expand Up @@ -126,7 +127,15 @@ The sensor value reports the average EPEX Spot market price during the day. The
price_ct_per_kwh: 29.63
```

### 4. Lowest Market Price Sensor
### 4. Median Market Price Sensor

The sensor value reports the median EPEX Spot market price during the day. The sensor value reports the market price in EUR/MWh. The market price in ct/kWh is available as sensor attribute.

```yaml
price_ct_per_kwh: 29.63
```

### 5. Lowest Market Price Sensor

The sensor value reports the lowest EPEX Spot market price during the day. The sensor value reports the market price in EUR/MWh. The market price in ct/kWh is available as sensor attribute.

Expand All @@ -138,7 +147,7 @@ start_time: '2023-02-15T22:00:00+00:00'
end_time: '2023-02-15T23:00:00+00:00'
```

### 5. Highest Market Price Sensor
### 6. Highest Market Price Sensor

The sensor value reports the highest EPEX Spot market price during the day. The sensor value reports the market price in EUR/MWh. The market price in ct/kWh is available as sensor attribute.

Expand All @@ -150,7 +159,7 @@ start_time: '2023-02-15T22:00:00+00:00'
end_time: '2023-02-15T23:00:00+00:00'
```

### 6. Quantile Sensor
### 7. Quantile Sensor

The sensor value reports the quantile between the lowest market price and the highest market price during the day in the range between 0 .. 1.

Expand All @@ -160,7 +169,7 @@ Examples:
- The sensor reports 1 if the current market price is the highest during the day.
- If the sensor reports e.g., 0.25, then the current market price is 25% of the range between the lowest and the highest market price.

### 7. Rank Sensor
### 8. Rank Sensor

The sensor value reports the rank of the current market price during the day. Or in other words: The number of hours in which the price is lower than the current price.

Expand Down
23 changes: 23 additions & 0 deletions custom_components/epex_spot/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from statistics import median

from homeassistant.components.sensor import SensorEntity
from homeassistant.const import (ATTR_IDENTIFIERS, ATTR_MANUFACTURER,
Expand Down Expand Up @@ -36,6 +37,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
EpexSpotLowestPriceSensorEntity(hass, shell.get_source(unique_id)),
EpexSpotHighestPriceSensorEntity(hass, shell.get_source(unique_id)),
EpexSpotAveragePriceSensorEntity(hass, shell.get_source(unique_id)),
EpexSpotMedianPriceSensorEntity(hass, shell.get_source(unique_id)),
]

if config_entry.data[CONF_SOURCE] == CONF_SOURCE_EPEX_SPOT_WEB:
Expand Down Expand Up @@ -337,3 +339,24 @@ def _on_update_sensor(self):
ATTR_PRICE_CT_PER_KWH: to_ct_per_kwh(self._attr_native_value),
}
self._attr_extra_state_attributes = attributes


class EpexSpotMedianPriceSensorEntity(EpexSpotSensorEntity):
"""Home Assistant sensor containing all EPEX spot data."""

def __init__(self, hass, source):
EpexSpotSensorEntity.__init__(self, hass, source)
self._attr_unique_id = f"{source.unique_id} Median Price"
self._attr_name = f"EPEX Spot {source.market_area} Median Price"
self._attr_icon = "mdi:currency-eur"
self._attr_native_unit_of_measurement = "EUR/MWh"
self._attr_suggested_display_precision = 2

def _on_update_sensor(self):
"""Update the value of the entity."""
self._attr_native_value = median([e.price_eur_per_mwh for e in self._source.sorted_marketdata_today])

attributes = {
ATTR_PRICE_CT_PER_KWH: to_ct_per_kwh(self._attr_native_value),
}
self._attr_extra_state_attributes = attributes

0 comments on commit 47f65e4

Please sign in to comment.