Danielhiversen / pyTibber

Async Python 3 library for Tibber
GNU General Public License v3.0
87 stars 27 forks source link

Wrong calculation of energy production & consuming #270

Open jovana opened 1 year ago

jovana commented 1 year ago

Hi,

We have some issues with the HA integration. But the problem seems to be in the pyTibber. (this is the original issue: https://github.com/home-assistant/core/issues/92859)

I think the problem is the consuming energy and the production energy are added together. Probably this is why the readings are not correct.

Probably the issue is on this line: https://github.com/Danielhiversen/pyTibber/blob/35dcd185d009b213d6aa46fa4a1a863e5237246d/tibber/tibber_home.py#L104

Running this example code to reproduce:

import tibber
import asyncio

access_token = "API_KEY" 
tibber_connection = tibber.Tibber(access_token, user_agent="HA")

async def home_data():
    home = tibber_connection.get_homes()[0]
    await home.fetch_consumption_data()
    await home.update_info()
    await home.update_price_info()
    print(home.month_cost)

async def start():
    await tibber_connection.update_info()
    await home_data()
    await tibber_connection.close_connection()

loop = asyncio.get_event_loop()
loop.run_until_complete(start())

This gives me: 30.86

Running the below code on https://developer.tibber.com/explorer

{
  viewer {
    homes {
      timeZone
      consumption(resolution: MONTHLY, first:2) {
        nodes {
          from
          to
          cost
          unitPrice
          unitPriceVAT
          consumption
          consumptionUnit
        }
      }

      production(resolution:MONTHLY, first:2) {
        nodes {
          from
          to          
          unitPrice
          unitPriceVAT
          production
          productionUnit
        }
      }
    }
  }
}

This gives me:


{
  "data": {
    "viewer": {
      "homes": [
        {
          "timeZone": "Europe/Amsterdam",
          "consumption": {
            "nodes": [
              {
                "from": "2023-05-01T00:00:00.000+02:00",
                "to": "2023-06-01T00:00:00.000+02:00",
                "cost": 1.7283521057,
                "unitPrice": 0.266474,
                "unitPriceVAT": 0.046248,
                "consumption": 6.486,
                "consumptionUnit": "kWh"
              },
              {
                "from": "2023-06-01T00:00:00.000+02:00",
                "to": "2023-07-01T00:00:00.000+02:00",
                "cost": 9.8557098475,
                "unitPrice": 0.234872,
                "unitPriceVAT": 0.040763,
                "consumption": 41.962,
                "consumptionUnit": "kWh"
              }
            ]
          },
          "production": {
            "nodes": [
              {
                "from": "2023-05-01T00:00:00.000+02:00",
                "to": "2023-06-01T00:00:00.000+02:00",
                "unitPrice": 0.229277,
                "unitPriceVAT": 0.039792,
                "production": 7.274,
                "productionUnit": "kWh"
              },
              {
                "from": "2023-06-01T00:00:00.000+02:00",
                "to": "2023-07-01T00:00:00.000+02:00",
                "unitPrice": 0.239023,
                "unitPriceVAT": 0.041483,
                "production": 46.494,
                "productionUnit": "kWh"
              }
            ]
          }
        }
      ]
    }
  }
}

Please let me know if you need more information from me.

Danielhiversen commented 1 year ago

What is wrong with this line?

if hourly_data.is_production:

jovana commented 1 year ago

What is wrong with this line?

if hourly_data.is_production:

Hi, I meant "it's starting from this line" (again I think). The calculation is not correct, and I have tried to find out what is going wrong, only try to help you trace the issue.

Maddimax commented 1 year ago

The actual issue from what I can tell is that in https://github.com/Danielhiversen/pyTibber/blob/35dcd185d009b213d6aa46fa4a1a863e5237246d/tibber/tibber_home.py#L484-L487 the logic is wrong ifhasPreviousPage is false. So instead of getting previous data, it just gets the same data again.

Also it does not set cursor (line 497 is not reached). I've reproduced this with the first example from the README.md with pyTibber installed via pip3 install pyTibber

P.S: I'm not sure what the code should do in that case, otherwise I'd create a pull request for it myself.

jovana commented 1 year ago

Cool that you have found the issue. Can we make a pull request to fix this @Danielhiversen ?

Maddimax commented 1 year ago

By the way: this only impacts new Tibber customers that joined Tibber less than 60 days ago, as the fetch_data function always tries to get 60 days worth of data.

Maddimax commented 1 year ago

@Danielhiversen has made another PR that also fixes part of the problem: https://github.com/Danielhiversen/pyTibber/pull/273

Its still getting a different (too low) montly cost for me than the app. It looks like the value the app uses is in the "totalCost" field of the "node"'s returned by get_historic_data(), not "cost". Even though the API Docs say that the totalCost field should not be used but the cost field.