MattJeanes / TeslaMateAgile

Integration to automatically fill in prices for charge data captured by TeslaMate for smart energy providers
MIT License
75 stars 9 forks source link

Support for Tibber #12

Closed MattJeanes closed 3 years ago

MattJeanes commented 3 years ago

Tibber is an energy provider much like Octopus Energy with a dynamic tariff and a public API to get pricing - this issue is to track the addition of Tibber support to TeslaMateAgile and more generally support for multiple energy providers.

They use a GraphQL API documented here: https://developer.tibber.com/explorer

I have discovered that we can request price info for arbitrary date ranges by using the start and end cursors on the range, which is a base64 encoded ISO 8601 string. For example, the following gets the last 24 hours from before 6am (+1 UTC) on the 1st January 2020:

The ISO 8601 date (2020-01-01T06:00:00+01:00) base64 encoded is MjAyMC0wMS0wMVQwNjowMDowMCswMTowMA==

{
  viewer {
    homes {
      currentSubscription{
        priceInfo{
          range(resolution: HOURLY, last: 24, before: "MjAyMC0wMS0wMVQwNjowMDowMCswMTowMA==") {
            pageInfo {
              endCursor,
              startCursor
            }
            nodes {
              total
              energy
              tax
              startsAt
              level
            }
          }
        }
      }
    }
  }
}
tobiasehlert commented 3 years ago

Looking forward to this! =D

MattJeanes commented 3 years ago

This has been implemented in v1.4.0-rc.3 - @tobiasehlert can you try it out? I've updated the README with info on how to use the new integration.

You'll need to either pull the latest tag or explicitly use the version above on docker. Let me know how it goes 😄

If it's all looking good, I'll promote it to a stable build and push this out as v.1.4.0 - I have tested a charge I completed just now under both Octopus and Tibber integrations (albeit with their demo key) and so far so good!

tobiasehlert commented 3 years ago

Hi @MattJeanes,

Awesome!

Just pulled the code (with latest) and looks like it's working, even thou there are some warnings did show up:

teslamateagile_1  | info: TeslaMateAgile.PriceService[0]
teslamateagile_1  |       Price service is starting
teslamateagile_1  | info: TeslaMateAgile.PriceService[0]
teslamateagile_1  |       Using energy provider Tibber
teslamateagile_1  | info: Microsoft.Hosting.Lifetime[0]
teslamateagile_1  |       Application started. Press Ctrl+C to shut down.
teslamateagile_1  | info: Microsoft.Hosting.Lifetime[0]
teslamateagile_1  |       Hosting environment: Production
teslamateagile_1  | info: Microsoft.Hosting.Lifetime[0]
teslamateagile_1  |       Content root path: /app/
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Updating prices
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 16.38 and energy 40.69 kWh for charging process 190
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 12.47 and energy 27.22 kWh for charging process 191
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 0.01 and energy 0.02 kWh for charging process 192
teslamateagile_1  | warn: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Mismatch between TeslaMate calculated energy used of 0.00 and ours of 0.02
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 0.03 and energy 0.03 kWh for charging process 193
teslamateagile_1  | warn: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Mismatch between TeslaMate calculated energy used of 0.00 and ours of 0.03
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 6.06 and energy 13.64 kWh for charging process 195
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 0.18 and energy 0.22 kWh for charging process 196
teslamateagile_1  | warn: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Mismatch between TeslaMate calculated energy used of 0.37 and ours of 0.22
teslamateagile_1  | info: TeslaMateAgile.PriceHelper[0]
teslamateagile_1  |       Calculated cost 0.01 and energy 0.02 kWh for charging process 197

It would be nice to see the rate, that it used when calculating the pricing for each charging process.

One thing that I can note is that Tibber itself calculated my charge of the car to be 190 SEK. Based on TeslaMate data and your calculation of historical data, it came together to 196.78 SEK. I don't know if it's worth checking into this :)

Regards, Tobias

tobiasehlert commented 3 years ago

My addition to the docker-compose.yml file was this:

  teslamateagile:
    image: mattjeanes/teslamateagile:latest
    restart: always
    environment:
      - DATABASE_USER=${TM_DB_USER}
      - DATABASE_PASS=${TM_DB_PASS}
      - DATABASE_NAME=${TM_DB_NAME}
      - DATABASE_HOST=database
      - TeslaMate__UpdateIntervalSeconds=300
      - TeslaMate__GeofenceId=50
      - TeslaMate__Phases=1
      - TeslaMate__EnergyProvider=Tibber
      - Tibber__AccessToken=${TIBBER_TOKEN}

Nice to have config params based on the .env file instead.. where I added my TIBBER_TOKEN as well.

MattJeanes commented 3 years ago

There will always be some discrepancies on the readings as they'll be calculated completely differently between TeslaMate and your energy provider but it should be pretty close. That said, I have found a bug which will result in lost charge data when calculating the cost. I've fixed this and added extra checks and debug logging in v1.4.0-rc.4.

To enable debug logging, add an environment variable:

- Logging__LogLevel__Default=Debug

You will see logging like this, should help diagnose any issues image

Do you know how to reset your costs to NULL in the TeslaMate database to get it to re-calculate them? I've added some information to the README on how to do this.

Hopefully there won't be any errors and the calculated values should be more accurate now, let me know!

tobiasehlert commented 3 years ago

Hi @MattJeanes,

Added the debug parameter and pulled latest image. Looks good to me 👍

Did also reset the costs to NULL and it made a recalculation of my 180 charges.

Thanks man!

MattJeanes commented 3 years ago

Perfect!

I've now released this as part of v1.4.0, so I will close this issue - thank you for helping me to test this.