Tibber-Auswertung in python

Ich habe einen Tibber-Stromvertrag und möchte mit python eine kleine Statistik anzeigen, einfach weil es eine schöne Bibliothek gibt.
Die Daten, die von Tibber kommen sehen so aus:

{
  "data": {
    "viewer": {
      "homes": [
        {
          "consumption": {
            "nodes": [
              {
                "from": "2024-04-07T13:00:00.000+02:00",
                "to": "2024-04-07T14:00:00.000+02:00",
                "cost": 0.005811246,
                "unitPrice": 0.1291388,
                "unitPriceVAT": 0.0206188,
                "consumption": 0.045,
                "consumptionUnit": "kWh"
              },
              {
                "from": "2024-04-07T14:00:00.000+02:00",
                "to": "2024-04-07T15:00:00.000+02:00",
                "cost": 0.0097374725,
                "unitPrice": 0.1371475,
                "unitPriceVAT": 0.0218975,
                "consumption": 0.071,
                "consumptionUnit": "kWh"
              }
            ]
          }
        }
      ]
    }
  }
}

So prinzipiell klappt das auch schon ganz gut:

#!/usr/bin/env python3

import tibber
from datetime import datetime
from itertools import groupby


account = tibber.Account('Mein geheimes Tibber Token')
home = account.homes[0]



data = home.fetch_consumption("HOURLY", last = 1000000)
data = [h for h in data if h.consumption ]


def print_monthly_statistics(entries:list)->None:
  av_price =  sum(hour.unit_price for hour in entries) / len([h for h in entries]) 
  cost = sum(hour.cost for hour in entries)
  consumption = sum(hour.consumption for hour in entries)
  av_cost = consumption * av_price

  print (f"            Durchschnittspreis: {av_price:4.4f} ct/kWh\n" +
         f"gewichteter Durchschnittspreis: {cost/consumption:4.4f} ct/kWh\n" +
         f"                       bezahlt: {cost:4.4f} € für {consumption:4.4f} kWh\n"
         f"                       gespart: {av_cost - cost:4.4f} € ({100*(av_cost - cost)/av_cost:4.4f}%)"
        )   

print_monthly_statistics(data)

Ausgabe:

            Durchschnittspreis: 0.2347 ct/kWh
gewichteter Durchschnittspreis: 0.2229 ct/kWh
                       bezahlt: 306.5412 € für 1375.4350 kWh
                       gespart: 16.3131 € (5.0528%)

Jetzt möchte ich diese Ausgabe monatsweise haben, also versuche ich, die Liste in eine Map zu wandeln:

def order_key(entry: str)->str:
  print(f"entry: {entry}")
  startDate = datetime.strptime(entry, '%y-%m')
  return startDate.year, startDate.month


consumtionsPerMonth = groupby(data, key=lambda hour: order_key(getattr(hour,'from')))

nur bekomme ich dann eine Fehlermeldung:

Traceback (most recent call last):
  File "/home/thomas/Dokumente/Haus/E-Auto/OWB/update/consumption_log/./calculate_waighted_average.py", line 41, in <module>
    for key, consumtionInMonth in consumtionsPerMonth:
  File "/home/thomas/Dokumente/Haus/E-Auto/OWB/update/consumption_log/./calculate_waighted_average.py", line 39, in <lambda>
    consumtionsPerMonth = groupby(data, key=lambda hour: order_key(getattr(hour,'from')))
AttributeError: 'Consumption' object has no attribute 'from'

Ich weiß nicht, warum ich im Sortier-Lambda nicht genau so auf die Attribute von „Entry“ zugreifen kann, wie bei der Summenbildung in der Statistikfunktion.

bye
TT

Habs gefunden: Das Framework tibberpy nutz andere Attribute als der API-Browser von Tibber, so dass das Attribut „from_time“ statt „from“ heißt.

bye
TT