mampfes / ha_epex_spot

Adds EPEX Spot data to Home Assistant.
MIT License
130 stars 19 forks source link

get_lowest_price_interval error when latest end is tomorrow #54

Closed joergsti closed 10 months ago

joergsti commented 10 months ago

I want to get the cheapest three hours from now to 7AM tomorrow.

I understood I need to populate earliest_start in order to make latest_end work as tomorrow's time. However, this call creates an error (at least when executed before 14:00 when there are no values for tomorrow present):

service: epex_spot.get_lowest_price_interval
data:
  earliest_start: "09:07:00"
  latest_end: "07:00:00"
  duration:
    hours: 3
    minutes: 0
    seconds: 0
  device_id: MYDEVICEID

It would be great if the service calculates the best times even if no prices are available yet for tomorrow morning.

Error:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2012, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2049, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/epex_spot/__init__.py", line 104, in get_lowest_price_interval
    return _find_extreme_price_interval(call, lambda a, b: a < b)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/epex_spot/__init__.py", line 126, in _find_extreme_price_interval
    return coordinator.source.find_extreme_price_interval(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/epex_spot/SourceShell.py", line 152, in find_extreme_price_interval
    result = find_extreme_price_interval(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/epex_spot/extreme_price_interval.py", line 79, in find_extreme_price_interval
    ip = _calc_interval_price(marketdata, start_time, duration)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/epex_spot/extreme_price_interval.py", line 24, in _calc_interval_price
    if mp.end_time > stop_time:
       ^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'end_time'
mampfes commented 10 months ago

Please check the main branch if this solves your issue. The latest_end time is now automatically trimmed to the available market data range. Therefore is shouldn't crash anymore.