Smart Home Automation Forum
how to add relay board to ESPHome by Modbus Controller Switch - Printable Version

+- Smart Home Automation Forum (https://www.kincony.com/forum)
+-- Forum: Technical Support (https://www.kincony.com/forum/forumdisplay.php?fid=1)
+--- Forum: KC868-A series and Uair Smart Controller (https://www.kincony.com/forum/forumdisplay.php?fid=6)
+--- Thread: how to add relay board to ESPHome by Modbus Controller Switch (/showthread.php?tid=2757)



how to add relay board to ESPHome by Modbus Controller Switch - admin - 04-03-2023

   
[Image: attachment.php?aid=2232]   
uart:
  id: mbus
  tx_pin: GPIO13
  rx_pin: GPIO16
  baud_rate: 9600

modbus:
  id: modbus1
  uart_id: mbus
  send_wait_time: 200ms

modbus_controller:
- id: a6
  address: 1
  modbus_id: modbus1
  update_interval: 1s


switch:
  - platform: modbus_controller
    name: 'modbus-switch1'
    address: 0  # 0:relay1  1:relay2 ......
    register_type: coil
    bitmask: 1


this is a demo , i have created one switch button in ESPHome, you can create many others.

KinCony Products use modbus command:
01 05 00 00 FF 00  turn on relay1
01 05 00 00 00 00  turn off relay1

make sure your relay board as these:
ON = 0xFF00  OFF=0000

if your relay board not use by this protocol. you can also write esphome as "write_lambda" way. i have tested, "write_lambda" to create switch also work fine. just need to create ON and OFF two command.

    write_lambda: |-
      ESP_LOGD("main","Modbus Switch incoming state = %f",x);
      // return false ; // use this to just change the value
      payload.push_back(0x1);  // device address
      payload.push_back(0x5);  // force single coil
      payload.push_back(0x00); // high byte address of the coil
      payload.push_back(0x00);  // low byte address of the coil
      payload.push_back(0xFF); // ON = 0xFF00 OFF=0000
      payload.push_back(0x00);
      return true;