Fix Home Assistant Legacy Template Deprecation + Update Malaysia TNB Energy Sensor for 2025 Tariff
If you see a yellow “Legacy sensor template deprecation” banner in Home Assistant, your energy cost sensors will break in version 2026.6. At the same time, if you’re still calculating your TNB bill using the old tiered block rates (0.218, 0.334 sen), your dashboard cost is wrong — TNB changed to a Base Rate + Rebate system in July 2025.
Here’s how to fix both problems with one YAML update.
The Two Problems
1. YAML Syntax Change (affects all template sensors)
The old format is deprecated:
# OLD — deprecated, stops working in HA 2026.6
sensor:
- platform: template
sensors:
my_energy_sensor:
...
The new format uses the top-level template: domain:
# NEW — required from HA 2026.6
template:
- sensor:
- name: "My Energy Sensor"
...
All your existing template sensor logic works the same — only the wrapping structure changes.
2. TNB 2025 Tariff Change (affects Malaysian energy cost calculations)
Old system (before July 2025):
- First 200kWh at 0.218 sen/kWh
- Next 100kWh at 0.334 sen/kWh
- Beyond 300kWh at 0.516 sen/kWh
New system (from July 2025):
- Everyone pays a flat Base Rate: 44.43 sen/kWh
- Adjusted by AFA (Fuel Adjustment Factor): -6.42 sen/kWh (changes quarterly)
- EEI Rebates applied based on usage tiers — instead of “cheap first blocks”, you get discounts for lower consumption
Net bill: (kWh × (base_rate + afa_rate)) - rebate
The Complete Drop-In Fix
Replace your old energy sensor block in configuration.yaml with this:
template:
- sensor:
- name: "Malaysia Energy Cost"
unique_id: malaysia_energy_cost_sensor
unit_of_measurement: "RM"
state: >
{% set kwh = states('sensor.tuya_power') | float(0) %}
{# July 2025 Rates — update AFA when TNB announces changes #}
{% set base_rate = 0.4443 %}
{% set afa_rate = -0.0642 %}
{# EEI Rebate calculation (replaces old tiered blocks) #}
{% set rebate = 0 %}
{% if kwh <= 200 %}
{% set rebate = kwh * 0.250 %}
{% elif kwh <= 250 %}
{% set rebate = (200 * 0.250) + ((kwh - 200) * 0.245) %}
{% elif kwh <= 300 %}
{% set rebate = (200 * 0.250) + (50 * 0.245) + ((kwh - 250) * 0.225) %}
{% elif kwh <= 600 %}
{% set rebate = 82.25 + ((kwh - 400) * 0.120) %}
{% else %}
{% set rebate = 106.25 + ((kwh - 600) * 0.075) %}
{% endif %}
{# Final calculation #}
{% set energy_cost = (kwh * (base_rate + afa_rate)) - rebate %}
{% set total = energy_cost + (10.00 if kwh > 600 else 0) %}
{# 8% Service Tax applies only on usage > 600kWh #}
{% if kwh > 600 %}
{% set total = total * 1.08 %}
{% endif %}
{{ total | round(2) }}
Replace sensor.tuya_power with whatever entity tracks your cumulative monthly kWh usage.
When TNB announces a new AFA rate: change only the afa_rate line. Current rate is -0.0642 (as of July 2025); this changes quarterly.
Why You Shouldn’t Apply This Formula to Daily Cost
The rebate and tier logic is based on cumulative monthly kWh. If you apply this formula to a single day’s consumption (e.g., 15kWh), it always calculates at the first (cheapest) tier — which understates the real cost for high-usage households.
Better approach for daily cost tracking:
- Use a flat average rate sensor (e.g., 40 sen/kWh) for an approximate “at-a-glance” daily number
- For accuracy: calculate
(today's monthly estimate) - (yesterday's monthly estimate)to derive daily marginal cost
After Migration
- Restart Home Assistant after updating
configuration.yaml - The yellow legacy warning banner should disappear
- Check the sensor state in Developer Tools → States → search for “Malaysia Energy Cost”
- Verify the calculated value looks reasonable against your last TNB bill
Frequently Asked Questions
What is the Home Assistant legacy template deprecation warning?
The old platform: template syntax under the sensor: domain is being removed in HA 2026.6. Migrate to the template: top-level domain now to avoid sensors breaking.
What is Malaysia’s new TNB tariff structure (July 2025)? Base Rate (44.43 sen/kWh) + AFA adjustment (-6.42 sen, changes quarterly) + EEI Rebates based on usage. Net bill = (kWh × net rate) - rebate. More complex than the old tier system but similar effective rates for most users.
How do I update the AFA rate when TNB changes it?
Change only the afa_rate value in the YAML. Everything else stays the same.
Why shouldn’t I use the monthly formula for daily cost? Daily usage (15kWh) always calculates at Tier 1/max rebate — mathematically wrong for cumulative monthly rebate logic. Use a flat average rate for daily approximation.
For more Home Assistant guides and smart home automation for Malaysia, see the Smart Home section.