Delivery Detection & Cost Per Gallon Fixes
v3.1.1 — Delivery Detection & Cost Per Gallon Fixes
🐛 Bug Fixes
Portal-Lag-Immune Delivery Detection
The v3.1.0 date-change trigger for automatic pre-delivery level capture could fire with stale last_delivery_gallons data when the AmeriGas portal updated last_delivery_date and last_delivery_gallons in separate API responses. This caused the captured pre-delivery level to be calculated against the previous delivery's gallons figure, producing a significantly wrong value (e.g. 420 - 28.1 = 391.9 instead of the correct 420 - 255.9 = 164.1), which cascaded into incorrect values for Used Since Delivery, Daily Average Usage, Days Until Empty, Cost Since Delivery, and Days Remaining Difference.
Fix: The date-change trigger now tracks last_delivery_gallons across polls alongside last_delivery_date. If the date changes but gallons hasn't updated yet, capture is deferred until a subsequent poll where gallons also changes. This eliminates the stale-data capture without affecting the normal case where both update in the same poll.
A new level-jump trigger (telemetry-first, portal-lag immune) fires immediately when the tank monitor reports an increase ≥ 10 gallons between consecutive coordinator polls, capturing both pre_fill and post_fill directly from the tank monitor — the most accurate possible baseline, independent of portal lag entirely.
Cost Per Gallon After Delivery
sensor.propane_cost_per_gallon previously used last_payment_amount / last_delivery_gallons unconditionally. Immediately after a delivery, the last payment on record is for the prior delivery cycle, producing an incorrect cost per gallon until the new invoice was paid.
Fix: If last_payment_date predates last_delivery_date, the sensor now uses account_balance (preferred) or amount_due as the numerator, since those fields reflect what is owed for the current delivery. Once payment is recorded and last_payment_date advances past last_delivery_date, the sensor automatically reverts to the standard calculation.
🔧 Technical Changes
delivery_tracker.py — DeliveryTracker
- Added
_last_known_delivery_gallons: float— tracks gallons value across polls - Added
_pending_api_capture: bool— deferred capture flag - Trigger 2 (date-change): three paths — immediate capture (date + gallons both changed), deferred capture (date changed, gallons unchanged), deferred-capture resolution (fires when gallons updates on a later poll)
_last_known_delivery_gallonsupdated every poll
sensor.py — AmeriGasSensorBase._calculate_cost_per_gallon()
- Compares
last_payment_datetolast_delivery_date - When payment predates delivery: uses
account_balanceif > 0, elseamount_dueif > 0 - Falls back to
last_payment_amountwhen payment is current
manifest.json
- Version bumped to
3.1.1
🔄 Migration Notes
No breaking changes. Update via HACS and restart.
If you installed v3.1.1 after a recent delivery (level-jump trigger was not running at delivery time), set the pre-delivery level manually:
service: amerigas.set_pre_delivery_level
data:
gallons: 145.0 # your actual pre-delivery level in gallonsCost per gallon and all dependent sensors self-correct automatically once the delivery invoice is paid.