1740 lines
64 KiB
Markdown
1740 lines
64 KiB
Markdown
|
||
|
||
|
||
|
||
```yaml
|
||
# ==================== 南方电网完整历史数据拼接传感器 ====================
|
||
# 请将此配置添加到 configuration.yaml 文件中
|
||
# 如果已有 template: 部分,请将传感器添加到现有的 - sensor: 列表中
|
||
# ==================== 南方电网完整历史数据拼接传感器(属性类型已修正)====================
|
||
template:
|
||
- sensor:
|
||
# ========== 1. 核心:历史数据拼接(上月+本月每日数据) ==========
|
||
- name: "南方电网历史拼接"
|
||
unique_id: csg_history_combined_0800041935246530
|
||
state: "{{ now().strftime('%Y-%m-%d') }}"
|
||
icon: mdi:chart-line
|
||
attributes:
|
||
history_day_value: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{{ l_m + t_m }}
|
||
total_days: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{{ (l_m + t_m) | length }}
|
||
last_month_days: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{{ (last_month | length) if last_month else 0 }}
|
||
this_month_days: >
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{{ (this_month | length) if this_month else 0 }}
|
||
date_range: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{% set all_data = l_m + t_m %}
|
||
{% if all_data | length > 0 %}
|
||
{{ all_data[0].date }} 至 {{ all_data[-1].date }}
|
||
{% else %}
|
||
无数据
|
||
{% endif %}
|
||
last_update: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
|
||
|
||
# ========== 2. 年度历史数据拼接 ==========
|
||
- name: "南方电网年度历史拼接"
|
||
unique_id: csg_yearly_history_combined_0800041935246530
|
||
state: "{{ now().strftime('%Y-%m') }}"
|
||
icon: mdi:calendar-multiple
|
||
attributes:
|
||
history_month_value: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{% set l_y = last_year if last_year is not none else [] %}
|
||
{% set t_y = this_year if this_year is not none else [] %}
|
||
{{ l_y + t_y }}
|
||
total_months: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{% set l_y = last_year if last_year is not none else [] %}
|
||
{% set t_y = this_year if this_year is not none else [] %}
|
||
{{ (l_y + t_y) | length }}
|
||
last_year_months: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{{ (last_year | length) if last_year else 0 }}
|
||
this_year_months: >
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{{ (this_year | length) if this_year else 0 }}
|
||
last_update: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
|
||
|
||
# ========== 3. 近30日平均用电 ==========
|
||
- name: "近30日平均用电"
|
||
unique_id: csg_30days_average_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
state_class: measurement
|
||
icon: mdi:chart-bar
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set recent = data[-30:] %}
|
||
{% set total = recent | map(attribute='kwh') | sum %}
|
||
{% set count = recent | length %}
|
||
{{ (total / count) | round(2) if count > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
calculation_days: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{{ data[-30:] | length if data else 0 }}
|
||
total_usage: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-30:] | map(attribute='kwh') | sum) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 4. 近30日最高用电 ==========
|
||
- name: "近30日最高用电"
|
||
unique_id: csg_30days_max_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
state_class: measurement
|
||
icon: mdi:arrow-up-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-30:] | map(attribute='kwh') | max) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
date: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set max_kwh = data[-30:] | map(attribute='kwh') | max %}
|
||
{% set max_item = data[-30:] | selectattr('kwh', 'equalto', max_kwh) | first %}
|
||
{{ max_item.date if max_item else '未知' }}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
formatted_date: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set max_kwh = data[-30:] | map(attribute='kwh') | max %}
|
||
{% set max_item = data[-30:] | selectattr('kwh', 'equalto', max_kwh) | first %}
|
||
{% if max_item %}
|
||
{{ max_item.date[5:7] }}月{{ max_item.date[8:10] }}日
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
weekday: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set max_kwh = data[-30:] | map(attribute='kwh') | max %}
|
||
{% set max_item = data[-30:] | selectattr('kwh', 'equalto', max_kwh) | first %}
|
||
{% if max_item %}
|
||
{% set weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] %}
|
||
{{ weekdays[strptime(max_item.date, '%Y-%m-%d').weekday()] }}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
|
||
# ========== 5. 近30日最低用电 ==========
|
||
- name: "近30日最低用电"
|
||
unique_id: csg_30days_min_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
state_class: measurement
|
||
icon: mdi:arrow-down-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-30:] | map(attribute='kwh') | min) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
date: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set min_kwh = data[-30:] | map(attribute='kwh') | min %}
|
||
{% set min_item = data[-30:] | selectattr('kwh', 'equalto', min_kwh) | first %}
|
||
{{ min_item.date if min_item else '未知' }}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
formatted_date: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set min_kwh = data[-30:] | map(attribute='kwh') | min %}
|
||
{% set min_item = data[-30:] | selectattr('kwh', 'equalto', min_kwh) | first %}
|
||
{% if min_item %}
|
||
{{ min_item.date[5:7] }}月{{ min_item.date[8:10] }}日
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
weekday: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set min_kwh = data[-30:] | map(attribute='kwh') | min %}
|
||
{% set min_item = data[-30:] | selectattr('kwh', 'equalto', min_kwh) | first %}
|
||
{% if min_item %}
|
||
{% set weekdays = ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] %}
|
||
{{ weekdays[strptime(min_item.date, '%Y-%m-%d').weekday()] }}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
{% else %}
|
||
未知
|
||
{% endif %}
|
||
|
||
# ========== 6. 近7日平均用电 ==========
|
||
- name: "近7日平均用电"
|
||
unique_id: csg_7days_average_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
state_class: measurement
|
||
icon: mdi:calendar-week
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set recent = data[-7:] %}
|
||
{% set total = recent | map(attribute='kwh') | sum %}
|
||
{% set count = recent | length %}
|
||
{{ (total / count) | round(2) if count > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
total_usage: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-7:] | map(attribute='kwh') | sum) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 7. 近7日最高用电 ==========
|
||
- name: "近7日最高用电"
|
||
unique_id: csg_7days_max_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
icon: mdi:arrow-up-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-7:] | map(attribute='kwh') | max) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 8. 近7日最低用电 ==========
|
||
- name: "近7日最低用电"
|
||
unique_id: csg_7days_min_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
icon: mdi:arrow-down-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{{ (data[-7:] | map(attribute='kwh') | min) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 9. 工作日平均用电 ==========
|
||
- name: "工作日平均用电"
|
||
unique_id: csg_weekday_average_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
icon: mdi:briefcase
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set weekday_data = [] %}
|
||
{% for item in data[-30:] %}
|
||
{% set day_of_week = strptime(item.date, '%Y-%m-%d').weekday() %}
|
||
{% if day_of_week < 5 %}
|
||
{% set weekday_data = weekday_data + [item.kwh] %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{% if weekday_data | length > 0 %}
|
||
{{ ((weekday_data | sum) / (weekday_data | length)) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set count = 0 %}
|
||
{% for item in data[-30:] %}
|
||
{% set day_of_week = strptime(item.date, '%Y-%m-%d').weekday() %}
|
||
{% if day_of_week < 5 %}
|
||
{% set count = count + 1 %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ count }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 10. 周末平均用电 ==========
|
||
- name: "周末平均用电"
|
||
unique_id: csg_weekend_average_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
device_class: energy
|
||
icon: mdi:home-heart
|
||
state: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set weekend_data = [] %}
|
||
{% for item in data[-30:] %}
|
||
{% set day_of_week = strptime(item.date, '%Y-%m-%d').weekday() %}
|
||
{% if day_of_week >= 5 %}
|
||
{% set weekend_data = weekend_data + [item.kwh] %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{% if weekend_data | length > 0 %}
|
||
{{ ((weekend_data | sum) / (weekend_data | length)) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nanfangdianwanglishipipinjie', 'history_day_value') %}
|
||
{% if data and data|length > 0 %}
|
||
{% set count = 0 %}
|
||
{% for item in data[-30:] %}
|
||
{% set day_of_week = strptime(item.date, '%Y-%m-%d').weekday() %}
|
||
{% if day_of_week >= 5 %}
|
||
{% set count = count + 1 %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ count }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ========== 11. 预计本月电费 ==========
|
||
- name: "预计本月电费"
|
||
unique_id: csg_estimated_cost_0800041935246530
|
||
|
||
|
||
```
|
||
|
||
|
||
|
||
|
||
```yaml
|
||
views:
|
||
- title: 电力监控
|
||
path: power-monitor
|
||
icon: mdi:flash
|
||
cards:
|
||
- type: vertical-stack
|
||
cards:
|
||
# 仪表盘部分
|
||
- type: grid
|
||
columns: 2
|
||
square: false
|
||
cards:
|
||
- type: gauge
|
||
entity: sensor.0800041935246530_yesterday_kwh
|
||
name: 昨日用电
|
||
unit: kWh
|
||
min: 0
|
||
max: 50
|
||
severity:
|
||
green: 0
|
||
yellow: 30
|
||
red: 40
|
||
needle: true
|
||
segments:
|
||
- from: 0
|
||
color: '#4CAF50'
|
||
- from: 30
|
||
color: '#FFC107'
|
||
- from: 40
|
||
color: '#F44336'
|
||
- type: gauge
|
||
entity: sensor.0800041935246530_this_month_total_usage
|
||
name: 当月电量
|
||
unit: kWh
|
||
min: 0
|
||
max: 1000
|
||
severity:
|
||
green: 0
|
||
yellow: 600
|
||
red: 800
|
||
needle: true
|
||
segments:
|
||
- from: 0
|
||
color: '#4CAF50'
|
||
- from: 600
|
||
color: '#FFC107'
|
||
- from: 800
|
||
color: '#F44336'
|
||
|
||
# 关键数据卡片
|
||
- type: grid
|
||
columns: 3
|
||
square: false
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_balance
|
||
name: 账户余额
|
||
icon: mdi:wallet
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_arrears
|
||
name: 欠缴电费
|
||
icon: mdi:alert-circle
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_month_total_cost
|
||
name: 上月电费
|
||
icon: mdi:currency-cny
|
||
graph: none
|
||
|
||
# 本月数据
|
||
- type: horizontal-stack
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_month_total_usage
|
||
name: 本月用电
|
||
icon: mdi:flash
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_month_total_cost
|
||
name: 本月电费
|
||
icon: mdi:currency-cny
|
||
graph: none
|
||
|
||
# 上月数据
|
||
- type: horizontal-stack
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_month_total_usage
|
||
name: 上月用电
|
||
icon: mdi:flash-outline
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_month_total_cost
|
||
name: 上月电费
|
||
icon: mdi:currency-cny
|
||
graph: none
|
||
|
||
# 今年数据
|
||
- type: horizontal-stack
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_year_total_usage
|
||
name: 今年用电
|
||
icon: mdi:calendar-star
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_year_total_cost
|
||
name: 今年电费
|
||
icon: mdi:currency-cny
|
||
graph: none
|
||
|
||
# 去年数据
|
||
- type: horizontal-stack
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_year_total_usage
|
||
name: 去年用电
|
||
icon: mdi:calendar-clock
|
||
graph: none
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_year_total_cost
|
||
name: 去年电费
|
||
icon: mdi:currency-cny
|
||
graph: none
|
||
|
||
# 30日统计卡片
|
||
- type: grid
|
||
columns: 3
|
||
square: false
|
||
cards:
|
||
- type: markdown
|
||
content: |
|
||
**📈 最高用电**
|
||
{% set data = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% if data and data|length > 0 %}
|
||
<h3 style="color: #EF4444; margin: 5px 0;">{{ (data[-30:] | map(attribute='kwh') | max) | round(1) }} kWh</h3>
|
||
<small style="color: #666;">
|
||
{% set max_kwh = data[-30:] | map(attribute='kwh') | max %}
|
||
{% set max_item = data[-30:] | selectattr('kwh', 'equalto', max_kwh) | first %}
|
||
{{ max_item.date[5:7] }}/{{ max_item.date[8:10] }}
|
||
</small>
|
||
{% else %}
|
||
<h3 style="color: #EF4444; margin: 5px 0;">暂无数据</h3>
|
||
{% endif %}
|
||
- type: markdown
|
||
content: |
|
||
**📉 最低用电**
|
||
{% set data = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% if data and data|length > 0 %}
|
||
<h3 style="color: #3B82F6; margin: 5px 0;">{{ (data[-30:] | map(attribute='kwh') | min) | round(1) }} kWh</h3>
|
||
<small style="color: #666;">
|
||
{% set min_kwh = data[-30:] | map(attribute='kwh') | min %}
|
||
{% set min_item = data[-30:] | selectattr('kwh', 'equalto', min_kwh) | first %}
|
||
{{ min_item.date[5:7] }}/{{ min_item.date[8:10] }}
|
||
</small>
|
||
{% else %}
|
||
<h3 style="color: #3B82F6; margin: 5px 0;">暂无数据</h3>
|
||
{% endif %}
|
||
- type: markdown
|
||
content: |
|
||
**🎯 平均用电**
|
||
{% set data = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% if data and data|length > 0 %}
|
||
<h3 style="color: #10B981; margin: 5px 0;">{{ ((data[-30:] | map(attribute='kwh') | sum) / (data[-30:] | length)) | round(1) }} kWh</h3>
|
||
<small style="color: #666;">近30日平均</small>
|
||
{% else %}
|
||
<h3 style="color: #10B981; margin: 5px 0;">暂无数据</h3>
|
||
{% endif %}
|
||
|
||
# 近30日用电趋势图
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📊 近30日用电趋势
|
||
show_states: true
|
||
colorize_states: true
|
||
graph_span: 30d
|
||
apex_config:
|
||
chart:
|
||
height: 300px
|
||
dataLabels:
|
||
enabled: false
|
||
stroke:
|
||
curve: smooth
|
||
width: 3
|
||
fill:
|
||
type: gradient
|
||
gradient:
|
||
shadeIntensity: 1
|
||
opacityFrom: 0.7
|
||
opacityTo: 0.3
|
||
markers:
|
||
size: 4
|
||
hover:
|
||
size: 6
|
||
tooltip:
|
||
x:
|
||
format: MM月dd日
|
||
grid:
|
||
borderColor: '#e0e0e0'
|
||
strokeDashArray: 4
|
||
series:
|
||
- entity: sensor.0800041935246530_this_month_total_usage
|
||
type: area
|
||
name: 日用电量
|
||
color: '#8B5CF6'
|
||
unit: kWh
|
||
curve: smooth
|
||
data_generator: |
|
||
const dailyData = entity.attributes.this_month_by_day || [];
|
||
const recentData = dailyData.slice(-30);
|
||
return recentData.map(item => {
|
||
const date = new Date(item.date);
|
||
return [date.getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
yaxis:
|
||
- min: 0
|
||
decimals: 1
|
||
apex_config:
|
||
title:
|
||
text: 用电量 (kWh)
|
||
|
||
# 当月每日用电详情
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📅 当月每日用电详情
|
||
show_states: true
|
||
graph_span: 35d
|
||
span:
|
||
start: month
|
||
apex_config:
|
||
chart:
|
||
height: 320px
|
||
plotOptions:
|
||
bar:
|
||
borderRadius: 4
|
||
columnWidth: 70%
|
||
dataLabels:
|
||
enabled: false
|
||
stroke:
|
||
width:
|
||
- 0
|
||
- 3
|
||
tooltip:
|
||
x:
|
||
format: MM月dd日
|
||
shared: true
|
||
series:
|
||
- entity: sensor.0800041935246530_this_month_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const data = entity.attributes.this_month_by_day || [];
|
||
return data.map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每日电量
|
||
color: '#2196F3'
|
||
yaxis_id: power
|
||
- entity: sensor.0800041935246530_this_month_total_cost
|
||
type: line
|
||
data_generator: |
|
||
const data = entity.attributes.this_month_by_day || [];
|
||
return data.map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.cost || item.fee) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每日电费
|
||
color: '#FF9800'
|
||
yaxis_id: fee
|
||
curve: smooth
|
||
yaxis:
|
||
- id: power
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电量 (kWh)
|
||
- id: fee
|
||
opposite: true
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电费 (元)
|
||
|
||
# 上月每日用电详情
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📊 上月每日用电详情
|
||
show_states: true
|
||
graph_span: 35d
|
||
span:
|
||
start: month
|
||
offset: '-1M'
|
||
apex_config:
|
||
chart:
|
||
height: 300px
|
||
plotOptions:
|
||
bar:
|
||
borderRadius: 4
|
||
columnWidth: 70%
|
||
dataLabels:
|
||
enabled: false
|
||
stroke:
|
||
width:
|
||
- 0
|
||
- 3
|
||
tooltip:
|
||
x:
|
||
format: MM月dd日
|
||
shared: true
|
||
series:
|
||
- entity: sensor.0800041935246530_last_month_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const data = entity.attributes.last_month_by_day || [];
|
||
return data.map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每日电量
|
||
color: '#9C27B0'
|
||
yaxis_id: power
|
||
- entity: sensor.0800041935246530_last_month_total_cost
|
||
type: line
|
||
data_generator: |
|
||
const data = entity.attributes.last_month_by_day || [];
|
||
return data.map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.cost || item.fee) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每日电费
|
||
color: '#E91E63'
|
||
yaxis_id: fee
|
||
curve: smooth
|
||
yaxis:
|
||
- id: power
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电量 (kWh)
|
||
- id: fee
|
||
opposite: true
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电费 (元)
|
||
|
||
# 今年每月用电趋势
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📈 今年每月用电趋势
|
||
show_states: true
|
||
graph_span: 1y
|
||
span:
|
||
start: year
|
||
apex_config:
|
||
chart:
|
||
height: 320px
|
||
plotOptions:
|
||
bar:
|
||
borderRadius: 6
|
||
columnWidth: 60%
|
||
dataLabels:
|
||
enabled: true
|
||
enabledOnSeries:
|
||
- 0
|
||
style:
|
||
fontSize: 11px
|
||
stroke:
|
||
width:
|
||
- 0
|
||
- 3
|
||
tooltip:
|
||
x:
|
||
format: yyyy年MM月
|
||
series:
|
||
- entity: sensor.0800041935246530_this_year_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const data = entity.attributes.this_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每月电量
|
||
color: '#4CAF50'
|
||
yaxis_id: power
|
||
- entity: sensor.0800041935246530_this_year_total_cost
|
||
type: line
|
||
data_generator: |
|
||
const data = entity.attributes.this_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.cost || item.fee) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 每月电费
|
||
color: '#FF5722'
|
||
yaxis_id: fee
|
||
curve: smooth
|
||
yaxis:
|
||
- id: power
|
||
decimals: 0
|
||
apex_config:
|
||
title:
|
||
text: 电量 (kWh)
|
||
- id: fee
|
||
opposite: true
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电费 (元)
|
||
|
||
# 今年vs去年对比
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 🔄 今年 vs 去年用电对比
|
||
graph_span: 1y
|
||
span:
|
||
start: year
|
||
apex_config:
|
||
chart:
|
||
height: 340px
|
||
type: bar
|
||
plotOptions:
|
||
bar:
|
||
horizontal: false
|
||
columnWidth: 65%
|
||
borderRadius: 4
|
||
dataLabels:
|
||
enabled: false
|
||
stroke:
|
||
show: true
|
||
width: 2
|
||
colors:
|
||
- transparent
|
||
tooltip:
|
||
x:
|
||
format: MM月
|
||
shared: true
|
||
legend:
|
||
position: top
|
||
series:
|
||
- entity: sensor.0800041935246530_this_year_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const data = entity.attributes.this_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 今年
|
||
color: '#00BCD4'
|
||
- entity: sensor.0800041935246530_last_year_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const currentYear = new Date().getFullYear();
|
||
const data = entity.attributes.last_year_by_month || [];
|
||
return data.map(item => {
|
||
const date = new Date(item.month || item.date);
|
||
date.setFullYear(currentYear);
|
||
return [date.getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 去年同期
|
||
color: '#FFC107'
|
||
yaxis:
|
||
- decimals: 0
|
||
apex_config:
|
||
title:
|
||
text: 用电量 (kWh)
|
||
|
||
# 去年每月用电详情
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📉 去年每月用电详情
|
||
graph_span: 1y
|
||
span:
|
||
start: year
|
||
offset: '-1y'
|
||
apex_config:
|
||
chart:
|
||
height: 300px
|
||
plotOptions:
|
||
bar:
|
||
borderRadius: 4
|
||
stroke:
|
||
width:
|
||
- 0
|
||
- 3
|
||
tooltip:
|
||
x:
|
||
format: yyyy年MM月
|
||
series:
|
||
- entity: sensor.0800041935246530_last_year_total_usage
|
||
type: column
|
||
data_generator: |
|
||
const data = entity.attributes.last_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 去年每月电量
|
||
color: '#9C27B0'
|
||
yaxis_id: power
|
||
- entity: sensor.0800041935246530_last_year_total_cost
|
||
type: line
|
||
data_generator: |
|
||
const data = entity.attributes.last_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.cost || item.fee) || 0];
|
||
});
|
||
extend_to: false
|
||
float_precision: 2
|
||
name: 去年每月电费
|
||
color: '#E91E63'
|
||
yaxis_id: fee
|
||
curve: smooth
|
||
yaxis:
|
||
- id: power
|
||
decimals: 0
|
||
apex_config:
|
||
title:
|
||
text: 电量 (kWh)
|
||
- id: fee
|
||
opposite: true
|
||
decimals: 2
|
||
apex_config:
|
||
title:
|
||
text: 电费 (元)
|
||
|
||
```
|
||
|
||
|
||
|
||
|
||
# 🔌 南方电网电力监控完整配置指南
|
||
|
||
## 📋 目录
|
||
1. [前置要求](#前置要求)
|
||
2. [完整配置文件](#完整配置文件)
|
||
3. [安装步骤](#安装步骤)
|
||
4. [调试方法](#调试方法)
|
||
5. [常见问题](#常见问题)
|
||
|
||
---
|
||
|
||
## 前置要求
|
||
|
||
### 必需组件
|
||
- ✅ Home Assistant 2023.x 或更高版本
|
||
- ✅ 南方电网集成(HACS安装)
|
||
- ✅ ApexCharts Card(HACS前端安装)
|
||
|
||
### 安装必需组件
|
||
```bash
|
||
# 1. HACS → 集成 → 搜索 "南方电网" → 安装
|
||
# 2. HACS → 前端 → 搜索 "ApexCharts Card" → 安装
|
||
# 3. 重启 Home Assistant
|
||
```
|
||
|
||
---
|
||
|
||
## 完整配置文件
|
||
|
||
### 📝 configuration.yaml
|
||
|
||
将以下内容添加到 `configuration.yaml`:
|
||
|
||
```yaml
|
||
template:
|
||
- sensor:
|
||
# ==================== 1. 历史数据拼接(上月+本月) ====================
|
||
- name: "南方电网历史拼接"
|
||
unique_id: csg_history_combined_0800041935246530
|
||
state: "{{ now().strftime('%Y-%m-%d') }}"
|
||
icon: mdi:chart-line
|
||
attributes:
|
||
history_day_value: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{{ l_m + t_m }}
|
||
total_days: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{{ (l_m + t_m) | length }}
|
||
last_month_days: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{{ (last_month | length) if last_month else 0 }}
|
||
this_month_days: >
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{{ (this_month | length) if this_month else 0 }}
|
||
date_range: >
|
||
{% set last_month = state_attr('sensor.0800041935246530_last_month_total_usage', 'last_month_by_day') %}
|
||
{% set this_month = state_attr('sensor.0800041935246530_this_month_total_usage', 'this_month_by_day') %}
|
||
{% set l_m = last_month if last_month is not none else [] %}
|
||
{% set t_m = this_month if this_month is not none else [] %}
|
||
{% set all_data = l_m + t_m %}
|
||
{% if all_data | length > 0 %}
|
||
{{ all_data[0].date }} 至 {{ all_data[-1].date }}
|
||
{% else %}
|
||
无数据
|
||
{% endif %}
|
||
last_update: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
|
||
|
||
# ==================== 2. 年度历史拼接(去年+今年) ====================
|
||
- name: "南方电网年度历史拼接"
|
||
unique_id: csg_yearly_history_combined_0800041935246530
|
||
state: "{{ now().strftime('%Y-%m') }}"
|
||
icon: mdi:calendar-month
|
||
attributes:
|
||
history_month_value: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{% set l_y = last_year if last_year is not none else [] %}
|
||
{% set t_y = this_year if this_year is not none else [] %}
|
||
{{ l_y + t_y }}
|
||
total_months: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{% set l_y = last_year if last_year is not none else [] %}
|
||
{% set t_y = this_year if this_year is not none else [] %}
|
||
{{ (l_y + t_y) | length }}
|
||
last_year_months: >
|
||
{% set last_year = state_attr('sensor.0800041935246530_last_year_total_usage', 'last_year_by_month') %}
|
||
{{ (last_year | length) if last_year else 0 }}
|
||
this_year_months: >
|
||
{% set this_year = state_attr('sensor.0800041935246530_this_year_total_usage', 'this_year_by_month') %}
|
||
{{ (this_year | length) if this_year else 0 }}
|
||
last_update: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
|
||
|
||
# ==================== 3. 近30日统计 ====================
|
||
- name: "近30日最高用电"
|
||
unique_id: csg_30d_max_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:arrow-up-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set max_item = recent | sort(attribute='kwh', reverse=true) | first %}
|
||
{{ max_item.kwh if max_item else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
date: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set max_item = recent | sort(attribute='kwh', reverse=true) | first %}
|
||
{{ max_item.date if max_item else 'N/A' }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
formatted_date: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set max_item = recent | sort(attribute='kwh', reverse=true) | first %}
|
||
{% if max_item %}
|
||
{{ as_timestamp(max_item.date) | timestamp_custom('%m月%d日') }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
weekday: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set max_item = recent | sort(attribute='kwh', reverse=true) | first %}
|
||
{% if max_item %}
|
||
{% set weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] %}
|
||
{{ weekdays[as_timestamp(max_item.date) | timestamp_custom('%w') | int] }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
|
||
- name: "近30日最低用电"
|
||
unique_id: csg_30d_min_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:arrow-down-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set min_item = recent | sort(attribute='kwh') | first %}
|
||
{{ min_item.kwh if min_item else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
date: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set min_item = recent | sort(attribute='kwh') | first %}
|
||
{{ min_item.date if min_item else 'N/A' }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
formatted_date: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set min_item = recent | sort(attribute='kwh') | first %}
|
||
{% if min_item %}
|
||
{{ as_timestamp(min_item.date) | timestamp_custom('%m月%d日') }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
weekday: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set min_item = recent | sort(attribute='kwh') | first %}
|
||
{% if min_item %}
|
||
{% set weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'] %}
|
||
{{ weekdays[as_timestamp(min_item.date) | timestamp_custom('%w') | int] }}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
{% else %}
|
||
N/A
|
||
{% endif %}
|
||
|
||
- name: "近30日平均用电"
|
||
unique_id: csg_30d_avg_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:chart-bell-curve
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set total = recent | map(attribute='kwh') | map('float') | sum %}
|
||
{{ (total / recent | length) | round(2) if recent | length > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
total_usage: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{{ (recent | map(attribute='kwh') | map('float') | sum) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{{ data[-30:] | length if data else 0 }}
|
||
|
||
# ==================== 4. 近7日统计 ====================
|
||
- name: "近7日最高用电"
|
||
unique_id: csg_7d_max_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:arrow-up-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-7:] %}
|
||
{% set max_item = recent | sort(attribute='kwh', reverse=true) | first %}
|
||
{{ max_item.kwh if max_item else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
- name: "近7日最低用电"
|
||
unique_id: csg_7d_min_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:arrow-down-bold
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-7:] %}
|
||
{% set min_item = recent | sort(attribute='kwh') | first %}
|
||
{{ min_item.kwh if min_item else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
- name: "近7日平均用电"
|
||
unique_id: csg_7d_avg_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:chart-bell-curve
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-7:] %}
|
||
{% set total = recent | map(attribute='kwh') | map('float') | sum %}
|
||
{{ (total / recent | length) | round(2) if recent | length > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
total_usage: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-7:] %}
|
||
{{ (recent | map(attribute='kwh') | map('float') | sum) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{{ data[-7:] | length if data else 0 }}
|
||
|
||
# ==================== 5. 工作日vs周末 ====================
|
||
- name: "工作日平均用电"
|
||
unique_id: csg_weekday_avg_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:briefcase
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set weekday_data = [] %}
|
||
{% for item in recent %}
|
||
{% set day_of_week = as_timestamp(item.date) | timestamp_custom('%w') | int %}
|
||
{% if day_of_week >= 1 and day_of_week <= 5 %}
|
||
{% set weekday_data = weekday_data + [item.kwh | float] %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ (weekday_data | sum / weekday_data | length) | round(2) if weekday_data | length > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set count = 0 %}
|
||
{% for item in recent %}
|
||
{% set day_of_week = as_timestamp(item.date) | timestamp_custom('%w') | int %}
|
||
{% if day_of_week >= 1 and day_of_week <= 5 %}
|
||
{% set count = count + 1 %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ count }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
- name: "周末平均用电"
|
||
unique_id: csg_weekend_avg_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:home
|
||
state: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set weekend_data = [] %}
|
||
{% for item in recent %}
|
||
{% set day_of_week = as_timestamp(item.date) | timestamp_custom('%w') | int %}
|
||
{% if day_of_week == 0 or day_of_week == 6 %}
|
||
{% set weekend_data = weekend_data + [item.kwh | float] %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ (weekend_data | sum / weekend_data | length) | round(2) if weekend_data | length > 0 else 0 }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
days_count: >
|
||
{% set data = state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') %}
|
||
{% if data %}
|
||
{% set recent = data[-30:] %}
|
||
{% set count = 0 %}
|
||
{% for item in recent %}
|
||
{% set day_of_week = as_timestamp(item.date) | timestamp_custom('%w') | int %}
|
||
{% if day_of_week == 0 or day_of_week == 6 %}
|
||
{% set count = count + 1 %}
|
||
{% endif %}
|
||
{% endfor %}
|
||
{{ count }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
# ==================== 6. 预测与对比 ====================
|
||
- name: "预计本月用电"
|
||
unique_id: csg_predicted_month_usage_0800041935246530
|
||
unit_of_measurement: "kWh"
|
||
icon: mdi:crystal-ball
|
||
state: >
|
||
{% set current_usage = states('sensor.0800041935246530_this_month_total_usage') | float(0) %}
|
||
{% set today = now().day %}
|
||
{% set days_in_month = (now().replace(day=1) + timedelta(days=32)).replace(day=1) - timedelta(days=1) %}
|
||
{% set total_days = days_in_month.day %}
|
||
{% if today > 0 %}
|
||
{{ ((current_usage / today) * total_days) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
- name: "预计本月电费"
|
||
unique_id: csg_predicted_month_cost_0800041935246530
|
||
unit_of_measurement: "元"
|
||
icon: mdi:calculator
|
||
state: >
|
||
{% set current_cost = states('sensor.0800041935246530_this_month_total_cost') | float(0) %}
|
||
{% set today = now().day %}
|
||
{% set days_in_month = (now().replace(day=1) + timedelta(days=32)).replace(day=1) - timedelta(days=1) %}
|
||
{% set total_days = days_in_month.day %}
|
||
{% if today > 0 %}
|
||
{{ ((current_cost / today) * total_days) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
|
||
- name: "环比上月用电变动"
|
||
unique_id: csg_mom_usage_change_0800041935246530
|
||
unit_of_measurement: "%"
|
||
icon: mdi:trending-up
|
||
state: >
|
||
{% set this_month = states('sensor.0800041935246530_this_month_total_usage') | float(0) %}
|
||
{% set last_month = states('sensor.0800041935246530_last_month_total_usage') | float(0) %}
|
||
{% if last_month > 0 %}
|
||
{{ (((this_month - last_month) / last_month) * 100) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
trend: >
|
||
{% set this_month = states('sensor.0800041935246530_this_month_total_usage') | float(0) %}
|
||
{% set last_month = states('sensor.0800041935246530_last_month_total_usage') | float(0) %}
|
||
{% if this_month > last_month %}
|
||
上升
|
||
{% elif this_month < last_month %}
|
||
下降
|
||
{% else %}
|
||
持平
|
||
{% endif %}
|
||
|
||
- name: "同比去年用电变动"
|
||
unique_id: csg_yoy_usage_change_0800041935246530
|
||
unit_of_measurement: "%"
|
||
icon: mdi:calendar-compare
|
||
state: >
|
||
{% set this_year = states('sensor.0800041935246530_this_year_total_usage') | float(0) %}
|
||
{% set last_year = states('sensor.0800041935246530_last_year_total_usage') | float(0) %}
|
||
{% if last_year > 0 %}
|
||
{{ (((this_year - last_year) / last_year) * 100) | round(2) }}
|
||
{% else %}
|
||
0
|
||
{% endif %}
|
||
attributes:
|
||
trend: >
|
||
{% set this_year = states('sensor.0800041935246530_this_year_total_usage') | float(0) %}
|
||
{% set last_year = states('sensor.0800041935246530_last_year_total_usage') | float(0) %}
|
||
{% if this_year > last_year %}
|
||
上升
|
||
{% elif this_year < last_year %}
|
||
下降
|
||
{% else %}
|
||
持平
|
||
{% endif %}
|
||
|
||
# ==================== 7. 预算管理 ====================
|
||
- name: "本月电费剩余预算"
|
||
unique_id: csg_budget_remaining_0800041935246530
|
||
unit_of_measurement: "元"
|
||
icon: mdi:cash-multiple
|
||
state: >
|
||
{% set budget = 200 %}
|
||
{% set current_cost = states('sensor.0800041935246530_this_month_total_cost') | float(0) %}
|
||
{{ (budget - current_cost) | round(2) }}
|
||
attributes:
|
||
budget: 200
|
||
used: >
|
||
{{ states('sensor.0800041935246530_this_month_total_cost') | float(0) | round(2) }}
|
||
budget_usage_percent: >
|
||
{% set budget = 200 %}
|
||
{% set current_cost = states('sensor.0800041935246530_this_month_total_cost') | float(0) %}
|
||
{{ ((current_cost / budget) * 100) | round(2) if budget > 0 else 0 }}
|
||
status: >
|
||
{% set budget = 200 %}
|
||
{% set current_cost = states('sensor.0800041935246530_this_month_total_cost') | float(0) %}
|
||
{% set percent = (current_cost / budget) * 100 if budget > 0 else 0 %}
|
||
{% if percent < 50 %}
|
||
✅ 预算充足
|
||
{% elif percent < 80 %}
|
||
⚠️ 预算适中
|
||
{% elif percent < 100 %}
|
||
🔶 预算紧张
|
||
{% else %}
|
||
🚨 超出预算
|
||
{% endif %}
|
||
```
|
||
|
||
---
|
||
|
||
### 📊 仪表板配置(dashboard.yaml)
|
||
|
||
创建新仪表板或添加到现有仪表板:
|
||
|
||
```yaml
|
||
views:
|
||
- title: 电力监控
|
||
path: power-monitor
|
||
icon: mdi:flash
|
||
badges:
|
||
- entity: sensor.0800041935246530_balance
|
||
- entity: sensor.yu_ji_ben_yue_dian_fei
|
||
- entity: sensor.jin_30_ri_ping_jun_yong_dian
|
||
cards:
|
||
# ========== 顶部仪表 ==========
|
||
- type: grid
|
||
columns: 2
|
||
cards:
|
||
- type: gauge
|
||
entity: sensor.0800041935246530_yesterday_kwh
|
||
name: 昨日用电
|
||
min: 0
|
||
max: 50
|
||
needle: true
|
||
segments:
|
||
- from: 0
|
||
color: green
|
||
- from: 30
|
||
color: yellow
|
||
- from: 40
|
||
color: red
|
||
|
||
- type: gauge
|
||
entity: sensor.0800041935246530_this_month_total_usage
|
||
name: 当月电量
|
||
min: 0
|
||
max: 1000
|
||
needle: true
|
||
segments:
|
||
- from: 0
|
||
color: green
|
||
- from: 600
|
||
color: yellow
|
||
- from: 800
|
||
color: red
|
||
|
||
# ========== 关键数据 ==========
|
||
- type: grid
|
||
columns: 3
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_balance
|
||
name: 账户余额
|
||
icon: mdi:wallet
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_arrears
|
||
name: 欠缴电费
|
||
icon: mdi:alert-circle
|
||
- type: sensor
|
||
entity: sensor.yu_ji_ben_yue_dian_fei
|
||
name: 预计本月
|
||
icon: mdi:calculator
|
||
|
||
# ========== 预测对比 ==========
|
||
- type: grid
|
||
columns: 3
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.yu_ji_ben_yue_yong_dian
|
||
name: 预计用电
|
||
- type: sensor
|
||
entity: sensor.huan_bi_shang_yue_yong_dian_bian_dong
|
||
name: 环比上月
|
||
- type: sensor
|
||
entity: sensor.tong_bi_qu_nian_yong_dian_bian_dong
|
||
name: 同比去年
|
||
|
||
# ========== 预算状态 ==========
|
||
- type: markdown
|
||
content: |
|
||
## 💰 本月预算
|
||
**剩余:**{{ states('sensor.ben_yue_dian_fei_sheng_yu_yu_suan') }} 元
|
||
**使用率:**{{ state_attr('sensor.ben_yue_dian_fei_sheng_yu_yu_suan', 'budget_usage_percent') }}%
|
||
**状态:**{{ state_attr('sensor.ben_yue_dian_fei_sheng_yu_yu_suan', 'status') }}
|
||
|
||
# ========== 阶梯电价 ==========
|
||
- type: grid
|
||
columns: 3
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_current_ladder
|
||
name: 阶梯档位
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_current_ladder_remaining_kwh
|
||
name: 阶梯剩余
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_current_ladder_tariff
|
||
name: 当前电价
|
||
|
||
# ========== 本月/上月 ==========
|
||
- type: grid
|
||
columns: 2
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_month_total_usage
|
||
name: 本月用电
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_this_month_total_cost
|
||
name: 本月电费
|
||
|
||
- type: grid
|
||
columns: 2
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_month_total_usage
|
||
name: 上月用电
|
||
- type: sensor
|
||
entity: sensor.0800041935246530_last_month_total_cost
|
||
name: 上月电费
|
||
|
||
# ========== 30日统计 ==========
|
||
- type: grid
|
||
columns: 3
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.jin_30_ri_zui_gao_yong_dian
|
||
name: 30日最高
|
||
- type: sensor
|
||
entity: sensor.jin_30_ri_zui_di_yong_dian
|
||
name: 30日最低
|
||
- type: sensor
|
||
entity: sensor.jin_30_ri_ping_jun_yong_dian
|
||
name: 30日平均
|
||
|
||
# ========== 工作日vs周末 ==========
|
||
- type: grid
|
||
columns: 2
|
||
cards:
|
||
- type: sensor
|
||
entity: sensor.gong_zuo_ri_ping_jun_yong_dian
|
||
name: 工作日平均
|
||
- type: sensor
|
||
entity: sensor.zhou_mo_ping_jun_yong_dian
|
||
name: 周末平均
|
||
|
||
# ========== 近30日趋势图 ==========
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📊 近30日用电趋势
|
||
graph_span: 30d
|
||
series:
|
||
- entity: sensor.nan_fang_dian_wang_li_shi_pin_jie
|
||
type: area
|
||
name: 日用电量
|
||
color: purple
|
||
data_generator: |
|
||
const data = entity.attributes.history_day_value || [];
|
||
return data.slice(-30).map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
|
||
# ========== 近7日柱状图 ==========
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📅 近7日用电详情
|
||
graph_span: 7d
|
||
apex_config:
|
||
chart:
|
||
type: bar
|
||
series:
|
||
- entity: sensor.nan_fang_dian_wang_li_shi_pin_jie
|
||
type: column
|
||
name: 日用电量
|
||
color: blue
|
||
data_generator: |
|
||
const data = entity.attributes.history_day_value || [];
|
||
return data.slice(-7).map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
|
||
# ========== 当月每日详情 ==========
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📅 当月每日用电
|
||
span:
|
||
start: month
|
||
series:
|
||
- entity: sensor.0800041935246530_this_month_total_usage
|
||
type: column
|
||
name: 每日电量
|
||
data_generator: |
|
||
const data = entity.attributes.this_month_by_day || [];
|
||
return data.map(item => {
|
||
return [new Date(item.date).getTime(), parseFloat(item.kwh) || 0];
|
||
});
|
||
|
||
# ========== 今年月度趋势 ==========
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 📈 今年每月用电
|
||
span:
|
||
start: year
|
||
series:
|
||
- entity: sensor.0800041935246530_this_year_total_usage
|
||
type: column
|
||
name: 每月电量
|
||
data_generator: |
|
||
const data = entity.attributes.this_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
|
||
# ========== 今年vs去年 ==========
|
||
- type: custom:apexcharts-card
|
||
header:
|
||
show: true
|
||
title: 🔄 今年 vs 去年
|
||
span:
|
||
start: year
|
||
series:
|
||
- entity: sensor.0800041935246530_this_year_total_usage
|
||
type: column
|
||
name: 今年
|
||
data_generator: |
|
||
const data = entity.attributes.this_year_by_month || [];
|
||
return data.map(item => {
|
||
return [new Date(item.month || item.date).getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
- entity: sensor.0800041935246530_last_year_total_usage
|
||
type: column
|
||
name: 去年
|
||
data_generator: |
|
||
const year = new Date().getFullYear();
|
||
const data = entity.attributes.last_year_by_month || [];
|
||
return data.map(item => {
|
||
const date = new Date(item.month || item.date);
|
||
date.setFullYear(year);
|
||
return [date.getTime(), parseFloat(item.kwh || item.usage) || 0];
|
||
});
|
||
```
|
||
|
||
---
|
||
|
||
## 安装步骤
|
||
|
||
### 1️⃣ 安装依赖
|
||
```bash
|
||
# HACS → 集成 → 南方电网
|
||
# HACS → 前端 → ApexCharts Card
|
||
# 重启 Home Assistant
|
||
```
|
||
|
||
### 2️⃣ 配置传感器
|
||
```bash
|
||
1. 编辑 configuration.yaml
|
||
2. 添加上面的传感器配置
|
||
3. 开发者工具 → YAML → 检查配置
|
||
4. 重启 Home Assistant
|
||
5. 等待 3-5 分钟传感器初始化
|
||
```
|
||
|
||
### 3️⃣ 创建仪表板
|
||
```bash
|
||
1. 设置 → 仪表板 → 添加仪表板
|
||
2. 名称:电力监控
|
||
3. 编辑仪表板 → 原始配置编辑器
|
||
4. 粘贴仪表板配置
|
||
5. 保存
|
||
```
|
||
|
||
---
|
||
|
||
## 调试方法
|
||
|
||
### 🔍 检查传感器状态
|
||
```yaml
|
||
# 开发者工具 → 模板
|
||
{{ states('sensor.nan_fang_dian_wang_li_shi_pin_jie') }}
|
||
{{ state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value') | length }}
|
||
```
|
||
|
||
### 🔍 查看所有传感器
|
||
```yaml
|
||
# 开发者工具 → 状态
|
||
# 搜索:nan_fang 或 jin_30 或 gong_zuo
|
||
```
|
||
|
||
### 🔍 检查数据结构
|
||
```yaml
|
||
# 开发者工具 → 模板
|
||
{{ state_attr('sensor.nan_fang_dian_wang_li_shi_pin_jie', 'history_day_value')[:3] }}
|
||
```
|
||
|
||
### 🔍 验证图表数据
|
||
```javascript
|
||
// 浏览器控制台(F12)
|
||
console.log(entity.attributes.history_day_value);
|
||
```
|
||
|
||
---
|
||
|
||
## 常见问题
|
||
|
||
### ❌ 传感器显示 `unknown`
|
||
**原因:**南方电网集成未配置或数据未加载
|
||
**解决:**
|
||
```bash
|
||
1. 检查南方电网集成是否正常
|
||
2. 等待数据更新(可能需要24小时)
|
||
3. 手动触发更新:开发者工具 → 服务 → homeassistant.update_entity
|
||
```
|
||
|
||
### ❌ 图表不显示
|
||
**原因:**ApexCharts Card 未安装
|
||
**解决:**
|
||
```bash
|
||
1. HACS → 前端 → ApexCharts Card → 安装
|
||
2. 清除浏览器缓存(Ctrl+F5)
|
||
3. 重启 Home Assistant
|
||
```
|
||
|
||
### ❌ 实体ID不匹配
|
||
**原因:**您的户号与示例不同
|
||
**解决:**
|
||
```bash
|
||
1. 开发者工具 → 状态 → 搜索 "0800041935246530"
|
||
2. 找到您的实际户号
|
||
3. 全局替换配置中的户号
|
||
```
|
||
|
||
### ❌ 配置检查失败
|
||
**原因:**YAML 语法错误
|
||
**解决:**
|
||
```bash
|
||
1. 检查缩进(使用2个空格,不是Tab)
|
||
2. 检查引号配对
|
||
3. 使用在线YAML验证器检查
|
||
```
|
||
|
||
---
|
||
|
||
## 📌 重要提示
|
||
|
||
1. **户号替换:**将所有 `0800041935246530` 替换为您的实际户号
|
||
2. **预算调整:**修改 `budget: 200` 为您的实际预算
|
||
3. **数据延迟:**南方电网数据通常延迟1-2天
|
||
4. **定期更新:**建议每月检查一次配置
|
||
|
||
---
|
||
|
||
## 🎯 功能清单
|
||
|
||
✅ 实时余额与欠费
|
||
✅ 本月/上月用电统计
|
||
✅ 今年/去年对比
|
||
✅ 阶梯电价监控
|
||
✅ 30日/7日趋势分析
|
||
✅ 工作日vs周末对比
|
||
✅ 预算管理与预警
|
||
✅ 环比/同比变动
|
||
✅ 多维度图表展示
|
||
|
||
---
|
||
|
||
**配置完成后,您将拥有一个功能完整、美观实用的电力监控仪表板!** 🎉 |