『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 29507|回复: 22

[进阶教程] MODBUS协议的新风设备接入

[复制链接]

9

主题

217

帖子

873

积分

高级会员

Rank: 4

积分
873
金钱
646
HASS币
40
发表于 2020-1-17 13:08:22 | 显示全部楼层 |阅读模式
家里的新风接入已经好久了,通讯用的是标准MODBUS协议,厂家发给我的通讯地址如下
微信图片_20200117130135.jpg
用支持TCP的485模块接入到两台设备的AB端,分别把设备地址设为1和2(我负二楼的新风设备地址2,负一楼的设备地址为1)
接入后的HA中的显示如下:
微信图片_20200117130431.png
配置文件如下(水平有限,配置文件写的一般,望论坛内的高手指点):
modbus:
#新风机 -1楼为1号机 -2楼为2号机
  - type: tcp
    host: 192.168.1.2
    port: 9999
    name: hub2

#新风数据
sensor:
  - platform: modbus
#    scan_floaterval: 10    #0.88版以后这个语句失效
    registers:   
#负1楼新风系统读参数
      - name: fresh_air_f1_power
        hub: hub2
        unit_of_measurement: 1开0关
        slave: 1
        register: 8704
      - name: fresh_air_f1_temperature
        hub: hub2
        unit_of_measurement: °C
        slave: 1
        register: 8706
      - name: fresh_air_f1_humidity
        hub: hub2
        unit_of_measurement: "%RH"
        slave: 1
        register: 8707
      - name: fresh_air_f1_PM2.5
        hub: hub2
        unit_of_measurement: μg/m³
        slave: 1
        register: 8712
      - name: fresh_air_f1_mode
        hub: hub2
        unit_of_measurement: 0除湿1通风2净化
        slave: 1
        register: 8713
      - name: fresh_air_f1_speed
        hub: hub2
        unit_of_measurement: 0低档1中档2高档
        slave: 1
        register: 8714
        
#负2楼新风系统读参数
      - name: fresh_air_f2_power
        hub: hub2
        unit_of_measurement: 1开0关
        slave: 2
        register: 8704
      - name: fresh_air_f2_temperature
        hub: hub2
        unit_of_measurement: °C
        slave: 2
        register: 8706
      - name: fresh_air_f2_humidity
        hub: hub2
        unit_of_measurement: "%RH"
        slave: 2
        register: 8707
      - name: fresh_air_f2_PM2.5
        hub: hub2
        unit_of_measurement: μg/m³
        slave: 2
        register: 8712
      - name: fresh_air_f2_mode
        hub: hub2
        unit_of_measurement: 0除湿1通风2净化
        slave: 2
        register: 8713
      - name: fresh_air_f2_speed
        hub: hub2
        unit_of_measurement: 0低档1中档2高档
        slave: 2
        register: 8714


input_select:
#负1楼新风系统选择
  fresh_air_f1_power:
    name: 电源开关
    icon: mdi:fan
    options:
      - "打开新风"
      - "关闭新风"
  fresh_air_f1_mode:
    name: 模式
    icon: mdi:fan
    options:
      - "除湿"
      - "通风"
  fresh_air_f1_speed:
    name: 风速
    icon: mdi:fan
    options:
      - "低档"
      - "中档"
      - "高档"
#负2楼新风系统选择
  fresh_air_f2_power:
    name: 电源开关
    icon: mdi:fan
    options:
      - "打开新风"
      - "关闭新风"
  fresh_air_f2_mode:
    name: 模式
    icon: mdi:fan
    options:
      - "除湿"
      - "通风"
  fresh_air_f2_speed:
    name: 风速
    icon: mdi:fan
    options:
      - "低档"
      - "中档"
      - "高档"
       
group:

#负1楼新风
  fresh_air_f1:
    name: 负1楼新风系统
    view: no
    control: hidden
    entities:
      - sensor.fresh_air_f1_temperature
      - sensor.fresh_air_f1_humidity
      - sensor.fresh_air_f1_pm2_5
      - input_select.fresh_air_f1_power
      - input_select.fresh_air_f1_mode
      - input_select.fresh_air_f1_speed
#负2楼新风
  fresh_air_f2:
    name: 负2楼新风系统
    view: no
    control: hidden
    entities:
      - sensor.fresh_air_f2_temperature
      - sensor.fresh_air_f2_humidity
      - sensor.fresh_air_f2_pm2_5
      - input_select.fresh_air_f2_power
      - input_select.fresh_air_f2_mode
      - input_select.fresh_air_f2_speed

