diff options
Diffstat (limited to 'scripts/tibber_consumption.py')
-rw-r--r-- | scripts/tibber_consumption.py | 41 |
1 files changed, 23 insertions, 18 deletions
diff --git a/scripts/tibber_consumption.py b/scripts/tibber_consumption.py index b88d00b..f54faa6 100644 --- a/scripts/tibber_consumption.py +++ b/scripts/tibber_consumption.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -''' import energy consumption from tibber ''' +""" import energy consumption from tibber """ import os import sys @@ -10,13 +10,13 @@ import requests from tzlocal import get_localzone # variables -apiKey = os.environ['el_tibber_token'] +apiKey = os.environ["el_tibber_token"] apiUrl = "https://api.tibber.com/v1-beta/gql" -startTime = datetime.now(get_localzone()) - timedelta(days = 1) -startTime = startTime.isoformat('T') +startTime = datetime.now(get_localzone()) - timedelta(days=1) +startTime = startTime.isoformat("T") -endTime = datetime.now(get_localzone()).isoformat('T') +endTime = datetime.now(get_localzone()).isoformat("T") # Get the data try: @@ -24,11 +24,12 @@ try: # Request headers hdr = { - 'Authorization': "Bearer " + apiKey, - 'Content-Type': 'application/json', + "Authorization": "Bearer " + apiKey, + "Content-Type": "application/json", } - body = {"query":"""{ + body = { + "query": """{ viewer { homes { consumption(resolution: HOURLY, last:100) { @@ -43,7 +44,8 @@ try: } } } - } }"""} + } }""" + } response = requests.post(url, headers=hdr, json=body, timeout=10) if response.status_code != 200: @@ -65,17 +67,20 @@ print("Got " + str(numdata) + " rows from Tibber") values = [] for item in data["data"]["viewer"]["homes"][0]["consumption"]["nodes"]: if item["consumption"] is not None: - values.append(( - item["from"], - item["to"], - item["consumption"], - item["consumptionUnit"], - item["cost"], - item["unitPrice"], - item["unitPriceVAT"])) + values.append( + ( + item["from"], + item["to"], + item["consumption"], + item["consumptionUnit"], + item["cost"], + item["unitPrice"], + item["unitPriceVAT"], + ) + ) # SQL -sql = """INSERT INTO tibber_consumption +sql = """INSERT INTO tibber_consumption VALUES(%s, %s, %s, %s, %s, %s, %s) ON CONFLICT (startTime,endTime) DO NOTHING""" |