BottlecapDave / HomeAssistant-OctopusEnergy

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

Sensors created for old meters #35

Closed DazWorrall closed 2 years ago

DazWorrall commented 2 years ago

Describe the bug

Sensors are created for meters which have been replaced at the same property.

To Reproduce

Have a property which returns these kind of API responses 😅

Get account REST API response ```json { "number": "A-BCD123EF", "properties": [ { "id": 1234567, // redacted for brevity "electricity_meter_points": [ { "mpan": "0123456789", "profile_class": 1, "consumption_standard": 5720, "meters": [ { "serial_number": "XXXXXXXXX", "registers": [ { "identifier": "00", "rate": "STANDARD", "is_settlement_register": true } ] }, { "serial_number": "YYYYYYYYY", "registers": [ { "identifier": "1", "rate": "STANDARD", "is_settlement_register": true } ] } ], "agreements": [ { "tariff_code": "E-1R-SUPER-GREEN-24M-21-05-29-E", "valid_from": "2021-06-30T00:00:00+01:00", "valid_to": "2023-06-30T00:00:00+01:00" } ], "is_export": false } ], "gas_meter_points": [ { "mprn": "12345678", "consumption_standard": 47722, "meters": [ { "serial_number": "AAAAAAAAAAAAAA" }, { "serial_number": "BBBBBBBBBBBBBB" } ], "agreements": [ { "tariff_code": "G-1R-SUPER-GREEN-24M-21-05-29-E", "valid_from": "2021-06-30T00:00:00+01:00", "valid_to": "2023-06-30T00:00:00+01:00" } ] } ] } ] } ``` `YYYYYYYYY ` is the currently active electricity meter, and `AAAAAAAAAAAAAA` the gas - it doesn't look like the ordering matters, which is a shame as there's very little extra info for the gas mater in particular.

Consumption API for the old meters is completely blank:

{"count":0,"next":null,"previous":null,"results":[]}

Expected behavior

No sensors are created for the old meters.

Misc

Background here: moved into property with old style meters, which where shortly thereafter replaced by smart meters. Sensors are created for the new meters just fine, but also for the meters which were removed, and have no data.

This might be a bug in the rest API - the old meters don't show in the UI, and don't appear in the graphql API afaict:

Screenshot 2022-01-03 at 15 04 38

Using the REST API this might be tricky to handle cleanly because the sensors are created based on the data in the account API, which then go on to have no data in the consumption API but they have already been created at that point. I have disabled the entities for now so it's no big deal to me, but wanted to document the issue here while I had all the info in my head.

Thanks for creating this - I wanted to get some price sensors and started to look at building a small component based on the GraphQL api, this component saved me the effort 🙂

BottlecapDave commented 2 years ago

Thanks for raising this. I'll contact Octopus to see what they suggest.

BottlecapDave commented 2 years ago

Out of interest, are registers present against your old smart meters?

e.g.