automation:
#******************负一楼欧伦新风(带除湿功能)*********************
#F1新风开关状态读取
#F1新风开关状态写入
  - alias: fresh_air_f1_set_power
    initial_state: true
    trigger:
      platform: state
      entity_id: sensor.fresh_air_f1_power
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f1_power
        option: >
          {% if states.sensor.fresh_air_f1_power.state == "0" %}关闭新风
          {% elif states.sensor.fresh_air_f1_power.state == "1" %}打开新风
          {% endif %}
#F1新风开关状态写入寄存器(关->开)
  - alias: fresh_air_f1_power_open
    trigger:
      entity_id: input_select.fresh_air_f1_power
      from: 关闭新风
      platform: state
      to: 打开新风
    condition: []
    action:
    - data:
        address: 8960
        hub: hub2
        unit: 1
        value:
        - 23041
      service: modbus.write_register
#F1新风开关状态写入寄存器(开->关)
  - alias: fresh_air_f1_power_close
    trigger:
      entity_id: input_select.fresh_air_f1_power
      from: 打开新风
      platform: state
      to: 关闭新风
    condition: []
    action:
    - data:
        address: 8960
        hub: hub2
        unit: 1
        value:
        - 23040
      service: modbus.write_register

#F1新风模式读取
  - alias: fresh_air_f1_get_mode
    initial_state: true
    trigger:
      platform: time_pattern
      seconds: /5
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f1_mode
        option: >
          {% if states.sensor.fresh_air_f1_mode.state == "0" %}除湿
          {% elif states.sensor.fresh_air_f1_mode.state == "1" %}通风
          {% endif %}
#F1新风模式选择(写入)
  - alias: fresh_air_f1_set_mode
    initial_state: true
    trigger:
      platform: state
      entity_id: input_select.fresh_air_f1_mode
    action:
      service: input_select.select_option
      data_template:
        entity_id: sensor.fresh_air_f1_mode
        option: >
          {% if states.input_select.fresh_air_f1_mode.state == "除湿" %}0
          {% elif states.input_select.fresh_air_f1_mode.state == "通风" %}1
          {% endif %}
#F1新风模式写入寄存器(除湿->通风)
  - alias: fresh_air_f1_mode_tf
    trigger:
      entity_id: input_select.fresh_air_f1_mode
      from: 除湿
      platform: state
      to: 通风
    condition: []
    action:
    - data:
        address: 8963
        hub: hub2
        unit: 1
        value:
        - 1
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 1
        value:
        - 23205
      service: modbus.write_register
#F1新风模式写入寄存器(通风->除湿)
  - alias: fresh_air_f1_mode_cs
    trigger:
      entity_id: input_select.fresh_air_f1_mode
      from: 通风
      platform: state
      to: 除湿
    condition: []
    action:
    - data:
        address: 8963
        hub: hub2
        unit: 1
        value:
        - 0
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 1
        value:
        - 23205
      service: modbus.write_register
#F1新风模式风速读取
  - alias: fresh_air_f1_get_speed
    initial_state: true
    trigger:
      platform: time_pattern
      seconds: /5
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f1_speed
        option: >
          {% if states.sensor.fresh_air_f1_speed.state == "0" %}低档
          {% elif states.sensor.fresh_air_f1_speed.state == "1" %}中档
          {% elif states.sensor.fresh_air_f1_speed.state == "2" %}高档
          {% endif %}

#F1新风速度选择(写入)
  - alias: fresh_air_f1_set_speed
    initial_state: true
    trigger:
      platform: state
      entity_id: input_select.fresh_air_f1_speed
    action:
      service: input_select.select_option
      data_template:
        entity_id: sensor.fresh_air_f1_speed
        option: >
          {% if states.input_select.fresh_air_f1_speed.state == "低档" %}0
          {% elif states.input_select.fresh_air_f1_speed.state == "中档" %}1
          {% elif states.input_select.fresh_air_f1_speed.state == "高档" %}2
          {% endif %}
       
#负一楼新风风速选择(写寄存器)
#低速
  - alias: fresh_air_f1_speed_low
    trigger:
      entity_id: input_select.fresh_air_f1_speed
      from: 中档 
      platform: state
      to: 低档
      entity_id: input_select.fresh_air_f1_speed
      from: 高档
      platform: state
      to: 低档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 1
        value:
        - 0
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 1
        value:
        - 23205
      service: modbus.write_register
#中速
  - alias: fresh_air_f1_speed_ medium
    trigger:
      entity_id: input_select.fresh_air_f1_speed
      from: 低档
      platform: state
      to: 中档
      entity_id: input_select.fresh_air_f1_speed
      from: 高档
      platform: state
      to: 中档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 1
        value:
        - 1
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 1
        value:
        - 23205
      service: modbus.write_register
