This is a modbus I/O device. Inputs: 8 digital Outputs: 8 SPDT relay

default ip address: 192.168.1.200 default password: password or maybe Password or maybe it doesn't matter

Module is in "linkage mode" by default which means the output relays are tied to the inputs. I want them to operate independently (and you probably do too) so we need to change the relays to "normal mode" by writing velue to register.

to use this in Home Assitant: On the web interface, change protocol from "None" to "Modbus TCP to RTU"

modbus:
  - name: modbus_hub
    type: tcp
    host: 10.73.73.55
    port: 4196
    switches:
        # switches for controllng the 8 relays
      - name: Relay 1
        address: 0
        slave: 1
        write_type: coil
      - name: Relay 2
        address: 1
        slave: 1
        write_type: coil
      - name: Relay 3
        address: 2
        slave: 1
        write_type: coil
      - name: Relay 4
        address: 3
        slave: 1
        write_type: coil
      - name: Relay 5
        address: 4
        slave: 1
        write_type: coil
      - name: Relay 6
        address: 5
        slave: 1
        write_type: coil
      - name: Relay 7
        address: 6
        slave: 1
        write_type: coil
      - name: Relay 8
        address: 7
        slave: 1
        write_type: coil
        # switches for toggling the sontrol mode of the relays between normal mode (0,off) and linkage mode(1,on)
        # other possible values for your on and off are toggle mode (2, on or off, you pick) and edge mode (3, on or off, you pick)
      - name: Set Mode Relay 1
        address: 0x1000
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 2
        address: 0x1001
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 3
        address: 0x1002
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 4
        address: 0x1003
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 5
        address: 0x1004
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 6
        address: 0x1005
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 7
        address: 0x1006
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
      - name: Set Mode Relay 8
        address: 0x1007
        slave: 1
        command_on: 1
        command_off: 0
        write_type: holding
    binary_sensors:
      - name: Binary Inputs
        address: 0x0000
        slave: 1
        slave_count: 7
        input_type: discrete_input
        unique_id: Inputs
    sensors:
        # sensors for reading the relay control mode of the 8 relays.
      - name: Mode 1
        address: 0x1000
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 2
        address: 0x1001
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 3
        address: 0x1002
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 4
        address: 0x1003
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 5
        address: 0x1004
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 6
        address: 0x1005
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 7
        address: 0x1006
        slave: 1
        input_type: holding
        data_type: int16
      - name: Mode 8
        address: 0x1007
        slave: 1
        input_type: holding
        data_type: int16

pip install pymodbus Change 'localhost' to appropriate IP address Change port to 4196 (or whatever)

from pymodbus.client import ModbusTcpClient

# Connect to the Modbus server
client = ModbusTcpClient('10.73.73.55', port=4196)
client.connect()

try:
    print("Connected to Modbus server")

    # Read holding registers from address 0x1000 to 0x1007 (8 registers)
    result = client.read_holding_registers(0x1000, count=8)
    if result.isError():
        print("Error reading registers:", result)
    else:
        print("Initial values:")
        for i in range(8):
            # Since endian isn't supported, we'll manually handle big-endian
            value = result.registers[i]
            print(f"Register 0x{0x1000 + i:04X}: {value}")

        # Prepare to write 0x0000 to each register
        # change the payload to 1 
        payload = [0] * 8  # 8 registers set to 0x0000

        # Write 0x0000 to registers 0x1000 to 0x1007
        write_result = client.write_registers(0x1000, payload)
        if write_result.isError():
            print("Error writing registers:", write_result)
        else:
            print("Registers set to 0x0000")

        # Read back to verify
        verify_result = client.read_holding_registers(0x1000, count=8)
        if verify_result.isError():
            print("Error verifying registers:", verify_result)
        else:
            print("Values after setting:")
            for i in range(8):
                value = verify_result.registers[i]
                print(f"Register 0x{0x1000 + i:04X}: {value}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    client.close()

For telegraf, have to set Protocol to "None" under Multi-host settings and use transmiossion mode RTUoverTCP in telegraf. If not, you will get errors like: telegrasf Error in plugin: slave 1 on controller : modbus: response transaction id '1' does not match request '7' There is some kind of desynchronization with the transaction ids that telegraf never recovers from. This is from wither a flaw waveshare's Modbus TCP to RTU mechanism or in the library telegraf is using to query the Modbs device. I don't know enough about it to figure out which.

also this error: 2025-02-23T12:08:10Z W! [inputs.modbus] Collection took longer than expected; not complete after interval of 10s

[[inputs.modbus]]
  name = "lift_station"
  controller = "tcp://10.73.73.55:4196"
  configuration_type = "request"

  #transmission_mode = "auto"
  transmission_mode = "RTUoverTCP"

  [[inputs.modbus.request]]
    slave_id = 1
    register = "discrete"
    measurement = "lift_station_status"
    fields = [
      {name = "float_power_1", address = 0},
      {name = "bottom_float_2", address = 1},
      {name = "lead_float_3", address = 2},
      {name = "lag_float_4",  address = 3},
      {name = "alarm_float_5", address = 4},
      {name = "pump_1_power_6", address = 5},
      {name = "pump_2_power_7", address = 6},
      {name = "unused_8", address = 7},
    ]


[[outputs.file]]
  files = ["stdout"]
  data_format = "influx"