Homeassistant接入国家电网用电数据

本文记录了将电网数据接入HA的方法。

项目地址:HomeAssistant 国家电网电费查询:ARC-MX/sgcc_electricity_new

可以帮助将国网的电费、用电量数据接入homeassistant,实现实时追踪家庭用电量情况;并且可以将每日用电量保存到数据库,历史有迹可循。
在homeassistant以实体显示:

实体entity_id 说明
sensor.last_electricity_usage_xxxx 最近一天用电量,单位KWH、度。
sensor.electricity_charge_balance_xxxx 预付费显示电费余额,反之显示上月应交电费,单位元。
sensor.yearly_electricity_usage_xxxx 今年总用电量,单位KWH、度。
sensor.yearly_electricity_charge_xxxx 今年总用电费,单位元。
sensor.month_electricity_usage_xxxx 最近一个月用电量,单位KWH、度。
sensor.month_electricity_charge_xxxx 上月总用电费,单位元。
  • 适用范围
    适用于除广东、广西、云南、贵州、海南等省份的用户外,均可使用本应用获取电力、电费数据。
  • 实现流程
    通过python的selenium包获取国家电网的数据,通过homeassistant的提供的REST API将采用POST请求将实体状态更新到homeassistant。

安装与部署

注册国家电网账户

首先要注册国家电网账户,绑定电表,并且可以手动查询电量,需要手机号和登录密码。
注册网址:https://www.95598.cn/osgweb/login

获取HA token

docker镜像部署

1
2
3
4
5
6
7
git clone https://github.com/ARC-MX/sgcc_electricity_new.git
# 如果github网络环境不好的话可以使用国内镜像,完全同步的,个人推荐使用国内镜像
# git clone https://gitee.com/ARC-MX/sgcc_electricity_new.git
cd sgcc_electricity_new

cp example.env .env # 创建环境变量文件
nano .env

参考:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
### 以下项都需要修改
## 国网登录信息
# 修改为自己的登录账号
PHONE_NUMBER="xxx"
# 修改为自己的登录密码
PASSWORD="xxxx"
# 排除指定用户ID,如果出现一些不想检测的ID或者有些充电、发电帐号、可以使用这个环境变量,如果有多个就用","分隔,","之间不要有空格
IGNORE_USER_ID=xxxxxxx,xxxxxxx,xxxxxxx

# SQLite 数据库配置
# or False 不启用数据库储存每日用电量数据。
ENABLE_DATABASE_STORAGE=True
# 数据库名,默认为homeassistant
DB_NAME="homeassistant.db"
# COLLECTION_NAME默认为electricity_daily_usage_{国网用户id},不支持修改。

## homeassistant配置
# 改为你的localhost为你的homeassistant地址
HASS_URL="http://localhost:8123/"
# homeassistant的长期令牌
HASS_TOKEN="eyxxxxx"

## selenium运行参数
# 任务开始时间,24小时制,例如"07:00”则为每天早上7点执行,第一次启动程序如果时间晚于早上7点则会立即执行一次,每隔12小时执行一次。
JOB_START_TIME="07:00"
# 每次操作等待时间,推荐设定范围为[2,30],该值表示每次点击网页后所要等待数据加载的时间,如果出现“no such element”诸如此类的错误可适当调大该值,如果硬件性能较好可以适当调小该值
RETRY_WAIT_TIME_OFFSET_UNIT=15


## 记录的天数, 仅支持填写 7 或 30
# 国网原本可以记录 30 天,现在不开通智能缴费只能查询 7 天造成错误
DATA_RETENTION_DAYS=7

## 余额提醒
# 是否缴费提醒
RECHARGE_NOTIFY=Flase
# 余额
BALANCE=5.0
# pushplus token 如果有多个就用","分隔,","之间不要有空格,单个就不要有","
PUSHPLUS_TOKEN=xxxxxxx,xxxxxxx,xxxxxxx

运行:

1
2
3
运行获取传感器名称
docker-compose up -d
docker-compose logs sgcc_electricity_app

