Skip to content

About

TOU Writer to Tesla Powerwall via Teslemetry

Resources

Stars

4 stars

Watchers

0 watching

Forks

Repository files navigation

TOU Writer for Home Assistant

TOU Writer pushes a 24-hour, 30-minute Time-of-Use (TOU) tariff to a Tesla Powerwall through Teslemetry. Paired with the example Amber Electric automation, it keeps the Powerwall's tariff in step with Amber's live prices, so the Powerwall's own optimiser works from realistic buy and sell prices.

Warning

0.2.2 contains breaking changes. If you are upgrading from 0.2.1 or earlier, read Breaking changes in 0.2.2 first. You must update both the integration and the automation YAML; HACS only updates the integration.

How it fits together

Amber forecasts ──► Example automation ──► tou_writer.push_tou ──► Teslemetry ──► Powerwall tariff
                         ▲
input_select.powerwall_mode (Normal / Discharge Start / Charge Start)
                         ▲
        your controller (e.g. home-battery-control) or you, manually
Part Role
Integration (custom_components/tou_writer) Provides the tou_writer.push_tou action. Turns a list of rate periods into a Tesla tariff and sends it. It makes no pricing decisions.
Example automation (automations/amber_tesla.yaml) Reads Amber forecasts, averages them into 48 × 30-min slots, decides whether a push is needed, and calls the action. Stores the last successful push in MQTT.
input_select.powerwall_mode The control input. A change always pushes immediately. Discharge Start boosts prices to make the Powerwall export.
Your controller (optional) Decides when to charge or discharge and sets the dropdown. The author uses home-battery-control. TOU Writer just mirrors prices; it doesn't decide anything.

Breaking changes in 0.2.2

Version 0.2.2 cuts Teslemetry API usage from roughly 576 calls/day to a few dozen, after Teslemetry started returning HTTP 429 (rate limited).

Integration (delivered by a HACS update)

# Change What you'll notice What to do
1 tou_writer.push_tou now raises an error when a push fails. Previously it failed silently and the caller carried on. Any automation or script calling it stops at that step; its trace shows as errored, and HA logs Error executing script. Nothing, if you use the example automation (this is what stops a failed push being recorded as successful). In your own scripts, add continue_on_error: true to the action if you want the old behaviour.
2 HTTP 429 (rate limited) is no longer retried. Previously 3 attempts within ~6 s. One WARNING Teslemetry rate limited (429) — not retrying … Headers: … per event. Nothing. The next scheduled run is the retry.
3 Readback verification is off by default. Previously every push was followed by a GET to confirm it. No verified log lines; half the API calls. To re-enable for debugging, set VERIFY_READBACK = True in __init__.py.

Example automation (NOT delivered by HACS: re-paste it)

# Change What you'll notice
4 Runs every 30 min (at :00 / :30, plus a 60 s settle delay) instead of every 5 min. Far fewer runs.
5 Pushes only if any slot in the next 2 hours differs from the last pushed rates by more than 2 c/kWh (import or export). Most runs stop at the check without pushing. Slots more than 2 h ahead can be stale until they come into the window. The @ HH:MM in the Tesla plan name is the time of the last push, which can be hours old.
6 Never pushes when Amber forecasts are empty, including on HA start before Amber has loaded. No more 0 c tariffs after a restart or Amber outage.
7 The MQTT "last pushed" record is written only after a successful push. A failed push is retried on the next run.

Upgrade checklist

  1. Update the integration (HACS → TOU Writer → Update, or copy the folder manually) and restart Home Assistant.
  2. Open your Amber automation in YAML mode and replace it with the new automations/amber_tesla.yaml. Re-apply any local edits (sensor names, price_adjustment_c).
  3. Optionally adjust window_hours and threshold_c in step 4 of the automation.
  4. Check a trace after the next :01 or :31 run: it should either push or stop at the changed_in_window condition.

Caution

Update both parts. With the new integration and the old automation, it still runs every 5 min and every 429 shows as an errored run. With the old integration and the new automation, a failed push is still recorded in MQTT as if it succeeded.

Installation

HACS (recommended)

  1. Make sure HACS is installed.
  2. HACS → ⋮ (top right) → Custom repositories, add https://github.com/RangeyRover/tou-writer, category Integration.
  3. Find TOU Writer and click Download.
  4. Restart Home Assistant.

