Skip to content
Get started

Modbus Controller Switch

The modbus_controller switch platform creates a switch from a modbus_controller component and requires Modbus Controller to be configured.

  • register_type (Required): type of the modbus register.

    • coil : Coils are 1-bit registers (on/off values) that are used to control discrete outputs. They may be read and/or written. Modbus Function Code 1 (Read Coil Status) will be used.
    • discrete_input : discrete input register (read only coil) are similar to coils but can only be read. Modbus Function Code 2 (Read Input Status) will be used.
    • holding : Holding Registers - Holding registers are the most universal 16-bit register. They may be read and/or written. Modbus Function Code 3 (Read Holding Registers) will be used.
    • input : Input Registers - 16-bit registers used for input; may only be read. Modbus Function Code 4 (Read Input Registers) will be used. read is an accepted alias for this value.
  • address (Required, int): start address of the first register in a range (can be decimal or hexadecimal).

  • assumed_state (Optional boolean): Disables updates (periodic read commands) for this register. Default is false.

  • skip_updates (Optional, int): Deprecated and no longer has any effect — every register range is polled on each update_interval. The key is still accepted (logging a config-time warning) and will be removed in 2027.3.0. To poll some registers less often, place those sensors on a second modbus_controller with the same address: and a slower update_interval:.

  • use_write_multiple (Optional, boolean): By default the modbus command Function Code 6 (Preset Single Registers) is used for setting the holding register if only one register is set. If your device only supports Function Code 16 (Preset Multiple Registers) set this option to true.

  • bitmask (Optional, int): Some values are packed in a response. The bitmask is used to determined if the result is true or false. See Bitmasks.

  • lambda (Optional, lambda): Lambda to be evaluated every update interval to read the status of the switch.

  • write_lambda (Optional, lambda): Lambda called before send. Lambda is evaluated before the modbus write command is created.

    Parameters passed into the lambda

    • x (bool): The new boolean state being set
    • item (ModbusSwitch *): The entity as its own modbus device: the write helpers and queue_pdu() are available - for example item->write_single_register(0x1234, 42) or item->queue_pdu(...) - regardless of the switch’s own register type. Also a SensorItem subclass, so the configured address, offset, bitmask, etc. are accessible.
    • payload (PduBuffer &payload): Deprecated, removed in 2027.3.0 - use item instead. An empty buffer; if the lambda fills it (push_back/clear/assign - not the full std::vector API), it is sent as a full modbus request frame - the leading device-address byte followed by the PDU (function code + data); the CRC is added automatically. Unlike custom_pdu below (which is PDU-only), the device-address byte is part of this payload - it is the wire target address and is sent as given, normally this controller’s address:. If the buffer is filled, the return value is ignored.

    Possible return values for the lambda:

    • return true; or return false; the value to be written to the device. The switch still reports the requested state x; return a different value to translate between the ESPHome state and an inverted modbus transmitted value.
    • return {}; if the lambda handled (or deliberately skipped) the write itself, for example via item->write_*().
  • custom_pdu (Optional, list of bytes): The modbus PDU (function code + data) for a custom command. This allows using non-standard commands. The device address (taken from the controller’s address:) and the CRC are added automatically, so do not include a leading device-address byte. If custom_pdu is used, address and register_type can’t be used. Renamed from custom_command, which is now removed and raises a config-validation error. See Using custom_pdu for how to use custom_pdu

  • offset (Optional, int): Offset from start address in bytes (only required for uncommon response encodings). If more than one register is written in a command, this value is used to find the start of this datapoint relative to the start address. The component calculates the size of the range based on offset and size of the value type. The value for offset depends on the register type. For holding input registers, the offset is in bytes. For coil and discrete input resisters, the LSB of the first data byte contains the coil addressed in the request. The other coils follow toward the high-order end of this byte and from low order to high order in subsequent bytes. For registers, the offset is the position of the relevant bit. To get the value of the coil register, 2 can be retrieved using address: 2 / offset: 0 or address: 0 / offset 2.

  • restore_mode (Optional): See Switch, since this configuration variable is inherited. The default value for this setting is DISABLED (recommended). DISABLED leaves the initial state up to the hardware: usually the state lives in the device and ESPHome does not need to remember it. The switch frontend will show an undetermined state until the real state is retrieved from the device on the next refresh. Use any other setting if a reboot of your ESPHome device is tied to a reboot of the modbus device.

switch:
- platform: modbus_controller
modbus_controller_id: epever
id: enable_load_test
register_type: coil
address: 2
name: "enable load test mode"
bitmask: 1
switch:
- platform: modbus_controller
modbus_controller_id: epever
id: enable_load_test
register_type: coil
address: 2
name: "enable load test mode"
write_lambda: |-
ESP_LOGD("main","Modbus Switch incoming state = %d",x);
// return !x; // use this to write an inverted value (the switch still reports x)
// deliberately targets a different coil (0x0006) than the entity's own address
item->write_single_coil(0x0006, x);
// the lambda must return something - return empty, the write is already done
return {};

Since offset is not zero the read command is part of a range and will be parsed when the range is updated. The write command to be constructed uses the function code to determine the write command. For a coil it is write single coil. Because the write command only touches one register start_address and offset have to be corrected. The final command will be write_single_coil Function Code 5 address (start_address+offset) value 1 or 0

For holding registers the write command will be write_single_register (Function Code 6 (Preset Single Registers)). Because the offset for holding registers is given in bytes and the size of a register is 16 bytes the start_address is calculated as start_address + offset/2

switch:
- platform: modbus_controller
modbus_controller_id: ventilation_system
name: "enable turn off"
register_type: holding
address: 25
bitmask: 1
entity_category: config
icon: "mdi:toggle-switch"