成功的话,日志应该是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2025-03-31 16:36:23  [INFO    ] ---- Homeassistant sensor sensor.month_electricity_charge_7787 state updated: 104.52 CNY
2025-03-31 16:36:23 [INFO ] ---- User xxxxxx3667787 state-refresh task run successfully!
2025-03-31 16:37:43 [INFO ] ---- Get electricity charge balance for 1601356178519 successfully, balance is 131.3 CNY.
2025-03-31 16:39:19 [INFO ] ---- Get year power usage for xxxxxx6178519 successfully, usage is 141 kwh
2025-03-31 16:39:19 [INFO ] ---- Get year power charge for xxxxxx6178519 successfully, yealrly charge is 73.32 CNY
2025-03-31 16:39:34 [INFO ] ---- Get month power charge for xxxxxx6178519 successfully, 2025-01-01-2025-01-31 usage is 73 KWh, charge is 37.96 CNY.
2025-03-31 16:39:34 [INFO ] ---- Get month power charge for xxxxxx6178519 successfully, 2025-02-01-2025-02-28 usage is 68 KWh, charge is 35.36 CNY.
2025-03-31 16:39:49 [INFO ] ---- Get daily power consumption for 1601356178519 successfully, , 2025-03-30 usage is 1.95 kwh.
2025-03-31 16:39:49 [INFO ] ---- enable_database_storage is true, we will store the data to the database.
2025-03-31 16:40:20 [INFO ] ---- Database of /data/homeassistant.db created successfully.
2025-03-31 16:40:20 [INFO ] ---- Table dailyxxxxxx6178519 created successfully
2025-03-31 16:40:20 [INFO ] ---- Table dataxxxxxx6178519 created successfully
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 1.95KWh on 2025-03-30 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 2.45KWh on 2025-03-29 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 2.77KWh on 2025-03-28 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 1.76KWh on 2025-03-27 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 2.52KWh on 2025-03-26 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 2.1KWh on 2025-03-25 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- The electricity consumption of 2.74KWh on 2025-03-24 has been successfully deposited into the database
2025-03-31 16:40:20 [INFO ] ---- Check the electricity bill balance, the notification will be sent = False

HA中的显示

样式1

样式2
结合mini-graph-card 和mushroom实现美化效果(在hacs上安装):

电费不足通知

电费余额不足提醒,在.env里设置提醒余额。目前是用pushplus的方案,注册pushplus,获取token。通知给谁就让谁注册并将token填到.env中。

修改configuration.yaml文件

由于是API方式传递传感器数据,要想重启ha实体ID可用,必须配置如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
template:
- trigger:
- platform: event
event_type: state_changed
event_data:
entity_id: sensor.electricity_charge_balance_xxxx
sensor:
- name: electricity_charge_balance_xxxx
unique_id: electricity_charge_balance_xxxx
state: "{{ states('sensor.electricity_charge_balance_xxxx') }}"
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_xxxx
sensor:
- name: last_electricity_usage_xxxx
unique_id: last_electricity_usage_xxxx
state: "{{ states('sensor.last_electricity_usage_xxxx') }}"
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_xxxx
sensor:
- name: month_electricity_usage_xxxx
unique_id: month_electricity_usage_xxxx
state: "{{ states('sensor.month_electricity_usage_xxxx') }}"
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_xxxx
sensor:
- name: month_electricity_charge_xxxx
unique_id: month_electricity_charge_xxxx
state: "{{ states('sensor.month_electricity_charge_xxxx') }}"
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_xxxx
sensor:
- name: yearly_electricity_usage_xxxx
unique_id: yearly_electricity_usage_xxxx
state: "{{ states('sensor.yearly_electricity_usage_xxxx') }}"
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_xxxx
sensor:
- name: yearly_electricity_charge_xxxx
unique_id: yearly_electricity_charge_xxxx
state: "{{ states('sensor.yearly_electricity_charge_xxxx') }}"
state_class: total_increasing
unit_of_measurement: "CNY"
device_class: monetary

xxxx是log中的_xxxx后缀。
configuration.yaml在homeassistant配置目录下,也就是docker挂载的目录。
修改后重启ha,就不会出现传感器无效的提示。

在HA当中改变显示样式太复杂了,没学会。电网的数据更新很慢,每天下午才会刷新前一天的数据。发现一个问题,ha的docker一重启数据就没了。(已解决)