HACS installs the integration only. The example automation must be pasted in manually (see Example: Amber automation).

Manual

  1. Copy custom_components/tou_writer into your HA config/custom_components/ folder.
  2. Restart Home Assistant.

Configuration

  1. Settings → Devices & services → Add integration, search for TOU Writer.
  2. Enter your Teslemetry access token and energy site ID.

Action: tou_writer.push_tou

Builds a 48-slot Tesla tariff from your rate periods and sends it to the Powerwall.

Field Required Description
rates Yes List of periods, each {start: "HH:MM", end: "HH:MM", buy: c/kWh, sell: c/kWh}. end: "00:00" means midnight. Should cover the full 24 h; any uncovered 30-min slot defaults to 0 (with a WARNING).
plan_name No Name shown in the Tesla app. Default Custom TOU (TOU Writer).
action: tou_writer.push_tou
data:
  plan_name: "My TOU Plan"
  rates:
    - { start: "00:00", end: "07:00", buy: 15.0, sell: 5.0 }
    - { start: "07:00", end: "00:00", buy: 35.0, sell: 10.0 }

The tariff applies every day, all year, in AUD.

Results and failures

Outcome Behaviour
Success Fires tou_writer_push_result with success: true, attempt_count, plan_name, masked site_id; dismisses any failure notification.
HTTP 5xx / network error Retries up to 3 attempts, 2 s then 4 s apart.
HTTP 429 No retry. Logs any Retry-After / X-RateLimit-* headers at WARNING.
HTTP 400 / 401 / 403, other statuses No retry.
Push failure (any of the above) Fires tou_writer_push_result with success: false and error, creates a persistent notification, and raises an error so the calling automation stops.
Tariff build error (bad rates) Fires tou_writer_push_result with success: false and error, and raises an error. Nothing is sent.

Example: Amber automation

automations/amber_tesla.yaml syncs Amber pricing to the Powerwall.

What it does

  • Triggers: HA start, every 30 min, and any change of input_select.powerwall_mode.
  • Rates: averages Amber's 5-min forecasts into 48 × 30-min slots over a rolling 24 h (slots that have already ended today are filled with tomorrow's prices).
  • Guard: stops if either Amber forecast list is empty.
  • Push decision: a mode change always pushes. Otherwise it pushes only if a slot in the next window_hours (default 2) differs from the last pushed rates by more than threshold_c (default 2 c/kWh), on import or export. If there is no valid MQTT record yet, it pushes.
  • Force discharge: in Discharge Start, slots overlapping the next hour get +100 c/kWh on both buy and sell (price_adjustment_c). The hour slides with each run, so it pushes every 30 min while discharging.
  • Persistence: after a successful push, it writes the rates to a retained MQTT topic, exposed as sensor.amber_tou_last_push_hash.

Setup

  1. Requires the Amber Electric integration and a working MQTT broker. Edit the two sensor names in the automation (sensor.4_rosella_general_forecast, sensor.4_rosella_feed_in_forecast) to match yours.
  2. Create a dropdown helper: Settings → Devices & services → Helpers → Create helper → Dropdown, entity ID input_select.powerwall_mode, options Normal, Discharge Start, Charge Start.
  3. Create a new automation, switch to YAML mode, and paste the file.

Known limitations

  • Charge Start only changes the plan name; it applies no price adjustment.
  • Discharge Start doesn't expire on its own; something (you or your controller) must set it back to Normal.
  • If Amber's forecast doesn't reach a full 24 h ahead, slots beyond it are pushed as 0 c.
  • On daylight-saving changeover days, the 48 slots are counted from midnight and may be off by an hour.

Troubleshooting

Symptom Meaning / action
Teslemetry rate limited (429) — not retrying … Headers: {...} Teslemetry is throttling. The headers (if any) show the real limits. Other Teslemetry users on the same account (e.g. the HA Teslemetry integration) share the quota.
Automation trace errored at tou_writer.push_tou The push failed (see the log line before it). MQTT wasn't updated; the next run retries.
How many pushes per day? Look at the history of sensor.amber_tou_last_push_hash: one state change per successful push. (push complete log lines are INFO level, hidden by default.)
Tesla app plan name time is old Normal: prices haven't moved more than the threshold since that push.

See CHANGELOG.md for version history.

Credits

About

TOU Writer to Tesla Powerwall via Teslemetry

Resources

Stars

4 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages