本帖最后由 zjhcr 于 2021-12-21 21:52 编辑
接1楼继续
modbus:
- type: tcp
close_comm_on_error: false
#host、port改成你控制器的IP和端口
host: 192.168.1.86
port: 8088
name: hub1
sensors:
- name: PV_Voltage
slave: 1
address: 1
input_type: holding
unit_of_measurement: V
scale: 0.01
precision: 2
device_class: voltage
scan_interval: 5
- name: PV_Current
slave: 1
address: 2
input_type: holding
unit_of_measurement: A
scale: 0.01
precision: 2
device_class: current
scan_interval: 5
- name: PV_Power
slave: 1
address: 3
unit_of_measurement: W
device_class: power
input_type: holding
scan_interval: 5
- name: PV_State
slave: 1
address: 4
input_type: holding
scan_interval: 5
- name: Battery_Voltage
slave: 1
address: 5
unit_of_measurement: V
scale: 0.01
precision: 2
device_class: battery
input_type: holding
scan_interval: 5
- name: Battery_Temperature
slave: 1
address: 6
unit_of_measurement: °C
device_class: temperature
input_type: holding
scan_interval: 5
- name: Charge_Current
slave: 1
address: 7
unit_of_measurement: A
scale: 0.01
precision: 2
device_class: current
input_type: holding
scan_interval: 5
- name: Discharge_Current
slave: 1
address: 8
unit_of_measurement: A
scale: 0.01
precision: 2
device_class: current
input_type: holding
scan_interval: 5
- name: Battery_Level
slave: 1
address: 10
unit_of_measurement: "%"
device_class: battery
input_type: holding
scan_interval: 5
- name: Charge_State
slave: 1
address: 11
input_type: holding
scan_interval: 5
- name: Controller_Temperature
slave: 1
address: 12
unit_of_measurement: °C
device_class: temperature
input_type: holding
scan_interval: 5
- name: Controller_TotalEnergy
slave: 1
address: 16
unit_of_measurement: kAh
device_class: energy
input_type: holding
scan_interval: 5
- name: Controller_TotalUsed
slave: 1
address: 20
unit_of_measurement: Ah
device_class: energy
input_type: holding
scan_interval: 5
sensor:
- platform: template
sensors:
pv_state_change:
friendly_name: '光伏板状态'
value_template: >
{% if is_state('sensor.pv_state', '0') %}
低电压未发电
{% elif is_state('sensor.pv_state', '1') %}
发电中
{% elif is_state('sensor.pv_state', '2') %}
过压保护
{% elif is_state('sensor.pv_state', '3') %}
光伏板未接入
{% endif %}
pv_voltage_change:
unit_of_measurement: V
device_class: voltage
friendly_name: '光伏板电压'
value_template: >
{% set state = states('sensor.pv_voltage') %}{{ (state) }}
pv_current_change:
unit_of_measurement: A
device_class: current
friendly_name: '光伏板电流'
value_template: >
{% set state = states('sensor.pv_current') %}{{ (state) }}
pv_power_change:
unit_of_measurement: W
device_class: power
friendly_name: '光伏功率'
value_template: >
{% set state = states('sensor.pv_power_change2') %}{{ (state) }}
battery_voltage_change:
unit_of_measurement: V
device_class: voltage
friendly_name: '蓄电池电压'
value_template: >
{% set state = states('sensor.battery_voltage') %}{{ (state) }}
discharge_current_change:
unit_of_measurement: A
device_class: current
friendly_name: '放电电流'
value_template: >
{% set state = states('sensor.discharge_current') %}{{ (state) }}
discharge_power_change:
unit_of_measurement: W
device_class: power
friendly_name: '放电功率'
value_template: >
{% set state = states('sensor.discharge_power_change2') %}{{ (state) }}
controller_temperature_change:
unit_of_measurement: °C
device_class: temperature
friendly_name: '控制器温度'
value_template: >
{% set state = states('sensor.controller_temperature') %}{{ (state) }}
battery_temperature_change:
unit_of_measurement: °C
device_class: temperature
friendly_name: '蓄电池温度'
value_template: >
{% set state = states('sensor.battery_temperature') %}{{ (state) }}
battery_level_change:
unit_of_measurement: "%"
device_class: temperature
friendly_name: '蓄电池电量'
value_template: >
{% set state = states('sensor.battery_level') %}{{ (state) }}
charge_state_change:
friendly_name: '充电模式'
value_template: >
{% if is_state('sensor.pv_state', '0') %}
停充
{% elif is_state('sensor.pv_state', '1') %}
强充
{% elif is_state('sensor.pv_state', '2') %}
MPPT
{% elif is_state('sensor.pv_state', '3') %}
浮充
{% elif is_state('sensor.pv_state', '4') %}
提升
{% elif is_state('sensor.pv_state', '5') %}
均衡
{% elif is_state('sensor.pv_state', '6') %}
恒流
{% elif is_state('sensor.pv_state', '7') %}
恒压
{% endif %}
controller_totalenergy_change:
unit_of_measurement: kWh
device_class: energy
friendly_name: '总发电量'
value_template: >
{% set state = states('sensor.controller_totalenergy') %}{{ (state | float * 0.12 ) | round(2)}}
controller_totalused_change:
unit_of_measurement: kWh
device_class: energy
friendly_name: '总用电量'
value_template: >
{% set state = states('sensor.controller_totalused') %}{{ (state | float * 0.012 ) | round(2)}}
template:
- sensor:
- name: "PV Power Change2"
unit_of_measurement: W
state: "{{ (states('sensor.battery_voltage')|float * states('sensor.pv_current')|float ) | round(2) }}"
device_class: power
- name: "Discharge Power Change2"
unit_of_measurement: W
state: "{{ (states('sensor.battery_voltage')|float * states('sensor.Discharge_Current')|float ) | round(2) }}"
device_class: power
如果只想白嫖代码的话,到这里直接复制粘贴就已经基本完成了,如果想学习代码的内容那么可以接着往下看
首先modbus: 调用HA的Modbus协议支持host: 和 port: 需要改成你自己控制器接入了内网的地址和端口
sensors: 部分就是HA的通过获取到的Modbus数据转换成传感器参数,这里举一个例子:
sensors:
- name: PV_Voltage #在HA里生成的实体名称,这里取PV_Voltage意为光伏板电压
slave: 1 #从位数,Modbus作用于TCP模式时可不填,我统一填1
address: 1 #地址,这个就是前面我提到协议里的地址,1代表0x0001,读取地址1的数据
input_type: holding #输入类型,对应的是ModScan32里的03:Holding Register,一般读数都选择该类型
unit_of_measurement: V #测量单位,V代表是电压,根据所读的数属于哪种单位来填写,例如电流是A
scale: 0.01 #通过Modbus读取过来的光伏电压数值是1267,实际是12.67,需用使用该参数转换增加小数点
precision: 2 #保留小数点后2位精度
device_class: voltage #设备种类,这个属于电压,具体有哪些设备种类可参照HA官方文档
scan_interval: 5 #扫描间隔,就是每隔多久更新一次数据,单位是秒
如此类推把所有对应的地址和协议中的数值都可以读取成为HA里的实体……
由于原厂读取的光伏功率和放电功率都是没有数值(厂家bug?),所以我又建了两个模板sensor用公式自己拿电压和电流乘积得出两个功率的实体,又另外把发电量和用电量原本是Ah安时的单位转换成了kWh千瓦时,但发现模板sensor无法增加state_class: total_increasing 参数,致使发电量数据暂时还没有办法接入HA的能源板块,这个问题有待解决
添加好代码内容后保存mppt.yaml
然后重启HA
可以在实体注册表里看到了很多新生成的实体
剩下的事情,就是拿这些实体在Lovelace,也就是HA的主界面,新建你喜欢的卡片类型来展示,就可以了。
注意要选取中文名称的那些实体哈哈
对了,如果经常出现“实体不可用”的显示,则要考虑是否存在控制器与WiFi连接的信号强度不佳,ESP-12F内置芯片信号没那么强
ModScan32、汉非控制器的协议文件、MPPT.yaml这几样都在附件里了
上位机软件和NotePad++都超过了3M的附件限制,无法发附件里了
最后还请路过的大佬们帮忙解决一下模板sensor无法增加state_class: total_increasing 参数,致使发电量、用电量这两实体的数据暂时还没有办法接入HA的能源板块这个问题,万分感谢~
|