本帖最后由 zjhcr 于 2021-12-16 00:56 编辑
楼主最近玩Homeassistant的能源板块,买了个PZEM-004T电量监测并成功接入了ha,然后突发奇想,可否把太阳能也接入到ha中来?
正好手上有一个汉非的MPPT 120D太阳能控制器,自带WiFi通讯,但只能用厂家提供的手机APP和电脑上位机软件来查看数据。
经过研究发现控制器是内置了一个ESP-12F通讯模块,模块使用的esp-link固件将TTL数据透传。
后来通过官网查询到工程师并咨询工程师拿到了协议。该太阳能控制器使用的是Modbus协议,通过esp-link作为Modbus TCP透传。
通过调阅ha官方文档发现ha正好能够直接支持Modbus TCP,直接通过网络获取到透传数据进行解析转换成实体接入ha。
就是该款控制器
ha原生支持modbus tcp协议
厂家提供的协议数据
经过一番摸索,终于能使数据接入了ha,但是原始数据是没有小数点的格式,譬如获取到的蓄电池电压1260v,实际应该为12.60v,后来使用模版sensor将获取的原始sensor数值/100实现正确点位数值。
但是发现一个问题,该控制器的光伏功率获取值一直为0,跟上位机软件上也是一致,也许是厂家出厂时就没有功率的数据,于是只能自己通过电池电压和充电电流的sensor数据相乘来得出功率数据。
但楼主对代码不精通,尤其是template sensor这块,尝试了几次ha日志都报错,生成的template sensor都没有正确显示,在此求教各位大神正确的代码式
先帖上我configuration.yaml的代码:
modbus:
- type: tcp
close_comm_on_error: false
host: 192.168.1.86
port: 8088
name: hub1
sensors:
- name: PV_Voltage
slave: 1
address: 1
input_type: holding
unit_of_measurement: V
device_class: voltage
scan_interval: 5
- name: PV_Current
slave: 1
address: 2
input_type: holding
unit_of_measurement: A
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
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
device_class: current
input_type: holding
scan_interval: 5
- name: Discharge_Current
slave: 1
address: 8
unit_of_measurement: A
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: 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 | int / 100) | round(2)}}
pv_current_change:
unit_of_measurement: A
device_class: current
friendly_name: '光伏板电流'
value_template: >
{% set state = states('sensor.pv_current') %}{{ (state | float / 100) | round(2)}}
pv_power_change:
unit_of_measurement: W
device_class: power
friendly_name: '光伏功率'
value_template: >
{% set state = states('sensor.pv_current_change' ) %}
battery_voltage_change:
unit_of_measurement: V
device_class: voltage
friendly_name: '蓄电池电压'
value_template: >
{% set state = states('sensor.battery_voltage') %}{{ (state | float / 100) | round(2)}}
controller_totalenergy_change:
unit_of_measurement: kWh
# state_class: total_increasing
device_class: energy
friendly_name: '总发电量'
value_template: >
{% set state = states('sensor.controller_totalenergy') %}{{ (state | int * 0.12 ) | round(2)}}
controller_totalused_change:
unit_of_measurement: kWh
# state_class: total_increasing
device_class: energy
friendly_name: '总用电量'
value_template: >
{% set state = states('sensor.controller_totalused') %}{{ (state | int * 0.012 ) | round(2)}}
复制代码
重点就是这个 value_template: 如何正确引用sensor.pv_current_change和sensor.battery_voltage_change的值相乘 ?
pv_power_change:
unit_of_measurement: W
device_class: power
friendly_name: '光伏功率'
value_template: >
{% set state = states('sensor.pv_current_change' ) * states('sensor.battery_voltage_change') %}
复制代码
我使用上述表达式后,ha无法正常显示该template sensor,并日志提示如下错误
还请各位大佬赐教解决这个问题,谢谢!
另外再请教一下,template sensor生成的发电量数据如何能接入到ha的能源板块里面 ?我看了一下官方文档,sensor必须要有state_class:属性并且值要为total_increasing,device_class:属性值为energy
我尝试直接在templatre sensor下添加state_class: total_increasing和device_class: energy属性,但是ha日志报错不让template sensor添加state_class属性,请问还有啥办法将发电量数据接入到能源统计板块吗 ?
期待各位大佬的帮忙解决!最后附件送上汉非太阳能控制器的Modbus通用协议书,希望能帮到同款控制器的人