#高速
  - alias: fresh_air_f1_speed_high
    trigger:
      entity_id: input_select.fresh_air_f1_speed
      from: 低档
      platform: state
      to: 高档
      entity_id: input_select.fresh_air_f1_speed
      from: 中档
      platform: state
      to: 高档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 1
        value:
        - 2
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 1
        value:
        - 23205
      service: modbus.write_register


#******************负二楼欧伦新风(带除湿功能)*********************
#F2新风开关状态读取
#F2新风开关状态选择(写入)
  - alias: fresh_air_f2_set_power
    initial_state: true
    trigger:
      platform: state
      entity_id: sensor.fresh_air_f2_power
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f2_power
        option: >
          {% if states.sensor.fresh_air_f2_power.state == "0" %}关闭新风
          {% elif states.sensor.fresh_air_f2_power.state == "1" %}打开新风
          {% endif %}
#F2新风开关状态写入寄存器(关->开)
  - alias: fresh_air_f2_power_open
    trigger:
      entity_id: input_select.fresh_air_f2_power
      from: 关闭新风
      platform: state
      to: 打开新风
    condition: []
    action:
    - data:
        address: 8960
        hub: hub2
        unit: 1
        value:
        - 23041
      service: modbus.write_register
#F2新风开关状态写入寄存器(开->关)
  - alias: fresh_air_f1_power_close
    trigger:
      entity_id: input_select.fresh_air_f2_power
      from: 打开新风
      platform: state
      to: 关闭新风
    condition: []
    action:
    - data:
        address: 8960
        hub: hub2
        unit: 1
        value:
        - 23040
      service: modbus.write_register
#F2新风模式读取
  - alias: fresh_air_f2_get_mode
    initial_state: true
    trigger:
      platform: time_pattern
      seconds: /5
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f2_mode
        option: >
          {% if states.sensor.fresh_air_f2_mode.state == "0" %}除湿
          {% elif states.sensor.fresh_air_f2_mode.state == "1" %}通风
          {% endif %}
#F2新风模式选择(写入)
  - alias: fresh_air_f2_get_mode
    initial_state: true
    trigger:
      platform: state
      entity_id: input_select.fresh_air_f2_mode
    action:
      service: input_select.select_option
      data_template:
        entity_id: sensor.fresh_air_f2_mode
        option: >
          {% if states.input_select.fresh_air_f2_mode.state == "除湿" %}0
          {% elif states.input_select.fresh_air_f2_mode.state == "通风" %}1
          {% endif %}
#F2新风模式写入寄存器(除湿->通风)
  - alias: fresh_air_f2_mode_tf
    trigger:
      entity_id: input_select.fresh_air_f2_mode
      from: 除湿
      platform: state
      to: 通风
    condition: []
    action:
    - data:
        address: 8963
        hub: hub2
        unit: 2
        value:
        - 1
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 2
        value:
        - 23205
      service: modbus.write_register
#F2新风模式写入寄存器(通风->除湿)
  - alias: fresh_air_f2_mode_cs
    trigger:
      entity_id: input_select.fresh_air_f2_mode
      from: 通风
      platform: state
      to: 除湿
    condition: []
    action:
    - data:
        address: 8963
        hub: hub2
        unit: 2
        value:
        - 0
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 2
        value:
        - 23205
      service: modbus.write_register
F2新风速度读取
  - alias: fresh_air_f2_get_speed
    initial_state: true
    trigger:
      platform: time_pattern
      seconds: /5
    action:
      service: input_select.select_option
      data_template:
        entity_id: input_select.fresh_air_f2_speed
        option: >
          {% if states.sensor.fresh_air_f2_speed.state == "0" %}低档
          {% elif states.sensor.fresh_air_f2_speed.state == "1" %}中档
          {% elif states.sensor.fresh_air_f2_speed.state == "2" %}高档
          {% endif %}
#F2新风速度选择(写入)
  - alias: fresh_air_f2_set_speed
    initial_state: true
    trigger:
      platform: state
      entity_id: input_select.fresh_air_f2_speed
    action:
      service: input_select.select_option
      data_template:
        entity_id: sensor.fresh_air_f2_speed
        option: >
          {% if states.input_select.fresh_air_f2_speed.state == "低档" %}0
          {% elif states.input_select.fresh_air_f2_speed.state == "中档" %}1
          {% elif states.input_select.fresh_air_f2_speed.state == "高档" %}2
          {% endif %}
#负二楼新风风速选择(写寄存器)
#低速
  - alias: fresh_air_f2_speed_low
    trigger:
      entity_id: input_select.fresh_air_f2_speed
      from: 中档 
      platform: state
      to: 低档
      entity_id: input_select.fresh_air_f2_speed
      from: 高档
      platform: state
      to: 低档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 2
        value:
        - 0
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 2
        value:
        - 23205
      service: modbus.write_register
