HomeAssistant 接入国家电网
HomeAssistant 接入国家电网
一、简介:
将国网的电费、用电量数据接入homeassistant,实现实时追踪家庭用电量情况;并且可以将每日用电量保存到数据库,历史有迹可循。
二、项目地址:
https://github.com/ARC-MX/sgcc_electricity_new/
三、前提准备
01、注册国家电网
注册网址:https://www.95598.cn/osgweb/login
绑定用户
02、创建HomeAssistant Token
点击左下角头像、安全, 长期访问令牌,创建令牌。
四、部署sgcc_electricity_new
1、使用 docker-compose 安装
克隆项目到执定目录下
git clone https://github.com/ARC-MX/sgcc_electricity_new.git
cd sgcc_electricity_new
2、修改配置文件
修改 .env 配置文件
配置国网登录信息、HA Token。
3、运行项目
打开飞牛docker compose,新建项目,选中克隆的项目文件。
运行成功后,查看docker 运行日志,可以看到国网登录成功后的信息。
五、修改HomeAssistant 配置信息
1、修改HA配置文件
配置configuration.yaml文件, 添加如下内容,将下面中的_xxxx 替换为电号后4位。
template:
#xxx
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.electricity_charge_balance_xxx
sensor:
- name: electricity_charge_balance_xxx
unique_id: electricity_charge_balance_xxx
state: "{{ states('sensor.electricity_charge_balance_xxx') }}"
state_class: total
unit_of_measurement: "CNY"
device_class: monetary
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.last_electricity_usage_xxx
sensor:
- name: last_electricity_usage_xxx
unique_id: last_electricity_usage_xxx
state: "{{ states('sensor.last_electricity_usage_xxx') }}"
state_class: measurement
unit_of_measurement: "kWh"
device_class: energy
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.month_electricity_usage_xxx
sensor:
- name: month_electricity_usage_xxx
unique_id: month_electricity_usage_xxx
state: "{{ states('sensor.month_electricity_usage_xxx') }}"
state_class: measurement
unit_of_measurement: "kWh"
device_class: energy
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.month_electricity_charge_xxx
sensor:
- name: month_electricity_charge_xxx
unique_id: month_electricity_charge_xxx
state: "{{ states('sensor.month_electricity_charge_xxx') }}"
state_class: measurement
unit_of_measurement: "CNY"
device_class: monetary
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.yearly_electricity_usage_xxx
sensor:
- name: yearly_electricity_usage_xxx
unique_id: yearly_electricity_usage_xxx
state: "{{ states('sensor.yearly_electricity_usage_xxx') }}"
state_class: total_increasing
unit_of_measurement: "kWh"
device_class: energy
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.yearly_electricity_charge_xxx
sensor:
- name: yearly_electricity_charge_xxx
unique_id: yearly_electricity_charge_xxx
state: "{{ states('sensor.yearly_electricity_charge_xxx') }}"
state_class: total_increasing
unit_of_measurement: "CNY"
device_class: monetary
2、重启HA ,并添加HomeAssistant卡片,参考配置如下:
type: vertical-stack
# 定义通用卡片样式锚点(放在 cards 列表外)
card_style: &card_style
card_mod:
style: |
ha-card {
background: rgba(255, 255, 255, 0.15) !important;
backdrop-filter: blur(10px) !important;
border-radius: 20px !important;
border: 1px solid rgba(255, 255, 255, 0.2) !important;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.15) !important;
transition: all 0.3s ease-in-out !important;
overflow: hidden !important;
}
ha-card:hover {
transform: translateY(-3px) scale(1.02) !important;
box-shadow: 0 12px 40px rgba(31, 38, 135, 0.25) !important;
background: rgba(255, 255, 255, 0.25) !important;
}
ha-card:active {
transform: translateY(1px) scale(0.98) !important;
box-shadow: 0 4px 20px rgba(31, 38, 135, 0.2) !important;
}
.name {
font-size: 18px !important;
font-weight: 600 !important;
color: var(--primary-text-color) !important;
text-align: center !important;
margin: 12px 0 !important;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1) !important;
}
.state {
font-size: 16px !important;
color: var(--secondary-text-color) !important;
text-align: center !important;
font-weight: 500 !important;
margin-bottom: 12px !important;
}
cards:
# 第一部分:每日用电量与电费余额图表
- type: custom:mini-graph-card
entities:
- entity: sensor.last_electricity_usage_0767
name: 国网每日用电量
aggregate_func: first
show_state: true
show_points: true
icon: mdi:lightning-bolt-outline
- entity: sensor.electricity_charge_balance_0767
name: 电费余额
aggregate_func: first
show_state: true
show_points: true
color: "#e74c3c"
icon: mdi:cash
y_axis: secondary
group_by: date
hour24: true
hours_to_show: 240
lower_bound: 0
upper_bound: 10
lower_bound_secondary: 0
upper_bound_secondary: 120
show:
icon: false
<<: *card_style # 复用通用样式
# 第二部分:上月电费与用电量
- type: horizontal-stack
cards:
- type: sensor
entity: sensor.month_electricity_charge_0767
detail: 1
name: 上月电费
icon: ""
unit: 元
graph: none
<<: *card_style # 复用通用样式
- type: sensor
entity: sensor.month_electricity_usage_0767
detail: 1
name: 上月用电量
unit: 度
icon: mdi:lightning-bolt-outline
graph: none
<<: *card_style # 复用通用样式
# 第三部分:今年总用电量与总费用图表
- type: horizontal-stack
cards:
- type: custom:mini-graph-card
entities:
- entity: sensor.yearly_electricity_usage_0767
name: 今年总用电量
aggregate_func: first
show_state: true
show_points: true
group_by: date
hour24: true
hours_to_show: 240
animate: true
<<: *card_style # 复用通用样式
- type: custom:mini-graph-card
entities:
- entity: sensor.yearly_electricity_charge_0767
name: 今年总用电费用
aggregate_func: first
show_state: true
show_points: true
group_by: date
hour24: true
hours_to_show: 240
animate: true
<<: *card_style # 复用通用样式