{
    "properties": [
        {
            "electricity_meter_points": [
                {
                    "mpan": "XXX",
                    "meters": [
                        {
                            "serial_number": "XXX",
                            "registers": [
                                {
                                    ...
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

For instance I have one smart meter and one dumb smart meter, and one has a register (the smart meter) and one doesn't (the dumb smart meter) so was wondering if this could be used to determine meters that can be queried.

BottlecapDave commented 2 years ago

Ignore my last question - Just learning to read the complete bug report 🤦

DazWorrall commented 2 years ago

Just learning to read the complete bug report

😀 no worries.

Out of scope but: while moving to the GraphQL API to solve this issue would for sure be too big a hammer - and more work than it warrants - it might be worth a look in the longer term if only to reduce the number of API requests needed. All the account information, meter information, tariff details, and recent consumption could be retrieved in one round trip and atomically using GQL e.g.

Example GraphQL Query ```graphql query hassio_info($accountNumber: String!) { account(accountNumber: $accountNumber) { electricityAgreements { tariff { ... on StandardTariff { __typename displayName standingCharge unitRate productCode } } } gasAgreements { tariff { ... on GasTariffType { displayName standingCharge unitRate productCode } } } properties { electricityMeterPoints { meters { serialNumber activeFrom activeTo consumption(startAt: "2022-01-02T10:00:00Z", timezone: "UTC", first: 10, grouping: HALF_HOUR) { edges { node { value startAt endAt } } } } } gasMeterPoints { meters { serialNumber activeFrom activeTo consumption(startAt: "2022-01-02T10:00:00Z", timezone: "UTC", first: 10, grouping: HALF_HOUR) { edges { node { value startAt endAt } } } } } } } } ```

The auth is a little trickier with GQL though.

BottlecapDave commented 2 years ago

Yeah I have some experience with GraphQL (in other languages) and something I've been thinking especially if they're not going to keep feature parity with the REST API.

BottlecapDave commented 2 years ago

I've had a response back from Octopus Energy. They asked if you could email matt.porter@octoenergy.com with the subject "ref REST API - old meters" with your account information so they can look into it. Hopefully then we'll get an update to the API.

DazWorrall commented 2 years ago

Emails sent, thanks for following up @BottlecapDave.

BottlecapDave commented 2 years ago

As they're taking their sweet time, I've decided to take a look at the graphql endpoint. I'm quite busy at work at the moment, but will take a look within the next couple of weeks.

Before I do though, I need to add some more unit/integration tests first to make sure I don't break anything :)

BettySwallocks commented 2 years ago

Dunno if anyone got anywhere with Octopus about cleaning up the UI and data responses, but I can confim I have this same issue. Had my old meters replaced by Octopus around 6-7 weeks ago and that prompted me to look at options to get data from either the Chameleon monitor they gave me or from CT clamp or EM unit. In the end neither were options without spending, so I chose this limited data integration. I got data very easily after registration and API key entry into the integration, but I see entries for my old electricity and gas meters as well as the new smart meters. Thanks for the great work though - hope this development can continue.

BottlecapDave commented 2 years ago

Hello. I've switched the account information across to use the graphql endpoint, which I hope will rectify this. This should now be available on the latest release (4.3.0). When upgrading, the defunct meters should become orphaned and then you can manually remove them.

BettySwallocks commented 2 years ago

Hasn't made a change that I can see. The new version is loaded, several restarts later and I still have the same seven entities for my old meters that the system won't allow me to remove. It's low priority though as the true meter data has been flowing fine and I can work around this.

BottlecapDave commented 2 years ago

Sorry to hear this hasn't worked. I'll try and think of a solution on my end...

BottlecapDave commented 2 years ago

I've just created a new release (https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/releases/tag/v4.4.0) which includes two new options that will hopefully help with working out why this isn't working

Device diagnostics

The first is the ability to download diagnostics for your device. This will include some information about your octopus energy account with confidential information redacted. You can do this by

  1. Navigate to http://bottlecapha.local:8123/config/devices/dashboard and search octopus energy
  2. Click on either meter and you should have the option of Download Diagnostics
  3. Look at the JSON for anything else you don't want to include regarding your set up and post the contents here

Raw Account Data

The second ability is to set your log level for the integration to debug using the following in your configuration.yaml

logger:
  default: error
  logs:
    custom_components.octopus_energy: debug

Once you've restarted your application, the raw information for your account will be present in your full home assistant logs (Navigate to http://bottlecapha.local:8123/config/logs and click load full logs). This will have the following present [custom_components.octopus_energy.api_client] {'data':

If you take this information, redact the identifying information and post it here.

Hopefully one of these options will help point me in the right direction.

DazWorrall commented 2 years ago

Sorry for the belated reply from me, my notifications for this thread did not reach my inbox :/

Running 4.4.0, I no longer see the duplicate meters, and confirmed in the diagnostic JSON payload that only 1 meter was listed for both gas and electricity. I did have to remove the stale entities for the old meters manually.

Thanks for your help here.

BottlecapDave commented 2 years ago

Glad to hear. Closing the issue as this appears to be fixed.