Aller au contenu

ADR-0074: Car charges analysed by Home Assistant from the recorder — charges, solar share, kWh per hour and month

Page en anglais

Cette page n'existe qu'en anglais.

Status: Proposed (2026-11-09; the Home Assistant half of the cars in the Energy popup — built and tested off-device, never run on a real Home Assistant nor with a real car). Date: 2026-11-09

Context

A user with a solar installation and two electric cars (Tesla, through the Tesla Fleet or Teslemetry integration of Home Assistant; discussion #278) wants the cars in the Energy popup (ADR-0028, ADR-0058): the last charges and how much of each came from the sun, the kWh per hour of a day, the month's balance. The integrations give the battery level, the charger power and the energy added of the current charge — none of these: no list of charges, no share of the sun, no kWh per hour, no month. Home Assistant has to compute them, from its own data, the way the Energy popup does (ADR-0062).

The firmware never calls a Home Assistant action (ADR-0025) and the public packages hold no entity of a real home (ADR-0024): the sensors are handed to the script by the blueprint, as for script.tab5_energie.

Decision

  • A script that only reads and answers. script.tab5_voitures_analyse (packages/tab5_voitures.yaml) takes capteurs (solar power sensors, the home power sensor, whether it counts the cars, one or two cars: power of charge, optional odometer and energy added) and an optional jour, reads recorder.get_statistics and returns its result as the script response. It pushes nothing to the tablet and calls no service of the car's integration, so it never wakes the car. A later lot pushes the strings it returns.
  • Statistics, not states. The charger power (state_class: measurement) is read as its mean, in W (units: {power: W}: Home Assistant converts the kW the Tesla sensors give). The 5-minute rows are kept purge_keep_days days (10 by default); older ones only exist hourly. The two are stitched into one series of slots of 300 s or 3600 s: hourly rows before the first full hour covered by 5-minute rows, 5-minute rows after. Charges, shares and hours work the same on both; the precision does not (the package README says how).
  • A charge is a run of slots whose mean power is above 100 W; slots separated by 30 minutes at most are one charge (a data hole, a pause under a cloud). Duration runs from the first to the last slot; energy is the sum of power × duration. The ends of a charge are known to 5 minutes, or to the hour on hourly rows (the duration then counts whole hours, up to two too many; the energy stays right).
  • Share of the sun, slot by slot: available = solar − home consumption without the cars, never below 0 (the cars' charge is subtracted from the home sensor when it counts them, the default); each car receives the fraction min(1, available / charge of all the cars) of its power. The share of a charge is Σ solar / Σ energy over the slots where it can be known, 0 to 100 %. Where the plan said min(charge, available) per car, this version divides the available sun between cars charging together: two cars at 4 kW with 5.2 kW available get 65 % each, not 100 % each (which would count 8 kW of sun out of 5.2). With one car the two formulas are the same. A slot without a home row is left out of both sums; solar sensors without a row count for 0 (an inverter off at night is a charge at 0 %). No home sensor: the share is nan (« -- »); no solar sensor: it is empty (not chosen).
  • Month: from the 1st at 00:00 to now, same slots. kWh/100 km is the energy charged divided by the kilometres of the month (odometer statistics, change, in km), only above 20 km, and is an approximation — a charge is not a consumption (the previous day's charge, charging away from home). The share of the house is the cars' energy over the home sensor's energy of the month.
  • Formats (plan §5, family of tab5_maj_energie*: fields |, lists ;, empty = not chosen, nan = no value): charges rang|jour|heure|duree_min|kwh|part_soleil_pct (AAAA-MM-JJ and HH:MM local, 6 lines at most, newest first, one list per car and one for both); a day's 24 kWh per local hour, empty where the car has no row; the month as 11 fields (per car: kWh, share, km, kWh/100 km, kWh taken from the grid; then the cars' part of the house). A precision field says which statistics were found.
  • One method, two writings. tools/voitures_charges.py is the reference, in Python, tested by hand-computed series (night charge 0 %, midday ≈ 100 %, a cloud, a car plugged in without charging, data holes, one then several solar sensors, the home counting the cars or not, two cars together, hourly rows only, both, a daylight-saving day, the month). The Jinja of the package does the same operations in the same order and tests/test_voitures.py renders it in Jinja's sandbox on the same series: the strings must be identical.

Consequences

  • No contract change, no firmware change, no new entity: only a script in a new package (picked up by the release archive with the other packages).
  • The calculation walks thousands of rows in Home Assistant's event loop (0.3 s on a PC for 10 000 rows; not measured on a Raspberry Pi): call it when the popup opens, then at most every five minutes.
  • One template reads the rows (fixed 2026-11-10). Home Assistant refuses a template whose output is over 262 144 characters. The first version rendered the slots of every sensor into a variable (mesures, then an index idx) for the other templates to read: three sensors over the 10 days of 5-minute rows were enough to go past the limit — HTTP 500 from the script, the Charges page empty with no message (measured on the author's Home Assistant, 2026-11-10). The slots and their index now stay inside the one template analyse, which reads the recorder's response itself and returns only the six newest charges of each car and a few sums (kWh per hour and per day, the home's hours and month): its size depends neither on the number of sensors nor on the depth of the history. Same operations in the same order, so the strings are unchanged; tests/test_voitures.py renders four dense homes (17 400 rows each) against the reference under a guard of 262 144 characters per template.
  • Charging away from home (a public charger the car reports) is counted in the lists and the month; its share of the sun is meaningless. A home battery is not counted as sun.
  • Not verified on a real Home Assistant, nor with a real Tesla: the key distance of units, what the car's sensors do when it sleeps, whether charger power has statistics on the user's integration (the sensor must have state_class: measurement).