BottlecapDave / HomeAssistant-OctopusEnergy

Unofficial Home Assistant integration for interacting with Octopus Energy
https://bottlecapdave.github.io/HomeAssistant-OctopusEnergy/
MIT License
564 stars 55 forks source link

Gives "unsafe" rate when octopus fails to return valid data. #77

Closed jacekowski closed 1 year ago

jacekowski commented 1 year ago

Octopus seems to have an issue today and agile has disappeared from the API (returns "not found"). When this happens current rate is set to 0.0, which in case of a lot of price based automations can cause high electricity usage when electricity prices are high.

Ideally previous reply should be used when invalid reply is returned (should be valid for 24hrs+)

BottlecapDave commented 1 year ago

Hello, and sorry to hear that you are having issues. I will look into the issue. I feel that leaving it as the previously known value could cause similar issues as it too could cause automations to occur at the wrong time.

jacekowski commented 1 year ago

I was thinking more about a situation where if you get data from for example https://api.octopus.energy/v1/products/AGILE-22-08-31/electricity-tariffs/E-1R-AGILE-22-08-31-L/standard-unit-rates/ (previous agile tariff that for some reason still works) without period_from and period_to parameter it has rates for the whole day (and possibly next day if after ~16:00) that will not change once released by octopus, so can be cached for the whole day.

Also, reporting sensor as unavailable instead of "0" is also going to be a lot safer.

BottlecapDave commented 1 year ago

I was thinking more about a situation where if you get data from for example https://api.octopus.energy/v1/products/AGILE-22-08-31/electricity-tariffs/E-1R-AGILE-22-08-31-L/standard-unit-rates/ (previous agile tariff that for some reason still works) without period_from and period_to parameter it has rates for the whole day (and possibly next day if after ~16:00) that will not change once released by octopus, so can be cached for the whole day.

Currently if we are unable to retrieve the rates, it should bomb out and not update the rates that are cached as an exception should be raised. You should see Failed to extract standard rates in your logs. We attempt to retrieve rates for the next two days (including today) every 30 minutes (as it's my understanding that agile rates could change (e.g. they could get lower due to excess renewables). Do you know at what time the API was reporting this issue?

Also, reporting sensor as unavailable instead of "0" is also going to be a lot safer. Agreed that it should be reporting as none or unknown

jacekowski commented 1 year ago

Unfortunately it does not bomb out (i've added few extra logging bits to api_client.py so i have few more log messages than standard code)

current agile tariff is still missing from the API for some reason (requests for data for previous agile tariff are ok and return rates, but those are obviously not useful for someone on current version), it was working correctly up until 14:00 today and then started giving me 0's

     data = await self.__async_read_response(response, url, { "results": [] })
      _LOGGER.debug(data)
      results = rates_to_thirty_minute_increments(data, period_from, period_to, tariff_code)
      _LOGGER.debug("rates")
      _LOGGER.debug(results)

2022-10-22 19:11:46.371 DEBUG (MainThread) [custom_components.octopus_energy.api_client] {'data': {'account': {'electricityAgreements': [{'meterPoint': {'mpan': 'censored', 'meters': [{'serialNumber': 'censored', 'smartExportElectricityMeter': None, 'smartImportElectricityMeter': {'deviceId': 'censored'}}], 'agreements': [{'validFrom': '2022-10-21T23:00:00+00:00', 'validTo': None, 'tariff': {'tariffCode': 'E-1R-AGILE-VAR-22-10-19-L'}}]}}], 'gasAgreements': [{'meterPoint': {'mprn': 'censored', 'meters': [{'serialNumber': ''}], 'agreements': [{'validFrom': '2022-10-12T23:00:00+00:00', 'validTo': None, 'tariff': {'tariffCode': 'G-1R-VAR-22-10-01-L'}}]}}]}}} 2022-10-22 19:11:46.387 INFO (MainThread) [custom_components.octopus_energy] tariff_codes: {('censored', True): 'E-1R-AGILE-VAR-22-10-19-L'} 2022-10-22 19:11:46.762 DEBUG (MainThread) [custom_components.octopus_energy.api_client] {'detail': 'Not found.'} 2022-10-22 19:11:46.762 DEBUG (MainThread) [custom_components.octopus_energy.api_client] rates 2022-10-22 19:11:46.763 DEBUG (MainThread) [custom_components.octopus_energy.api_client] [] 2022-10-22 19:11:46.763 DEBUG (MainThread) [custom_components.octopus_energy] Finished fetching rates data in 2.717 seconds (success: True) 2022-10-22 19:11:47.034 DEBUG (MainThread) [custom_components.octopus_energy.api_client] {'data': {'account': {'electricityAgreements': [{'meterPoint': {'mpan': 'censored', 'meters': [{'serialNumber': 'censored', 'smartExportElectricityMeter': None, 'smartImportElectricityMeter': {'deviceId': 'censored'}}], 'agreements': [{'validFrom': '2022-10-21T23:00:00+00:00', 'validTo': None, 'tariff': {'tariffCode': 'E-1R-AGILE-VAR-22-10-19-L'}}]}}], 'gasAgreements': [{'meterPoint': {'mprn': 'censored', 'meters': [{'serialNumber': ''}], 'agreements': [{'validFrom': '2022-10-12T23:00:00+00:00', 'validTo': None, 'tariff': {'tariffCode': 'G-1R-VAR-22-10-01-L'}}]}}]}}} 2022-10-22 19:11:47.034 INFO (MainThread) [custom_components.octopus_energy.sensor] Adding electricity meter; mpan: censored; serial number: censored 2022-10-22 19:11:47.034 INFO (MainThread) [custom_components.octopus_energy.sensor] Adding gas meter; mprn: censored; serial number: 2022-10-22 19:11:47.035 INFO (MainThread) [custom_components.octopus_energy.sensor] Updating OctopusEnergyGasCurrentRate 2022-10-22 19:11:47.049 DEBUG (MainThread) [custom_components.octopus_energy] Finished fetching rates data in 0.000 seconds (success: True) 2022-10-22 19:11:47.049 INFO (MainThread) [custom_components.octopus_energy.sensor] Updating OctopusEnergyElectricityPreviousRate for 'censored/censored' 2022-10-22 19:11:47.054 INFO (MainThread) [custom_components.octopus_energy.sensor] Updating OctopusEnergyElectricityCurrentRate for 'censored/censored' 2022-10-22 19:11:47.272 DEBUG (MainThread) [custom_components.octopus_energy.sensor] Finished fetching rates data in 0.223 seconds (success: True) 2022-10-22 19:11:47.334 DEBUG (MainThread) [custom_components.octopus_energy.sensor] Finished fetching rates data in 0.286 seconds (success: True) 2022-10-22 19:11:47.335 INFO (MainThread) [custom_components.octopus_energy.sensor] Calculated previous electricity consumption for 'censored/censored'...

BottlecapDave commented 1 year ago

Ah thanks for that - I now see what's happening. I'll try and get a fix out as soon as possible.

BottlecapDave commented 1 year ago

A new release (https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/releases/tag/v4.6.2) will hopefully resolve this issue

github-actions[bot] commented 1 year ago

This issue has become stale because it has been open for 30 days with no activity. If you still think it's an issue, please respond soon.

BottlecapDave commented 1 year ago

@jacekowski did this release fix your issue?

jacekowski commented 1 year ago

I've not observed the issue again (not sure if it is because the API has not returned bad data since or is it because of the changes).

BottlecapDave commented 1 year ago

Ok no problem. I'll close for now just let me know if you see it again.