#中速
  - alias: fresh_air_f2_speed_ medium
    trigger:
      entity_id: input_select.fresh_air_f2_speed
      from: 低档
      platform: state
      to: 中档
      entity_id: input_select.fresh_air_f2_speed
      from: 高档
      platform: state
      to: 中档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 2
        value:
        - 1
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 2
        value:
        - 23205
      service: modbus.write_register
#高速
  - alias: fresh_air_f2_speed_high
    trigger:
      entity_id: input_select.fresh_air_f2_speed
      from: 低档
      platform: state
      to: 高档
      entity_id: input_select.fresh_air_f2_speed
      from: 中档
      platform: state
      to: 高档
    condition: []
    action:
    - data:
        address: 8965
        hub: hub2
        unit: 2
        value:
        - 2
      service: modbus.write_register
    - data:
        address: 8962
        hub: hub2
        unit: 2
        value:
        - 23205
      service: modbus.write_register
附上配置文件下载(放到packages目录下)
fresh_air.rar (1.71 KB, 下载次数: 145)

评分

参与人数 2金钱 +36 HASS币 +10 收起 理由
sorrypqa + 16 膜拜大神!
+ 20 + 10 厉害了word楼主!

查看全部评分

回复

使用道具 举报

8

主题

318

帖子

2172

积分

论坛技术达人

积分
2172
金钱
1844
HASS币
70
发表于 2020-1-17 13:50:47 | 显示全部楼层
如果这种稳定可靠,我直接就借鉴了!感谢分享
回复

使用道具 举报

9

主题

217

帖子

873

积分

高级会员

Rank: 4

积分
873
金钱
646
HASS币
40
 楼主| 发表于 2020-1-17 13:59:29 | 显示全部楼层
yuyamin 发表于 2020-1-17 13:50
如果这种稳定可靠,我直接就借鉴了!感谢分享

稳定的,就是感觉写的有点长,水平好的,可以写的更好,坐等高手出来指点
回复

使用道具 举报

8

主题

318

帖子

2172

积分

论坛技术达人

积分
2172
金钱
1844
HASS币
70
发表于 2020-1-17 14:05:51 | 显示全部楼层
lanhome 发表于 2020-1-17 13:59
稳定的,就是感觉写的有点长,水平好的,可以写的更好,坐等高手出来指点 ...

我一直用node-red转mqtt用的,最近经常崩
回复

使用道具 举报

2

主题

711

帖子

5043

积分

论坛元老

Rank: 8Rank: 8

积分
5043
金钱
4332
HASS币
85
发表于 2020-1-17 14:09:25 | 显示全部楼层
请问register:数值如何算?
回复

使用道具 举报

2

主题

240

帖子

1761

积分

金牌会员

Rank: 6Rank: 6

积分
1761
金钱
1521
HASS币
0
发表于 2020-1-17 15:09:14 | 显示全部楼层
大神  TCP-485  modbus产品推荐一个
回复

使用道具 举报

9

主题

217

帖子

873

积分

高级会员

Rank: 4

积分
873
金钱
646
HASS币
40
 楼主| 发表于 2020-1-17 16:52:28 | 显示全部楼层
nice2943 发表于 2020-1-17 14:09
请问register:数值如何算?

一般产品协议说明里写了16位的地址码的,转换成10进制就好了
回复

使用道具 举报

9

主题

217

帖子

873

积分

高级会员

Rank: 4

积分
873
金钱
646
HASS币
40
 楼主| 发表于 2020-1-17 16:53:30 | 显示全部楼层
心无痕星有迹 发表于 2020-1-17 15:09
大神  TCP-485  modbus产品推荐一个

你某宝搜485 MODBUS基本就出来了,很便宜的,我不好说产品
回复

使用道具 举报

2

主题

240

帖子

1761

积分

金牌会员

Rank: 6Rank: 6

积分
1761
金钱
1521
HASS币
0
发表于 2020-1-17 21:25:46 | 显示全部楼层
lanhome 发表于 2020-1-17 16:53
你某宝搜485 MODBUS基本就出来了,很便宜的,我不好说产品

按照这一波操作  只要厂家给提供通讯地址,是不是也可以把485的其他设备也接入了
回复

使用道具 举报

2

主题

711

帖子

5043

积分

论坛元老

Rank: 8Rank: 8

积分
5043
金钱
4332
HASS币
85
发表于 2020-1-17 21:51:16 | 显示全部楼层
lanhome 发表于 2020-1-17 16:52
一般产品协议说明里写了16位的地址码的,转换成10进制就好了

2300转成10进化应该是8960~!
8704不知如何算
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 01:09 , Processed in 0.833043 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表