ha-energy
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHome Assistant Energy Skill
Home Assistant 能源技能
Configure Home Assistant energy monitoring with dashboards, solar, grid, and device tracking.
配置带有仪表盘、太阳能、电网及设备追踪功能的Home Assistant能源监控系统。
Before You Start
开始之前
This skill prevents 8 common errors and saves ~45% tokens.
| Metric | Without Skill | With Skill |
|---|---|---|
| Setup Time | 45+ min | 15 min |
| Common Errors | 8 | 0 |
| Token Usage | ~10000 | ~5500 |
本技能可避免8种常见错误,并减少约45%的令牌消耗。
| 指标 | 未使用技能 | 使用技能 |
|---|---|---|
| 设置时间 | 45+ 分钟 | 15 分钟 |
| 常见错误数量 | 8 | 0 |
| 令牌消耗 | ~10000 | ~5500 |
Known Issues This Skill Prevents
本技能可预防的已知问题
- Incorrect values (must be
state_class,total, ortotal_increasing)measurement - Missing on energy sensors
device_class: energy - Wrong for utility_meter entities (must be
state_class)total_increasing - Using without
unit_of_measurement: kWh(breaks statistics)state_class - Forgetting to enable statistic collection in configuration.yaml
- Configuring solar sensors without battery tracking for self-consumption
- Grid monitoring with mismatched sensor pairs (consumption vs return)
- Utility meter cycle settings that don't align with billing periods
- 错误的值(必须为
state_class、total或total_increasing)measurement - 能源传感器缺少属性
device_class: energy - utility_meter实体使用错误的(必须为
state_class)total_increasing - 使用但未设置
unit_of_measurement: kWh(会导致统计功能失效)state_class - 忘记在configuration.yaml中启用统计数据收集
- 配置太阳能传感器时未搭配电池追踪以统计自耗量
- 电网监控使用不匹配的传感器对(消耗与回传)
- utility_meter的周期设置与账单周期不一致
Quick Start
快速开始
Step 1: Configure Energy Sensors
步骤1:配置能源传感器
yaml
undefinedyaml
undefinedconfiguration.yaml
configuration.yaml
template:
- sensor:
-
name: "Total Energy Consumed" unique_id: total_energy_consumed unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.energy_meter') | float(0)) }}"
-
name: "Solar Production" unique_id: solar_production unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.solar_meter') | float(0)) }}"
-
**Why this matters:** Proper `state_class` enables Home Assistant to automatically create statistics and energy dashboard integration. Without it, your sensors won't appear in the energy dashboard.template:
- sensor:
-
name: "Total Energy Consumed" unique_id: total_energy_consumed unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.energy_meter') | float(0)) }}"
-
name: "Solar Production" unique_id: solar_production unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.solar_meter') | float(0)) }}"
-
**为什么这很重要:** 正确的`state_class`可让Home Assistant自动创建统计数据并集成到能源仪表盘。如果没有该设置,传感器将不会显示在能源仪表盘中。Step 2: Set Up Utility Meter for Billing Cycles
步骤2:为账单周期设置Utility Meter
yaml
undefinedyaml
undefinedconfiguration.yaml
configuration.yaml
utility_meter:
daily_energy:
source: sensor.total_energy_consumed
cycle: daily
offset: [hours: 0]
monthly_energy:
source: sensor.total_energy_consumed
cycle: monthly
offset: [days: 0]
daily_solar:
source: sensor.solar_production
cycle: daily
offset: [hours: 0]
**Why this matters:** Utility meters automatically reset at specified intervals and track consumption periods for billing analysis.utility_meter:
daily_energy:
source: sensor.total_energy_consumed
cycle: daily
offset: [hours: 0]
monthly_energy:
source: sensor.total_energy_consumed
cycle: monthly
offset: [days: 0]
daily_solar:
source: sensor.solar_production
cycle: daily
offset: [hours: 0]
**为什么这很重要:** Utility Meter会在指定时间自动重置,并按周期追踪能耗,方便账单分析。Step 3: Enable Statistics Recorder
步骤3:启用统计数据记录器
yaml
undefinedyaml
undefinedconfiguration.yaml
configuration.yaml
recorder:
db_url: !secret database_url # Optional but recommended
auto_purge: true
auto_purge_days: 90 # Adjust retention as needed
Include sensors with state_class for statistics
include:
entities:
- sensor.total_energy_consumed
- sensor.solar_production
- sensor.grid_consumption
- sensor.grid_return
**Why this matters:** Statistics require the recorder to be properly configured. Without explicit inclusion, energy sensors may not generate statistics for long-term analysis.recorder:
db_url: !secret database_url # 可选但推荐
auto_purge: true
auto_purge_days: 90 # 根据需要调整保留时长
包含带有state_class的传感器以生成统计数据
include:
entities:
- sensor.total_energy_consumed
- sensor.solar_production
- sensor.grid_consumption
- sensor.grid_return
**为什么这很重要:** 统计数据需要记录器正确配置。如果未明确包含相关传感器,能源传感器可能无法生成用于长期分析的统计数据。Critical Rules
关键规则
Always Do
必须遵守的操作
- Use for cumulative meters that only increase
state_class: total_increasing - Add to all energy sensors
device_class: energy - Set (or appropriate unit) on energy sensors
unit_of_measurement: kWh - Configure utility_meter for billing cycle tracking
- Include all energy sensors in recorder's list
include - Reset cumulative sensors on device restart via template sensor
- Use separate sensors for grid consumption and grid return
- 对于仅递增的累积仪表,使用
state_class: total_increasing - 为所有能源传感器添加属性
device_class: energy - 为能源传感器设置(或合适的单位)
unit_of_measurement: kWh - 配置utility_meter以追踪账单周期能耗
- 将所有能源传感器添加到记录器的列表中
include - 通过模板传感器在设备重启时重置累积传感器
- 为电网消耗和电网回传分别使用独立的传感器
Never Do
绝对禁止的操作
- Use for cumulative meters (breaks statistics)
state_class: measurement - Forget the parameter on utility_meter (may cause midnight resets)
offset - Mix power (W) and energy (kWh) sensors without Riemann sum integration
- Create energy sensors without (they won't work in energy dashboard)
state_class - Assume solar production should subtract from consumption (energy dashboard handles this)
- Configure bidirectional flow on single sensors (use separate consumption/return sensors)
- Skip utility_meter configuration if you need consumption tracking by period
- 为累积仪表使用(会导致统计功能失效)
state_class: measurement - 忘记为utility_meter设置参数(可能导致午夜重置异常)
offset - 未通过Riemann求和集成就混用功率(W)和能量(kWh)传感器
- 创建未设置的能源传感器(无法在能源仪表盘中使用)
state_class - 假设太阳能发电量应直接从能耗中扣除(能源仪表盘会自动处理此逻辑)
- 在单个传感器上配置双向流量(使用独立的消耗/回传传感器)
- 如果需要按周期追踪能耗,跳过utility_meter配置
Common Mistakes
常见错误示例
Wrong:
yaml
sensor:
- platform: template
sensors:
total_energy:
unit_of_measurement: kWh
value_template: "{{ states('sensor.meter') }}"Correct:
yaml
template:
- sensor:
- name: "Total Energy"
unique_id: total_energy
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.meter') | float(0)) }}"Why: Template platform is deprecated. Use template integration with proper state_class and device_class to enable energy dashboard integration.
错误写法:
yaml
sensor:
- platform: template
sensors:
total_energy:
unit_of_measurement: kWh
value_template: "{{ states('sensor.meter') }}"正确写法:
yaml
template:
- sensor:
- name: "Total Energy"
unique_id: total_energy
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.meter') | float(0)) }}"原因: Template平台已被弃用。请使用Template集成,并配置正确的state_class和device_class,以实现能源仪表盘集成。
Known Issues Prevention
已知问题预防方案
| Issue | Root Cause | Solution |
|---|---|---|
| Energy dashboard shows no data | | Use |
| Statistics not generated | Sensors not in recorder's include list | Add sensor entities to recorder config |
| Utility meter not resetting | Wrong | Verify cycle (hourly/daily/monthly) and offset settings |
| Solar self-consumption not calculated | Battery sensors not configured separately | Create charge/discharge sensors for batteries |
| Grid consumption incorrect | Using bidirectional sensor instead of separate sensors | Split into grid_consumption and grid_return |
| Sensor unavailable after restart | Cumulative meter not preserved | Add |
| Statistics showing wrong values | Raw data has negative values or gaps | Use Riemann sum to convert power to energy |
| Utility meter shows wrong period | Offset set incorrectly for timezone | Align offset with local midnight or billing cycle |
| 问题 | 根本原因 | 解决方案 |
|---|---|---|
| 能源仪表盘无数据显示 | 缺少或错误设置 | 为累积仪表使用 |
| 未生成统计数据 | 传感器未加入记录器的include列表 | 将传感器实体添加到记录器配置中 |
| Utility Meter未按时重置 | | 验证周期(小时/日/月等)和偏移设置 |
| 太阳能自耗量未统计 | 未单独配置电池传感器 | 为电池创建充电/放电传感器 |
| 电网消耗数据错误 | 使用双向传感器而非独立传感器 | 拆分为grid_consumption和grid_return两个传感器 |
| 设备重启后传感器不可用 | 累积仪表数据未保留 | 为模板传感器添加 |
| 统计数据值错误 | 原始数据存在负值或间隙 | 使用Riemann求和将功率转换为能量 |
| Utility Meter周期错误 | 偏移设置与时区不匹配 | 调整偏移以匹配当地午夜或账单周期 |
Configuration Reference
配置参考
Energy Sensor Template (YAML)
能源传感器模板(YAML)
yaml
template:
- sensor:
- name: "Main Energy Meter"
unique_id: main_energy_meter
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
availability_template: "{{ states('sensor.meter_input') not in ['unavailable', 'unknown'] }}"
state: "{{ (states('sensor.meter_input') | float(0)) }}"
- name: "Instantaneous Power"
unique_id: instantaneous_power
unit_of_measurement: W
device_class: power
state_class: measurement
state: "{{ (states('sensor.power_meter') | float(0)) }}"Key settings:
- - Identifies as energy sensor for energy dashboard
device_class: energy - - Cumulative meter that only increases
state_class: total_increasing - - Instantaneous values (power, not energy)
state_class: measurement - - Energy units; use W for power
unit_of_measurement: kWh - - Prevents unavailable states from breaking statistics
availability_template
yaml
template:
- sensor:
- name: "Main Energy Meter"
unique_id: main_energy_meter
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
availability_template: "{{ states('sensor.meter_input') not in ['unavailable', 'unknown'] }}"
state: "{{ (states('sensor.meter_input') | float(0)) }}"
- name: "Instantaneous Power"
unique_id: instantaneous_power
unit_of_measurement: W
device_class: power
state_class: measurement
state: "{{ (states('sensor.power_meter') | float(0)) }}"关键设置:
- - 标记为能源传感器,用于能源仪表盘集成
device_class: energy - - 仅递增的累积仪表
state_class: total_increasing - - 瞬时值(功率,非能量)
state_class: measurement - - 能量单位;功率使用W
unit_of_measurement: kWh - - 防止不可用状态破坏统计数据
availability_template
Utility Meter Configuration (YAML)
Utility Meter配置(YAML)
yaml
utility_meter:
daily_consumption:
source: sensor.total_energy_consumed
cycle: daily
offset:
hours: 0
net_consumption: false # true for solar with self-consumption
monthly_consumption:
source: sensor.total_energy_consumed
cycle: monthly
offset:
days: 1 # Reset on 1st of month
hours: 0
peak_consumption:
source: sensor.total_energy_consumed
cycle: weekly
offset:
days: 0 # Reset on Monday
hours: 0Key settings:
- : hourly, daily, weekly, monthly, bimonthly, quarterly, yearly
cycle - : Time adjustment for billing alignment
offset - : Set to true for solar to track self-consumption
net_consumption
yaml
utility_meter:
daily_consumption:
source: sensor.total_energy_consumed
cycle: daily
offset:
hours: 0
net_consumption: false # 带自耗量的太阳能系统设置为true
monthly_consumption:
source: sensor.total_energy_consumed
cycle: monthly
offset:
days: 1 # 每月1日重置
hours: 0
peak_consumption:
source: sensor.total_energy_consumed
cycle: weekly
offset:
days: 0 # 周一重置
hours: 0关键设置:
- : hourly, daily, weekly, monthly, bimonthly, quarterly, yearly
cycle - : 用于对齐账单周期的时间调整
offset - : 太阳能系统设置为true以追踪自耗量
net_consumption
Riemann Sum Integration (for Power to Energy)
Riemann求和集成(功率转能量)
yaml
integration:
- platform: riemann_sum
name: "Daily Energy from Power"
unique_id: daily_energy_riemann
source: sensor.instantaneous_power
round: 3
unit_prefix: k # Converts W to kWh
unit_time: h
method: trapezoidalKey settings:
- : Power sensor (W) to integrate
source - - Converts watts to kilowatts
unit_prefix: k - or
method: trapezoidalfor integration algorithmleft - - Decimal precision
round: 3
yaml
integration:
- platform: riemann_sum
name: "Daily Energy from Power"
unique_id: daily_energy_riemann
source: sensor.instantaneous_power
round: 3
unit_prefix: k # 将W转换为kWh
unit_time: h
method: trapezoidal关键设置:
- : 要转换的功率传感器(W)
source - - 将瓦特转换为千瓦
unit_prefix: k - 或
method: trapezoidal- 集成算法left - - 小数精度
round: 3
Common Patterns
常见配置模式
Grid Monitoring (Consumption + Return)
电网监控(消耗+回传)
yaml
template:
- sensor:
- name: "Grid Consumption"
unique_id: grid_consumption
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.grid_import') | float(0)) }}"
- name: "Grid Return"
unique_id: grid_return
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.grid_export') | float(0)) }}"yaml
template:
- sensor:
- name: "Grid Consumption"
unique_id: grid_consumption
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.grid_import') | float(0)) }}"
- name: "Grid Return"
unique_id: grid_return
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.grid_export') | float(0)) }}"Solar Production with Battery
带电池的太阳能系统
yaml
template:
- sensor:
- name: "Solar Production"
unique_id: solar_production
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.solar_meter') | float(0)) }}"
- name: "Battery Charge"
unique_id: battery_charge
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.battery_charge_meter') | float(0)) }}"
- name: "Battery Discharge"
unique_id: battery_discharge
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.battery_discharge_meter') | float(0)) }}"yaml
template:
- sensor:
- name: "Solar Production"
unique_id: solar_production
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.solar_meter') | float(0)) }}"
- name: "Battery Charge"
unique_id: battery_charge
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.battery_charge_meter') | float(0)) }}"
- name: "Battery Discharge"
unique_id: battery_discharge
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.battery_discharge_meter') | float(0)) }}"Device Power Monitoring (Smart Plug)
设备功率监控(智能插座)
yaml
template:
- sensor:
- name: "Device Energy Today"
unique_id: device_energy_today
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.device_energy') | float(0)) }}"
utility_meter:
device_daily_energy:
source: sensor.device_energy_today
cycle: daily
offset:
hours: 0yaml
template:
- sensor:
- name: "Device Energy Today"
unique_id: device_energy_today
unit_of_measurement: kWh
device_class: energy
state_class: total_increasing
state: "{{ (states('sensor.device_energy') | float(0)) }}"
utility_meter:
device_daily_energy:
source: sensor.device_energy_today
cycle: daily
offset:
hours: 0Bundled Resources
配套资源
References
参考文档
Located in :
references/- - Comprehensive state_class reference
state-class-guide.md - - All energy-related device classes
device-class-reference.md - - Billing cycle configurations
utility-meter-patterns.md - - Step-by-step energy dashboard guide
energy-dashboard-setup.md - - Solar panel and battery tracking
solar-integration.md
Note: For deep dives on specific topics, see the reference files above.
位于目录下:
references/- - 全面的state_class参考指南
state-class-guide.md - - 所有能源相关的device_class
device-class-reference.md - - 账单周期配置模式
utility-meter-patterns.md - - 能源仪表盘分步设置指南
energy-dashboard-setup.md - - 太阳能板与电池追踪
solar-integration.md
注意: 如需深入了解特定主题,请参阅上述参考文档。
Assets
模板资产
Located in :
assets/- - Complete sensor template
energy-sensors-template.yaml - - Meter configurations for all scenarios
utility-meter-examples.yaml - - Dashboard card configuration
energy-dashboard-card.yaml
Copy these templates as starting points for your implementation.
位于目录下:
assets/- - 完整的传感器模板
energy-sensors-template.yaml - - 全场景仪表配置示例
utility-meter-examples.yaml - - 仪表盘卡片配置
energy-dashboard-card.yaml
可复制这些模板作为您实现的起点。
Context7 Documentation
Context7文档库
For current documentation, use these Context7 library IDs:
| Library ID | Purpose |
|---|---|
| User docs - energy, solar, grid, utility_meter |
| Integration docs for specific devices |
| Template sensor reference |
如需最新文档,请使用以下Context7库ID:
| 库ID | 用途 |
|---|---|
| 用户文档 - 能源、太阳能、电网、utility_meter |
| 特定设备的集成文档 |
| 模板传感器参考文档 |
Official Documentation
官方文档
Troubleshooting
故障排除
Energy Dashboard Shows No Data
能源仪表盘无数据显示
Symptoms: Energy dashboard loads but shows "No data available" or blank graphs.
Solution:
bash
undefined症状: 能源仪表盘加载后显示“无可用数据”或空白图表。
解决方案:
bash
undefined1. Verify sensors exist and have state_class
1. 验证传感器存在且配置了state_class
developer-tools > States > Search for energy/solar sensors
开发者工具 > 状态 > 搜索能源/太阳能传感器
2. Check recorder includes sensors
2. 检查记录器是否包含相关传感器
In configuration.yaml:
在configuration.yaml中:
recorder:
include:
entities:
- sensor.total_energy_consumed
- sensor.solar_production
recorder:
include:
entities:
- sensor.total_energy_consumed
- sensor.solar_production
3. Restart Home Assistant
3. 重启Home Assistant
4. Wait 10 minutes for statistics to generate
4. 等待10分钟让统计数据生成
5. Check Developer Tools > Statistics
5. 检查开发者工具 > 统计数据
undefinedundefinedUtility Meter Not Resetting
Utility Meter未重置
Symptoms: Utility meter keeps accumulating without resetting at expected time.
Solution:
yaml
undefined症状: Utility Meter持续累积数据,未在预期时间重置。
解决方案:
yaml
undefinedCheck offset configuration
检查offset配置
utility_meter:
daily_energy:
source: sensor.total_energy_consumed
cycle: daily
# For midnight reset in your timezone:
offset:
hours: 0
# For reset at 6 AM:
# offset:
# hours: 6
undefinedutility_meter:
daily_energy:
source: sensor.total_energy_consumed
cycle: daily
# 为匹配您所在时区的午夜重置:
offset:
hours: 0
# 如需在早上6点重置:
# offset:
# hours: 6
undefinedSolar Self-Consumption Not Tracking
太阳能自耗量未追踪
Symptoms: Self-consumption percentage is 0% or not shown.
Solution:
yaml
undefined症状: 自耗量百分比显示为0%或未显示。
解决方案:
yaml
undefinedCreate separate battery sensors
创建独立的电池传感器
template:
- sensor:
-
name: "Battery Charge" unique_id: battery_charge unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.battery_charge_meter') | float(0)) }}"
-
name: "Battery Discharge" unique_id: battery_discharge unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.battery_discharge_meter') | float(0)) }}"
-
template:
- sensor:
-
name: "Battery Charge" unique_id: battery_charge unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.battery_charge_meter') | float(0)) }}"
-
name: "Battery Discharge" unique_id: battery_discharge unit_of_measurement: kWh device_class: energy state_class: total_increasing state: "{{ (states('sensor.battery_discharge_meter') | float(0)) }}"
-
In energy dashboard configuration, set:
在能源仪表盘配置中设置:
- Source: Solar production sensor
- 来源:太阳能发电量传感器
- Battery (optional): Both charge and discharge sensors
- 电池(可选):充电和放电传感器
undefinedundefinedSensor Shows "unknown" or "unavailable"
传感器显示“unknown”或“unavailable”
Symptoms: Sensor state stuck on "unknown" or "unavailable" in Developer Tools.
Solution:
yaml
undefined症状: 在开发者工具中,传感器状态停留在“unknown”或“unavailable”。
解决方案:
yaml
undefinedAdd availability_template
添加availability_template
template:
- sensor:
- name: "Energy Meter" unique_id: energy_meter unit_of_measurement: kWh device_class: energy state_class: total_increasing availability_template: "{{ states('sensor.meter_input') not in ['unavailable', 'unknown'] }}" state: "{{ (states('sensor.meter_input') | float(0)) }}"
undefinedtemplate:
- sensor:
- name: "Energy Meter" unique_id: energy_meter unit_of_measurement: kWh device_class: energy state_class: total_increasing availability_template: "{{ states('sensor.meter_input') not in ['unavailable', 'unknown'] }}" state: "{{ (states('sensor.meter_input') | float(0)) }}"
undefinedSetup Checklist
设置检查清单
Before using this skill, verify:
- Home Assistant is running 2023.1 or later
- You have access to configuration.yaml (File Editor or VS Code add-on)
- You have at least one energy sensor available (grid meter, smart plug, integration sensor)
- You've decided on meter types (grid consumption/return, solar, battery, individual devices)
- You understand your billing cycle (daily, monthly, etc.)
- Recorder is enabled in configuration.yaml
- You've identified all devices/meters to monitor
使用本技能前,请确认以下事项:
- Home Assistant版本为2023.1或更高
- 可访问configuration.yaml(通过文件编辑器或VS Code插件)
- 至少有一个可用的能源传感器(电网仪表、智能插座、集成传感器)
- 已确定要监控的仪表类型(电网消耗/回传、太阳能、电池、单个设备)
- 已了解您的账单周期(日、月等)
- 记录器已在configuration.yaml中启用
- 已识别所有需要监控的设备/仪表