Home Assistant Modbus配置:热泵负温度值读取异常求助
解决Home Assistant中Modbus热泵温度寄存器的负数转换问题
问题分析
你的热泵采用无符号16位整数(uint16)存储补码形式的负数:实际温度-1℃对应寄存器值65436,计算逻辑为65436 - 65536 = -99,再乘以缩放比例0.01得到-0.99℃(近似-1℃)。HA默认用uint16解析时只会返回正数,无法自动识别补码负数。
方案1:使用模板传感器转换值
在现有Modbus传感器基础上,添加模板传感器完成补码转换与缩放:
template: - sensor: - name: "Heat Pump Outside Temperature (Corrected)" device_class: temperature state_class: measurement unit_of_measurement: "°C" unique_id: "ac_heating_outside_temp_corrected" state: > {% set raw_value = states('sensor.heat_pump_outside_temp') | int %} {% set corrected_value = (raw_value - 65536) if raw_value > 32767 else raw_value %} {{ (corrected_value * 0.01) | round(2) }}
代码说明
- 读取原始Modbus传感器数值,判断是否超过uint16中间值32767(大于该值即为补码负数)
- 对负数执行
raw_value - 65536转换,正数保持原样 - 最后乘以缩放比例0.01并保留两位小数
方案2:直接修改Modbus配置的data_type
若不想新增模板传感器,可将Modbus传感器的data_type改为int16,HA会自动将uint16补码值解析为对应负数:
modbus: - name: ... type: tcp host: ... port: ... delay: 5 timeout: 5 sensors: - name: Heat pump outside temperature address: 0 slave: 1 input_type: holding device_class: temperature state_class: measurement data_type: int16 # 替换原uint16 unit_of_measurement: "°C" scale: 0.01 # 直接配置缩放比例 unique_id: "ac_heating_outside_temp"
注意事项
- 部分设备补码实现存在差异,若
int16解析结果异常,优先使用模板传感器方案 - 确保原始Modbus传感器配置的寄存器地址、类型无误,避免读取错误数据
内容的提问来源于stack exchange,提问作者tomdelahaba
相关产品推荐
相关产品推荐

