home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.31k stars 30.62k forks source link

Wrong cost field used in Tibber API #87810

Closed BartLH closed 1 year ago

BartLH commented 1 year ago

The problem

The Tibber integration uses a wrong API field to get cost data for its statistic.

This line https://github.com/home-assistant/core/blob/9dd806278b5f0355536b688134928207a7d33b72/homeassistant/components/tibber/sensor.py#L585 should use cost instead of totalCost. totalCost is officially deprecated. See also https://developer.tibber.com/docs/reference#consumption.

The issue (in addition to the fact that we shouldn't use deprecated API fields in principle 😉) is that totalCost produces an hourly cost that is about 2 cents lower than the actual cost reported in the app, which is how I came to investigate this. I have verified in the API explorer that cost produces the same values as those shown in the app and can be used a drop-in replacement.

What version of Home Assistant Core has the issue?

2023.2.3

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Tibber

Link to integration documentation on our website

https://www.home-assistant.io/integrations/tibber/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

home-assistant[bot] commented 1 year ago

Hey there @danielhiversen, mind taking a look at this issue as it has been labeled with an integration (tibber) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `tibber` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Change the title of the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign tibber` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


tibber documentation tibber source (message by IssueLinks)

Danielhiversen commented 1 year ago

So if you use this query at https://developer.tibber.com/, you get different values for cost and totalCost?

{
  viewer {
    homes {
      consumption(resolution: HOURLY, last: 100) {
        nodes {
          cost
          totalCost
        }
      }
    }
  }
}
BartLH commented 1 year ago

Correct!

A sample of the response:

{
  "data": {
    "viewer": {
      "homes": [
        {
          "consumption": {
            "nodes": [
              ...
              {
                "cost": 0.0179507856,
                "totalCost": -0.003772135530952381
              },
              {
                "cost": 0.0091696946,
                "totalCost": -0.012553226530952382
              },
              {
                "cost": 0.0088277728,
                "totalCost": -0.01289514833095238
              },
              {
                "cost": 0.1500721376,
                "totalCost": 0.12834921646904762
              },
              {
                "cost": 0.2319120969,
                "totalCost": 0.2101891757690476
              },
              {
                "cost": 0.0401928846,
                "totalCost": 0.01846996346904762
              },
              {
                "cost": 0.0583959068,
                "totalCost": 0.03667298566904762
              },
              ...
            ]
          }
        }
      ]
    }
  }
}

Note that the difference is a constant 2,1722921 cents, and that the totalCost can go negative due to this when prices are low.

ckarrie commented 1 year ago

Do you have a PV installed?