elec-tariffs
Cost French electricity consumption hour by hour under Base, HP/HC and Tempo contracts.
elec-tariffs takes hourly consumption (as a Linky meter reports it) and works out what it cost under one or more contracts: which tranche each hour falls into, its price on that day, and its share of the yearly subscription. It also downloads the data that needs:
- the regulated prices (Tarif Bleu) of every period since 2012, from data.gouv.fr;
- the color of each Tempo day, from RTE.
It is async (aiohttp), fully typed and has no dependency on Home Assistant. It was written for the Electricity Contracts Home Assistant integration.
It is not affiliated with EDF, RTE or Enedis.
Install
pip install elec-tariffs
Python 3.14 or later.
Use
import asyncio
from datetime import date, datetime
from zoneinfo import ZoneInfo
import aiohttp
from elec_tariffs import (
ContractKind,
ContractRules,
RtePublicTempoClient,
TrvClient,
cost_hour,
)
PARIS = ZoneInfo("Europe/Paris")
async def main() -> None:
async with aiohttp.ClientSession() as session:
prices = await TrvClient(session).async_price_periods(ContractKind.TEMPO, 6)
colors = await RtePublicTempoClient(session).async_calendar(
date(2026, 1, 4), date(2026, 1, 5)
)
# 1.8 kWh used from 19:00 to 20:00 on 5 January 2026.
cost = cost_hour(
datetime(2026, 1, 5, 19, tzinfo=PARIS),
1.8,
ContractRules(kind=ContractKind.TEMPO),
prices,
colors,
)
print(cost.energy, cost.variable, cost.fixed, cost.total)
asyncio.run(main())
An HP/HC contract takes its own off-peak ranges, to the half hour, and can make weekends off-peak:
from datetime import time
from elec_tariffs import ContractKind, ContractRules, TimeRange
rules = ContractRules(
kind=ContractKind.HPHC,
off_peak=(
TimeRange(start=time(12, 30), end=time(14)),
TimeRange(start=time(22), end=time(6, 30)),
),
weekend_off_peak=True,
)
Prices you set yourself are PricePeriod objects: a first and last day (None while in force), the yearly fixed part and a price per tranche, all with taxes.
How an hour is costed
- Tranches. Each hour is split into two half hours with half the energy each, so an off-peak range that starts or ends on a half hour is followed. This assumes the consumption is even within the hour.
- Tempo. A Tempo day runs from 6:00 to 6:00, so a night takes the color of the day that started the evening before. Off-peak hours are 22:00 to 6:00.
- Prices change at midnight on the first day of a price period (1 February and 1 August for the regulated prices). An hour uses the prices of its calendar day.
- Subscription. The yearly fixed part is spread evenly over the hours of the year (8,760, or 8,784 in a leap year).
- Missing data. An hour whose Tempo color or price isn't known raises
MissingColorErrororMissingPriceErrorinstead of guessing.
Tempo calendar sources
| Source | Account | Notes |
|---|---|---|
RtePublicTempoClient |
none | The backend of RTE's public Tempo calendar page. It isn't documented and RTE can change it without notice, so its answers are checked strictly: an unexpected format raises TempoFormatError instead of giving wrong colors. |
RteOfficialTempoClient |
free data.rte-france.com application | RTE's documented "Tempo Like Supply Contract" API, with OAuth2 client credentials. The token is kept until it expires and renewed once if refused. Long ranges are split into requests of 366 days at most. Not yet tested against the live API: its tests follow the format used by other clients. |
FallbackTempoSource asks each source in turn, for example the public one first and the official one when it fails. It records which source answered and the errors of the others.
Past colors never change, so callers should cache them and only ask for the current season and the next day.
To create the official API application: sign up on data.rte-france.com, open the "Tempo Like Supply Contract" API, choose "S'abonner à l'API" (this creates an application), then copy its client ID and secret.
Errors
Every error derives from ElecTariffsError:
MissingColorError,MissingPriceError(with thedayconcerned);TrvServerError,TrvFormatErrorfor data.gouv.fr;TempoServerError(network or 5xx),TempoRateLimitError(429),TempoAuthError(credentials refused) andTempoFormatErrorfor the Tempo sources.
Development
python3.14 -m venv .venv
.venv/bin/pip install -r requirements-dev.txt -e .
.venv/bin/ruff check && .venv/bin/ruff format --check && .venv/bin/mypy
.venv/bin/pytest --cov
The tests use recorded answers in tests/fixtures/ and a local HTTP server; they make no network requests.
Releases are published to PyPI by GitHub Actions when a GitHub release is published; the tag (vX.Y.Z) must match src/elec_tariffs/_version.py.
License
Metadata
Release files for elec-tariffs 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| elec_tariffs-0.1.0.tar.gz | 33.9 kB | With provenance | Details for elec_tariffs-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| elec_tariffs-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for elec_tariffs-0.1.0-py3-none-any.whl |
Total release size: 50.1 kB
Release files / elec_tariffs-0.1.0.tar.gz
| Download URL | elec_tariffs-0.1.0.tar.gz |
|---|---|
| Size | 33.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3af93fd74f4f2e585d6d76df5856034997019ca050b6efa17192e8a00379497
|
|
BLAKE2b-256 checksum How to use checksums |
4b1a13be4c0ec1d198fd38cdc34f37b6fa19e3ffb25ccefb30b7273809e67e08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logRelease files / elec_tariffs-0.1.0-py3-none-any.whl
| Download URL | elec_tariffs-0.1.0-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d1fe3faa5771538dbdf96ad6cdd5e0530a852243802fa1ea0aca1bcae20c19f6
|
|
BLAKE2b-256 checksum How to use checksums |
0e59d4979b55e73c2f0bffed55a90fe53f2aceba649fef59253fddb7c0d93e4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logSecurity for elec-tariffs 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for elec_tariffs-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 6, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.