Skip to content

Commit a5e4f6e

Browse files
committed
docs: add example for fetching historical price data in README
1 parent 2f809bb commit a5e4f6e

File tree

1 file changed

+35
-7
lines changed

1 file changed

+35
-7
lines changed

README.md

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
> Note that this is NOT [pyTibber](https://github.com/Danielhiversen/pyTibber) which is the official python wrapper endorsed by Tibber themselves.
44
55
tibber.py is an unofficial python wrapper package for communication with the [Tibber API](https://developer.tibber.com/).
6-
This package aims to cover all functionalities of the Tibber API in the most beginner-friendly modern Pythonic way. You can read all the capabilites of the API and explore it
6+
This package aims to cover all functionalities of the Tibber API in the most beginner-friendly modern Pythonic way. You can read all the capabilites of the API and explore it
77
with [Tibbers' API explorer](https://developer.tibber.com/explorer). For documentation on how to use tibber.py head over to https://tibberpy.readthedocs.io/en/latest/.
88

99
Every field of the API types should be found in the corresponding `tibber.type` (e.g. the `size: Int` field of `Home`
@@ -21,21 +21,24 @@ docs (located on the right side of the Tibber API explorer).
2121
[![Pytest Python 3.7 / 3.11](https://github.com/BeatsuDev/tibber.py/actions/workflows/pytests.yml/badge.svg)](https://github.com/BeatsuDev/tibber.py/actions/workflows/pytests.yml)
2222
![Publish to PyPi status](https://github.com/BeatsuDev/tibber.py/actions/workflows/publish-to-pypi.yml/badge.svg)
2323

24-
25-
2624
Do you want to ask a question, report an issue, or even showcase your project that uses tibber.py? 🤩<br>Find out where to post by [checking out this overview](https://github.com/BeatsuDev/tibber.py/discussions/46).
2725

28-
2926
## Installation
27+
3028
### Install via pip
29+
3130
```
3231
python -m pip install tibber.py
3332
```
33+
3434
### Requirements
35+
3536
tibber.py depends on `gql`, `gql[aiohttp]`, `gql[websockets]` and `graphql-core`. tibber.py supports Python versions 3.7 and up!
3637

3738
## Examples
39+
3840
### Getting basic account data
41+
3942
```python
4043
import tibber
4144

@@ -56,6 +59,7 @@ print(account.name)
5659
```
5760

5861
### Getting basic home data
62+
5963
```python
6064
import tibber
6165

@@ -74,7 +78,8 @@ print(home.has_ventilation_system) # False
7478
print(home.main_fuse_size) # 25
7579
```
7680

77-
### Reading historical data
81+
### Reading historical consumption data
82+
7883
```python
7984
import tibber
8085

@@ -98,9 +103,32 @@ for hour in hour_data:
98103
print(hour.cost)
99104
```
100105

106+
### Reading historical price data
107+
108+
```python
109+
import tibber
110+
import datetime
111+
import base64
112+
113+
114+
account = tibber.Account(tibber.DEMO_TOKEN)
115+
price_info = account.homes[0].current_subscription.price_info
116+
117+
# The API requires the date to be passed as a base64 encoded string with timezone information
118+
date = datetime.datetime(2025, 1, 1, 0, 0, 0)
119+
encoded_date = base64.b64encode(date.astimezone().isoformat().encode("utf-8")).decode("utf-8")
120+
121+
# Only HOURLY and DAILY
122+
connection = price_info.fetch_range("HOURLY", first=10, after=encoded_date)
123+
124+
connection.nodes # A list of Price objects
125+
```
126+
101127
### Reading live measurements
128+
102129
Note how you can register multiple callbacks for the same event. These will be run
103130
in asynchronously (at the same time)!
131+
104132
```python
105133
import tibber
106134

@@ -115,11 +143,11 @@ async def show_current_power(data):
115143
@home.event("live_measurement")
116144
async def show_accumulated_cost(data):
117145
print(f"{data.accumulated_cost} {data.currency}")
118-
146+
119147
def when_to_stop(data):
120148
return data.power < 1500
121149

122150
# Start the live feed. This runs until data.power is less than 1500.
123151
# If a user agent was not defined earlier, this will be required here
124-
home.start_live_feed(user_agent = "UserAgent/0.0.1", exit_condition = when_to_stop)
152+
home.start_live_feed(user_agent = "UserAgent/0.0.1", exit_condition = when_to_stop)
125153
```

0 commit comments

Comments
 (0)