Skip to content

Commit

Permalink
Add property to run energy consumers at the best time (#173)
Browse files Browse the repository at this point in the history
* Add energy on best time property

* Update example and readme
  • Loading branch information
klaasnicolaas authored Aug 9, 2023
1 parent 352ebeb commit 618cf04
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The energy prices are different every hour, after 15:00 (more usually already at
- Time of highest price (datetime)
- Time of lowest price (datetime)
- Percentage of the current price compared to the maximum price
- Number of hours with the current price or lower (int)

### Gas prices

Expand Down
24 changes: 13 additions & 11 deletions examples/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ async def main() -> None:
"""Show example on fetching the energy prices from EnergyZero."""
async with EnergyZero() as client:
local = pytz.timezone("CET")
today = date(2023, 3, 29)
tomorrow = date(2023, 3, 29)
today = date(2023, 8, 9)
tomorrow = date(2023, 8, 9)

energy_today = await client.energy_prices(start_date=today, end_date=today)
energy_tomorrow = await client.energy_prices(
Expand All @@ -22,10 +22,10 @@ async def main() -> None:
)

print("--- ENERGY TODAY ---")
print(f"Max price: {energy_today.extreme_prices[1]}")
print(f"Min price: {energy_today.extreme_prices[0]}")
print(f"Average price: {energy_today.average_price}")
print(f"Percentage: {energy_today.pct_of_max_price}")
print(f"Max price: {energy_today.extreme_prices[1]}")
print(f"Min price: {energy_today.extreme_prices[0]}")
print(f"Average price: {energy_today.average_price}")
print(f"Percentage: {energy_today.pct_of_max_price}%")
print()
print(
f"High time: {energy_today.highest_price_time.astimezone(local)}",
Expand All @@ -34,15 +34,17 @@ async def main() -> None:
f"Lowest time: {energy_today.lowest_price_time.astimezone(local)}",
)
print()
print(f"Current hourprice: {energy_today.current_price}")
print(f"Current hourprice: {energy_today.current_price}")
next_hour = energy_today.utcnow() + timedelta(hours=1)
print(f"Next hourprice: {energy_today.price_at_time(next_hour)}")
print(f"Next hourprice: €{energy_today.price_at_time(next_hour)}")
best_hours = energy_today.hours_priced_equal_or_lower
print(f"Hours lower or equal than current price: {best_hours}")

print()
print("--- ENERGY TOMORROW ---")
print(f"Max price: {energy_tomorrow.extreme_prices[1]}")
print(f"Min price: {energy_tomorrow.extreme_prices[0]}")
print(f"Average price: {energy_tomorrow.average_price}")
print(f"Max price: {energy_tomorrow.extreme_prices[1]}")
print(f"Min price: {energy_tomorrow.extreme_prices[0]}")
print(f"Average price: {energy_tomorrow.average_price}")
print()
time_high = energy_tomorrow.highest_price_time.astimezone(local)
print(f"Highest price time: {time_high}")
Expand Down
11 changes: 11 additions & 0 deletions src/energyzero/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,17 @@ def timestamp_prices(self) -> list[dict[str, float | datetime]]:
"""
return self.generate_timestamp_list(self.prices)

@property
def hours_priced_equal_or_lower(self) -> int:
"""Return the number of hours with prices equal or lower than the current price.
Returns
-------
The number of hours with prices equal or lower than the current price.
"""
current: float = self.current_price or 0
return sum(price <= current for price in self.prices.values())

def utcnow(self) -> datetime:
"""Return the current timestamp in the UTC timezone.
Expand Down
1 change: 1 addition & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async def test_electricity_model(aresponses: ResponsesMockServer) -> None:
).replace(tzinfo=timezone.utc)
assert energy.pct_of_max_price == 87.27
assert isinstance(energy.timestamp_prices, list)
assert energy.hours_priced_equal_or_lower == 23


async def test_electricity_none_date(aresponses: ResponsesMockServer) -> None:
Expand Down

0 comments on commit 618cf04

Please sign in to comment.