Skip to main content

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;
  • the hourly outdoor temperature anywhere, from Open-Meteo, for homes without a thermometer.

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 MissingColorError or MissingPriceError instead of guessing.

Daily totals

A Linky meter without a load curve only reports daily totals. DayShape estimates how a day splits between its hours:

from elec_tariffs import DayShape

# Days with a load curve: {date: [24 hourly kWh, in local time]}.
shape = DayShape.learn(days)
# The local hours of a day with only a total, and that total.
values = shape.spread(hours, total)

It fits the night consumption (22:00 to 6:00) as a straight line of the daily total, and spreads both parts along the average hourly profile. With fewer than 14 complete days it spreads evenly. The result is an estimate: it can't know, for example, that consumption moves to the night on red Tempo days.

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.

Outdoor temperature

OpenMeteoClient(session).async_hourly_temperatures(latitude, longitude, start, end) returns the air temperature at 2 m (°C) of each hour of the UTC days start to end, keyed by the UTC start of the hour. It reads Open-Meteo's historical weather API: free for non-commercial use, without an account, back to 1940. Its default model blends the ERA5 reanalyses with the ECMWF forecast model, so it covers the last days too; those hours are revised about five days later, so callers should ask for the last week again. Long ranges are split into requests of 366 days at most. The data is under CC BY 4.0: credit Open-Meteo where it is shown.

Errors

Every error derives from ElecTariffsError:

  • MissingColorError, MissingPriceError (with the day concerned);
  • TrvServerError, TrvFormatError for data.gouv.fr;
  • TempoServerError (network or 5xx), TempoRateLimitError (429), TempoAuthError (credentials refused) and TempoFormatError for the Tempo sources;
  • WeatherServerError (network or 5xx), WeatherRateLimitError (429) and WeatherFormatError (request refused or unexpected answer) for Open-Meteo.

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

MIT

Metadata

Release files for elec-tariffs 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for elec-tariffs 0.3.0
File Size Uploaded Provenance Actions
elec_tariffs-0.3.0.tar.gz 39.8 kB With provenance Details for elec_tariffs-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for elec-tariffs 0.3.0
File Interpreter ABI Platform Actions
elec_tariffs-0.3.0-py3-none-any.whl Python 3 none any Details for elec_tariffs-0.3.0-py3-none-any.whl

Total release size: 60.8 kB

Release files / elec_tariffs-0.3.0.tar.gz

Download URL elec_tariffs-0.3.0.tar.gz
Size 39.8 kB
Tags Source
SHA-256 checksum
How to use checksums
63f0c88d1e81a9f0ede31e5dc08ccb35cc47841158b2e4c2218b8626e420f498
BLAKE2b-256 checksum
How to use checksums
c30d8ad9a2b28b25f8206ff3ff7af83324e548c0e0262e9929a39fb9b7de3e16
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 18, 2026.

Transparency log

Release files / elec_tariffs-0.3.0-py3-none-any.whl

Download URL elec_tariffs-0.3.0-py3-none-any.whl
Size 21.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bef90eed2bdc055a92fe9ebffe583cb7e3da39224dd447fda8c9b8c6b82aa78
BLAKE2b-256 checksum
How to use checksums
b062d308cebda2078b53feeb01bd8ae7a871e765bf95934459ec38ed757d9bff
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 18, 2026.

Transparency log

Security for elec-tariffs 0.3.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.

Select from 2 files

Attestation for elec_tariffs-0.3.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 18, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page