diff --git a/components/binary_sensor/modbus_controller.rst b/components/binary_sensor/modbus_controller.rst index 1c0b83197..8e8ecea77 100644 --- a/components/binary_sensor/modbus_controller.rst +++ b/components/binary_sensor/modbus_controller.rst @@ -16,26 +16,25 @@ Configuration variables: - **name** (**Required**, string): The name of the sensor. - **register_type** (**Required**): type of the modbus register. - - ``coil``: Function 01 (01hex) Read Coils - Reads the ON/OFF status of discrete coils in the device. - - ``discrete_input``: Function 02(02hex) - Reads the ON/OFF status of discrete inputs in the device. - - ``holding``: Function 03 (03hex) Read Holding Registers - Read the binary contents of holding registers in the device. - - ``read``: Function 04 (04hex) Read Input Registers - Read the binary contents of input registers in the device. + - ``coil``: Coils are 1-bit registers (ON/OFF values) that are used to control discrete outputs. Read and Write access. 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. Read and Write access. Modbus *Function Code 3 (Read Holding Registers)* will be used. + - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read. Modbus *Function Code 4 (Read Input Registers)* will be used. -- **address** (**Required**, int): start address of the first register in a range -- **bitmask** (*Optional*, int): Some values are packed in a response. The bitmask is used to determined if the result is true or false -- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). +- **bitmask** (*Optional*, int): sometimes multiple values are packed in a single register's response. The bitmask is used to determined if the result is true or false. See :ref:`bitmasks`. +- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle. Note: The modbus_controller groups components by address ranges to reduce number of transactions. All components with the same starting address will be updated in one request. ``skip_updates`` applies for *all* components in the same range. +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. - **response_size** (*Optional*, int): Size of the response for the register in bytes. Defaults to register_count*2. - **force_new_range** (*Optional*, boolean): If possible sensors with sequential addresses are grouped together and requested in one range. Setting ``force_new_range: true`` enforces the start of a new range at that address. - **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. - custom data must contain all required bytes including the modbus device address. The crc is automatically calculated and appended to the command. - See :ref:`modbus_custom_command` how to use ``custom_command`` + Custom data must contain all required bytes including the modbus device address. The CRC is automatically calculated and appended to the command. + See :ref:`modbus_custom_command` how to use ``custom_command``. - **lambda** (*Optional*, :ref:`lambda `): - Lambda to be evaluated every update interval to get the new value of the sensor - - Parameters + Lambda to be evaluated every update interval to get the new value of the sensor. Parameters: - **x** (bool): The parsed float value of the modbus data - - **data** (std::vector): vector containing the complete raw modbus response bytes for this sensor - **item** (const pointer to a ModbusBinarySensor object): The sensor object itself. Possible return values for the lambda: @@ -43,21 +42,19 @@ Configuration variables: - ``return true/false;`` the new value for the sensor. -- **offset** (*Optional*, int): not required for most cases - offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to 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. If a binary_sensor is created from an input register 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 the registers 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 + - **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. If a binary_sensor is created from an input register, 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``. +- All other options from :ref:`Binary Sensor `. -Example +Example: +-------- .. code-block:: yaml binary_sensor: - platform: modbus_controller - modbus_controller_id: epever - id: battery_internal_resistance_abnormal - name: "Battery internal resistance abnormal" + modbus_controller_id: modbus1 + name: "Error status" register_type: read address: 0x3200 bitmask: 0x80 #(bit 8) @@ -65,11 +62,14 @@ Example See Also -------- -- :apiclass:`:modbus_controller::ModbusBinarySensor` +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` -- :doc:`/components/switch/modbus_controller` -- :doc:`/components/output/modbus_controller` - :doc:`/components/sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` +- :doc:`/components/switch/modbus_controller` +- :doc:`/components/number/modbus_controller` +- :doc:`/components/select/modbus_controller` - :doc:`/components/text_sensor/modbus_controller` - https://www.modbustools.com/modbus.html +- :apiclass:`:modbus_controller::ModbusBinarySensor` - :ghedit:`Edit` diff --git a/components/modbus.rst b/components/modbus.rst index 30a900135..fd968fbf7 100644 --- a/components/modbus.rst +++ b/components/modbus.rst @@ -7,9 +7,32 @@ Modbus Component :description: Instructions for setting up Modbus in ESPHome. :keywords: Modbus -The ModBUS protocol is used by many consumer and industrial devices for communication. -This component allows components in ESPHome to communicate to those devices. -ModBUS requires a :ref:`UART Bus ` to communicate. +The Modbus protocol is used by many consumer and industrial devices for communication. +This component allows components in ESPHome to communicate to those devices via RTU protocol. You can access the coils, inputs, holding, read registers from your devices as sensors, switches, selects, numbers or various other ESPHome components and present them to your favorite Home Automation system. You can even write them as binary or float ouptputs from ESPHome. + +The various sub-components implement some of the Modbus functions below (depending on their required functionality): + ++---------------+----------------------------+ +| Function Code | Description | ++===============+============================+ +| 1 | Read Coil Status | ++---------------+----------------------------+ +| 2 | Read Discrete input Status | ++---------------+----------------------------+ +| 3 | Read Holding Registers | ++---------------+----------------------------+ +| 4 | Read Input Registers | ++---------------+----------------------------+ +| 5 | Write Single Coil | ++---------------+----------------------------+ +| 6 | Write Single Register | ++---------------+----------------------------+ +| 15 | Write Multiple Coils | ++---------------+----------------------------+ +| 16 | Write Multiple Registers | ++---------------+----------------------------+ + +Modbus RTU requires a :ref:`UART Bus ` to communicate. .. code-block:: yaml @@ -45,10 +68,11 @@ See Also - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` - :doc:`/components/switch/modbus_controller` - :doc:`/components/number/modbus_controller` -- :doc:`/components/output/modbus_controller` +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` - `Modbus RTU Protocol Description `__ - :ref:`uart` - :apiref:`modbus/modbus.h` diff --git a/components/modbus_controller.rst b/components/modbus_controller.rst index e098ebb63..c21b0a13e 100644 --- a/components/modbus_controller.rst +++ b/components/modbus_controller.rst @@ -2,11 +2,11 @@ Modbus Controller ================= .. seo:: - :description: Instructions for setting up the ModBUS Controller component. + :description: Instructions for setting up the Modbus Controller component. :image: modbus.png -The ``modbus_controller`` component creates a RS485 connection to control a ModBUS slave device, letting your ESPHome node to act as a ModBUS master. -You can access the coils and registers from your slave ModBUS device as sensors, switches or various other ESPHome components and present them to your favorite Home Automation system. +The ``modbus_controller`` component creates a RS485 connection to control a Modbus server (slave) device, letting your ESPHome node to act as a Modbus client (master). +You can access the coils, inputs, holding, read registers from your devices as sensors, switches, selects, numbers or various other ESPHome components and present them to your favorite Home Automation system. You can even write them as binary or float ouptputs from ESPHome. .. figure:: /images/modbus.png :align: center @@ -26,7 +26,7 @@ See `How is this RS485 module working? baud_rate: 0 - - Configuration variables: ------------------------ - **modbus_id** (*Optional*, :ref:`config-id`): Manually specify the ID of the ``modbus`` hub. -- **address** (**Required**, :ref:`config-id`): The ModBUS address of the slave device +- **address** (**Required**, :ref:`config-id`): The Modbus address of the slave device - **command_throttle** (*Optional*, :ref:`config-time`): minimum time in between 2 requests to the device. Default is ``0ms``. - Some ModBUS slave devices limit the rate of requests from the master, the interval between sending requests can be altered. + Some Modbus slave devices limit the rate of requests from the master, so this allows the interval between requests to be altered. - **update_interval** (*Optional*, :ref:`config-time`): The interval that the sensors should be checked. Defaults to 60 seconds. @@ -66,7 +64,6 @@ Configuration variables: slaves, this avoids waiting for timeouts allowing to read other slaves in the same bus. When the slave responds to a command, it'll be marked online again. - Example ------- The following code creates a ``modbus_controller`` hub talking to a ModBUS device at address ``1`` with ``115200`` bps @@ -76,54 +73,56 @@ Technically there is no difference between the "inline" and the standard definit .. code-block:: yaml + # Example configuration entry uart: - id: mod_bus - tx_pin: 17 - rx_pin: 16 - baud_rate: 115200 - stop_bits: 1 + ... modbus: flow_control_pin: 5 id: modbus1 modbus_controller: - - id: epever - address: 0x1 ## address of the ModBUS slave device on the bus - modbus_id: modbus1 - setup_priority: -10 - - text_sensor: - - name: "rtc_clock" - platform: modbus_controller - modbus_controller_id: epever - id: rtc_clock - internal: true - register_type: holding - address: 0x9013 ## address of the register inside the ModBUS slave device - register_count: 3 - raw_encode: HEXBYTES - response_size: 6 - - switch: - - platform: modbus_controller - modbus_controller_id: epever - id: reset_to_fabric_default - name: "Reset to Factory Default" - register_type: coil - address: 0x15 - bitmask: 1 + - id: modbus_device + address: 0x1 ## address of the Modbus slave device on the bus + modbus_id: modbus1 + setup_priority: -10 sensor: - - platform: modbus_controller - modbus_controller_id: epever - name: "Battery Capacity" - id: battery_capacity - register_type: holding - address: 0x9001 - unit_of_measurement: "AH" - value_type: U_WORD + - platform: modbus_controller + modbus_controller_id: modbus_device + name: "Battery Capacity" + register_type: holding + address: 0x9001 ## address of the register inside the Modbus slave device + unit_of_measurement: "AH" + value_type: U_WORD + switch: + - platform: modbus_controller + modbus_controller_id: modbus_device + name: "Reset to Factory Default" + register_type: coil + address: 0x15 + bitmask: 1 + + text_sensor: + - name: "rtc_clock" + platform: modbus_controller + modbus_controller_id: modbus_device + id: rtc_clock + internal: true + register_type: holding + address: 0x9013 + register_count: 3 + raw_encode: HEXBYTES + response_size: 6 + +The configuration example above creates a ``modbus_controller`` hub talking to a Modbus device at address ``1`` with a baudrate of ``115200`` bps, implementing a sensor, a switch and a text sensor. + +Check out the various Modbus components available at the bottom of the document in the :ref:`modbusseealso` section. They can be directly defined *(inline)* under the ``modbus_controller`` hub or as standalone components. Technically there is no difference between the *inline* and the standard definitions approach. + +Below you find a few general tips about using Modbus in more advanced scenarios. Applicable component functionalities have links pointing here: + +.. _bitmasks: Bitmasks -------- @@ -166,7 +165,7 @@ Some devices use decimal values in read registers to show multiple binary states | bit 15 | Binary Sensor 15 | 32768 | 8000 | +------------+------------------+-----------+-----------+ -For example, when reading register ``15``, a decimal value of ``12288`` is the sum of ``4096`` + ``8192``, meaning the corresponding bits ``12`` and ``13`` are ``1``, the other bits are ``0``. +In the example below, register ``15``, holds several binary values. It stores the decimal value ``12288``, which is the sum of ``4096`` + ``8192``, meaning the corresponding bits ``12`` and ``13`` are ``1``, the other bits are ``0``. To gather some of these bits as binary sensors in ESPHome, use ``bitmask``: @@ -174,39 +173,166 @@ To gather some of these bits as binary sensors in ESPHome, use ``bitmask``: binary_sensor: - platform: modbus_controller - modbus_controller_id: ventilation_system + modbus_controller_id: modbus1 name: Alarm bit0 - entity_category: diagnostic - device_class: problem register_type: read address: 15 bitmask: 0x1 - platform: modbus_controller - modbus_controller_id: ventilation_system + modbus_controller_id: modbus1 name: Alarm bit1 - entity_category: diagnostic - device_class: problem register_type: read address: 15 bitmask: 0x2 - platform: modbus_controller - modbus_controller_id: ventilation_system + modbus_controller_id: modbus1 name: Alarm bit10 - entity_category: diagnostic - device_class: problem register_type: read address: 15 bitmask: 0x400 - platform: modbus_controller - modbus_controller_id: ventilation_system + modbus_controller_id: modbus1 name: Alarm bit15 - entity_category: diagnostic - device_class: problem register_type: read address: 15 bitmask: 0x8000 +.. _modbus_custom_command: + +Using ``custom_command`` +------------------------ + +``custom_command`` can be used to create an arbitrary modbus command. Combined with a lambda any response can be handled. +This example re-implements the command to read the registers 0x156 (Total active energy) and 0x158 Total (reactive energy) from a SDM-120. +SDM-120 returns the values as floats using 32 bits in 2 registers. + +.. code-block:: yaml + + uart: + id: mod_uart + ... + + modbus: + send_wait_time: 200ms + uart_id: mod_uart + id: mod_bus + + modbus_controller: + - id: sdm + address: 2 + modbus_id: mod_bus + command_throttle: 100ms + setup_priority: -10 + update_interval: 30s + + sensors: + - platform: modbus_controller + modbus_controller_id: sdm + name: "Total active energy" + id: total_energy + # address: 0x156 + # register_type: "read" + ## reimplement using custom_command + # 0x2 : modbus device address + # 0x4 : modbus function code + # 0x1 : high byte of modbus register address + # 0x56: low byte of modbus register address + # 0x00: high byte of total number of registers requested + # 0x02: low byte of total number of registers requested + custom_command: [ 0x2, 0x4, 0x1, 0x56,0x00, 0x02] + value_type: FP32 + unit_of_measurement: kWh + accuracy_decimals: 1 + + - platform: modbus_controller + modbus_controller_id: sdm + name: "Total reactive energy" + # address: 0x158 + # register_type: "read" + custom_command: [0x2, 0x4, 0x1, 0x58, 0x00, 0x02] + ## the command returns an float value using 4 bytes + lambda: |- + ESP_LOGD("Modbus Sensor Lambda","Got new data" ); + union { + float float_value; + uint32_t raw; + } raw_to_float; + if (data.size() < 4 ) { + ESP_LOGE("Modbus Sensor Lambda", "invalid data size %d",data.size()); + return NAN; + } + raw_to_float.raw = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; + ESP_LOGD("Modbus Sensor Lambda", "FP32 = 0x%08X => %f", raw_to_float.raw, raw_to_float.float_value); + return raw_to_float.float_value; + unit_of_measurement: kVArh + accuracy_decimals: 1 + +.. _modbus_register_count: + +Optimizing modbus communications +-------------------------------- + +``register_count`` is an option only required for uncommon response encodings or to optimizie modbus communications. + +It describes the number of registers this data point spans, overriding the defaults determined by ``value_type``. If no value for ``register_count`` is provided, it is calculated based on the register type. The default size for one register is 16 bits (one word). Some devices are not adhering to this convention and have registers larger than 16 bits. In this case, ``register_count`` and ``response_size`` must be set. For example, if your Modbus device uses one register for a FP32 value (instead of the default of two), set ``register_count: 1`` and ``response_size: 4``. + +``register_count`` can also be used to skip a number of registers in consecutive range. + +An example is an SDM meter, with interesting data in register addresses 0, 2, 4 and 6: + +.. code-block:: yaml + + - platform: modbus_controller + name: "Voltage Phase 1" + address: 0 + register_type: "read" + value_type: FP32 + + - platform: modbus_controller + name: "Voltage Phase 2" + address: 2 + register_type: "read" + value_type: FP32 + + - platform: modbus_controller + name: "Voltage Phase 3" + address: 4 + register_type: "read" + value_type: FP32 + + - platform: modbus_controller + name: "Current Phase 1" + address: 6 + register_type: "read" + value_type: FP32 + accuracy_decimals: 1 + +The configuration above will generate *one* modbus command *read multiple registers from 0 to 6*. + +Maybe you don’t care about the data in register addresses 2 and 4, which are voltage values for Phase 2 and Phase 3 (or you have a SDM-120). +Of course, you can delete the sensors your don’t care about, but then you'd have a gap in the addresses. If you remove the registers at address 2 and 4, *two* commands will be generated -- *read register 0* and *read register 6*. To avoid generating multiple commands and thus reduce activity on the bus, ``register_count`` can be used to fill the gaps: + +.. code-block:: yaml + + - platform: modbus_controller + name: "Voltage Phase 1" + address: 0 + unit_of_measurement: "V" + register_type: "read" + value_type: FP32 + register_count: 6 + + - platform: modbus_controller + name: "Current Phase 1" + address: 6 + register_type: "read" + value_type: FP32 + +Because the option ``register_count: 6`` is used for the first sensor, *one* command *read multiple registers from 0 to 6* will be used but the values in between will be ignored. + +.. note:: *Calculation:* FP32 is a 32 bit value and uses 2 registers. Therefore, to skip the 2 FP32 registers the size of these 2 registers must be added to the default size for the first register. So we have 2 for address 0, 2 for address 2 and 2 for address 4 thus ``register_count`` must be 6. + Protocol decoding example ------------------------- @@ -214,87 +340,87 @@ Protocol decoding example .. code-block:: yaml sensors: - - platform: modbus_controller - modbus_controller_id: epever - id: array_rated_voltage - name: "array_rated_voltage" - address: 0x3000 - unit_of_measurement: "V" - register_type: read - value_type: U_WORD - accuracy_decimals: 1 - skip_updates: 60 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: array_rated_voltage + name: "array_rated_voltage" + address: 0x3000 + unit_of_measurement: "V" + register_type: read + value_type: U_WORD + accuracy_decimals: 1 + skip_updates: 60 + filters: + - multiply: 0.01 - - platform: modbus_controller - modbus_controller_id: epever - id: array_rated_current - name: "array_rated_current" - address: 0x3001 - unit_of_measurement: "V" - register_type: read - value_type: U_WORD - accuracy_decimals: 2 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: array_rated_current + name: "array_rated_current" + address: 0x3001 + unit_of_measurement: "V" + register_type: read + value_type: U_WORD + accuracy_decimals: 2 + filters: + - multiply: 0.01 - - platform: modbus_controller - modbus_controller_id: epever - id: array_rated_power - name: "array_rated_power" - address: 0x3002 - unit_of_measurement: "W" - register_type: read - value_type: U_DWORD_R - accuracy_decimals: 1 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: array_rated_power + name: "array_rated_power" + address: 0x3002 + unit_of_measurement: "W" + register_type: read + value_type: U_DWORD_R + accuracy_decimals: 1 + filters: + - multiply: 0.01 - -platform: modbus_controller - modbus_controller_id: epever - id: battery_rated_voltage - name: "battery_rated_voltage" - address: 0x3004 - unit_of_measurement: "V" - register_type: read - value_type: U_WORD - accuracy_decimals: 1 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: battery_rated_voltage + name: "battery_rated_voltage" + address: 0x3004 + unit_of_measurement: "V" + register_type: read + value_type: U_WORD + accuracy_decimals: 1 + filters: + - multiply: 0.01 - - platform: modbus_controller - modbus_controller_id: epever - id: battery_rated_current - name: "battery_rated_current" - address: 0x3005 - unit_of_measurement: "A" - register_type: read - value_type: U_WORD - accuracy_decimals: 1 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: battery_rated_current + name: "battery_rated_current" + address: 0x3005 + unit_of_measurement: "A" + register_type: read + value_type: U_WORD + accuracy_decimals: 1 + filters: + - multiply: 0.01 - - platform: modbus_controller - modbus_controller_id: epever - id: battery_rated_power - name: "battery_rated_power" - address: 0x3006 - unit_of_measurement: "W" - register_type: read - value_type: U_DWORD_R - accuracy_decimals: 1 - filters: - - multiply: 0.01 + - platform: modbus_controller + modbus_controller_id: epever + id: battery_rated_power + name: "battery_rated_power" + address: 0x3006 + unit_of_measurement: "W" + register_type: read + value_type: U_DWORD_R + accuracy_decimals: 1 + filters: + - multiply: 0.01 - - platform: modbus_controller - modbus_controller_id: epever id: charging_mode - name: "charging_mode" - address: 0x3008 - unit_of_measurement: "" - register_type: read - value_type: U_WORD - accuracy_decimals: 0 + - platform: modbus_controller + modbus_controller_id: epever id: charging_mode + name: "charging_mode" + address: 0x3008 + unit_of_measurement: "" + register_type: read + value_type: U_WORD + accuracy_decimals: 0 To minimize the required transactions all registers with the same base address are read in one request. @@ -379,8 +505,7 @@ The response is mapped to the sensor based on ``register_count`` and offset in b .. note:: - Write support is only implemented for switches and selects. - However the C++ code provides the required API to write to a ModBUS device. + Write support is only implemented for switches and selects; however, the C++ code provides the required API to write to a Modbus device. These methods can be called from a lambda. @@ -413,7 +538,7 @@ The response is mapped to the sensor based on ``register_count`` and offset in b // create the payload std::vector rtc_data = {uint16_t((minutes << 8) | seconds), uint16_t((day << 8) | hour), uint16_t((year << 8) | month)}; - // Create a ModBUS command item with the time information as the payload + // Create a Modbus command item with the time information as the payload esphome::modbus_controller::ModbusCommandItem set_rtc_command = esphome::modbus_controller::ModbusCommandItem::create_write_multiple_command(controller, 0x9013, 3, rtc_data); // Submit the command to the send queue @@ -517,17 +642,20 @@ The response is mapped to the sensor based on ``register_count`` and offset in b filters: - multiply: 0.01 +.. _modbusseealso: See Also -------- - :doc:`/components/modbus` +- :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` - :doc:`/components/switch/modbus_controller` - :doc:`/components/number/modbus_controller` -- :doc:`/components/output/modbus_controller` -- `ModBUS RTU Protocol Description `__ +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` +- `Modbus RTU Protocol Description `__ - `EPEVER MPPT Solar Charge Controller (Tracer-AN Series) `__ - `Genvex, Nibe, Alpha-Innotec heat recovery ventilation `__ - :ghedit:`Edit` diff --git a/components/number/modbus_controller.rst b/components/number/modbus_controller.rst index 9c5291c3b..3c5e2042b 100644 --- a/components/number/modbus_controller.rst +++ b/components/number/modbus_controller.rst @@ -12,8 +12,8 @@ Configuration variables: - **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation. - **name** (**Required**, string): The name of the sensor. -- **address** (**Required**, int): start address of the first register in a range -- **value_type** (**Required**): datatype of the mod_bus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first) +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). +- **value_type** (**Required**): datatype of the modbus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first): - ``U_WORD`` (unsigned 16 bit integer from 1 register = 16bit) - ``S_WORD`` (signed 16 bit integer from 1 register = 16bit) @@ -28,14 +28,16 @@ Configuration variables: - ``FP32`` (32 bit IEEE 754 floating point from 2 registers) - ``FP32_R`` (32 bit IEEE 754 floating point - same as FP32 but low word first) -- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle - Note: The modbus_controller groups component by address ranges to reduce number of transactions. All components with the same address will be updated in one request. skip_updates applies for all components in the same range. -- **response_size** (*Optional*): Size of the response for the register in bytes. Defaults to register_count*2. -- **force_new_range** (*Optional*, boolean): If possible sensors with sequential addresses are grouped together and requested in one range. Setting ``force_new_range: true`` enforces the start of a new range at that address. -- **offset** (*Optional*, int): only required for uncommon response encodings offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to start address. The component calculates the size of the range based on offset and size of the value type - **min_value** (*Optional*, float): The minimum value this number can be. - **max_value** (*Optional*, float): The maximum value this number can be. -- **step** (*Optional*, float): The granularity with which the number can be set. Defaults to 1 +- **step** (*Optional*, float): The granularity with which the number can be set. Defaults to 1. +- **multiply** (*Optional*, float): multiply the new value with this factor before sending the requests. Ignored if lambda is defined. +- **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``. +- **skip_updates** (*Optional*, int): By default, all sensors of a modbus_controller are updated together. For data points that don't change very frequently, updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle. Note: The modbus_controller groups components by address ranges to reduce number of transactions. All components with the same starting address will be updated in one request. ``skip_updates`` applies for *all* components in the same range. +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. +- **response_size** (*Optional*): Size of the response for the register in bytes. Defaults to register_count*2. +- **force_new_range** (*Optional*, boolean): If possible sensors with sequential addresses are grouped together and requested in one range. Setting ``force_new_range: true`` enforces the start of a new range at that address. +- **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 start address. The component calculates the size of the range based on offset and size of the value type. - **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. custom data must contain all required bytes including the modbus device address. The crc is automatically calculated and appended to the command. See :ref:`modbus_custom_command` how to use ``custom_command`` @@ -71,19 +73,24 @@ Configuration variables: - ``return ; and fill payload with data`` if the payload is added from the lambda then these 16 bit words will be sent - ``return {};`` if you don't want write the command to the device (or do it from the lambda). -- **multiply** (*Optional*, float): multiply the new value with this factor before sending the requests. Ignored if lambda is defined. -- **use_write_multiple** (*Optional*, boolean): By default the modbus command ``Preset Single Registers`` (function code 6) is used for setting the holding register if only 1 register is set. If your device only supports ``Preset Multiple Registers`` (function code 16) set this option to true. +- All other options from :ref:`Number `. - -All other options from :ref:`Number `. - -**Example** +Example: +-------- .. code-block:: yaml number: - platform: modbus_controller - modbus_controller_id: epever + modbus_controller_id: modbus1 + id: battery_capacity_number + name: "Battery Cap Number" + address: 0x9001 + value_type: U_WORD + multiply: 1.0 + + - platform: modbus_controller + modbus_controller_id: modbus1 id: battery_capacity_number name: "Battery Cap Number" address: 0x9001 @@ -94,17 +101,17 @@ All other options from :ref:`Number `. uint16_t b_capacity = x ; payload.push_back(b_capacity); return x * 1.0 ; - ## multiply is ignored because lamdba is used - multiply: 1.0 - See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` -- :doc:`/components/switch/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` - :doc:`/components/output/modbus_controller` +- :doc:`/components/switch/modbus_controller` +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` + - https://www.modbustools.com/modbus.html - :ghedit:`Edit` diff --git a/components/output/modbus_controller.rst b/components/output/modbus_controller.rst index 3ca458f87..7a5379135 100644 --- a/components/output/modbus_controller.rst +++ b/components/output/modbus_controller.rst @@ -2,17 +2,15 @@ Modbus Controller Output ======================== .. seo:: - :description: Instructions for setting up a modbus_controller device sensor. + :description: Instructions for setting up a modbus_controller device output. -The ``modbus_controller`` platform creates a output from a modbus_controller. +The ``modbus_controller`` platform creates an output from a modbus_controller. The goal is to write a value to a modbus register on a device. Configuration variables: ------------------------ -- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation. -- **name** (**Required**, string): The name of the sensor. -- **address** (**Required**, int): start address of the first register in a range -- **value_type** (**Required**): data type of the modbus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first) +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). +- **value_type** (**Required**): data type of the modbus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first). - ``U_WORD`` (unsigned 16 bit integer from 1 register = 16bit) - ``S_WORD`` (signed 16 bit integer from 1 register = 16bit) @@ -27,14 +25,19 @@ Configuration variables: - ``FP32`` (32 bit IEEE 754 floating point from 2 registers) - ``FP32_R`` (32 bit IEEE 754 floating point - same as FP32 but low word first) +- **register_type** (*Optional*): + - ``coil``: Write Coil - Write the ON/OFF status of a discrete coil in the device with *Function Code 5 or 15*. This will create a binary output. + - ``holding``: Write Holding Registers - write contents of holding registers in the device with *Function Code 6 or 16*. This will create a float output. + +- **multiply** (*Optional*, float): multiply the incoming value with this factor before writing it to the device. Ignored if ``write_lambda`` is defined. Only valid for ``register_type: holding``. +- **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``. - **write_lambda** (*Optional*, :ref:`lambda `): Lambda is evaluated before the modbus write command is created. The value is passed in as ``float x`` and an empty vector is passed in as ``std::vector&payload``. You can directly define the payload by adding data to payload then the return value is ignored and the content of payload is used. Parameters passed into the lambda - - **x** (float): The float value to be sent to the modbus device for ``register_type: holding`` - - **x** (bool): The boolean value to be sent to the modbus device for ``register_type: coil`` + - **x** (float or bool): The float value to be sent to the modbus device for ``register_type: holding`` or the boolean value to be sent to the modbus device for ``register_type: coil`` - **payload** (```std::vector&payload```): - for ``register_type: holding``: empty vector for the payload. The lamdba can add 16 bit raw modbus register words. @@ -49,41 +52,51 @@ Configuration variables: - ``return ; and fill payload with data`` if the payload is added from the lambda then these 16 bit words will be sent - ``return {};`` if you don't want write the command to the device (or do it from the lambda). -- **register_type** (*Optional*): ``coil`` to create a binary outout or ``holding`` to create a float output. -- **multiply** (*Optional*, float): multiply the new value with this factor before sending the requests. Ignored if lambda is defined. Only valid for ``register_type: holding``. -- **offset** (*Optional*, int): only required for uncommon response encodings - offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to start address. The component calculates the size of the range based on offset and size of the value type -- **use_write_multiple** (*Optional*, boolean): By default the modbus command ``Preset Single Registers`` (function code 6) is used for setting the holding register if only 1 register is set. If your device only supports ``Preset Multiple Registers`` (function code 16) set this option to true. +- **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 start address. The component calculates the size of the range based on offset and size of the value type. +- **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation. All other options from :ref:`Output `. -**Example** +Example: +-------- .. code-block:: yaml output: - - platform: modbus_controller - modbus_controller_id: epever - id: battery_capacity_output - write_lambda: |- - ESP_LOGD("main","Modbus Output incoming value = %f",x); - uint16_t b_capacity = x ; - payload.push_back(b_capacity); - return x * 1.0 ; - address: 0x9001 - value_type: U_WORD + - platform: modbus_controller + modbus_controller_id: modbus1 + address: 2048 + register_type: holding + value_type: U_WORD + multiply: 1000 +**The same with lambda:** + +.. code-block:: yaml + + output: + - platform: modbus_controller + modbus_controller_id: modbus1 + address: 2048 + value_type: U_WORD + write_lambda: |- + ESP_LOGD("main","Modbus Output incoming value = %f",x); + uint16_t value = x ; + payload.push_back(value); + return x * 1000 ; See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` - :doc:`/components/switch/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` - :doc:`/components/number/modbus_controller` +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` - https://www.modbustools.com/modbus.html - :ghedit:`Edit` diff --git a/components/select/modbus_controller.rst b/components/select/modbus_controller.rst index 4cc9244a6..1c90d8526 100644 --- a/components/select/modbus_controller.rst +++ b/components/select/modbus_controller.rst @@ -7,27 +7,12 @@ Modbus Controller Select The ``modbus_controller`` Select platform allows you to create a Select from modbus registers. -.. code-block:: yaml - - # Example configuration entry - select: - - platform: modbus_controller - name: "Modbus Select Register 1000" - address: 1000 - value_type: U_WORD - optionsmap: - "Zero": 0 - "One": 1 - "Two": 2 - "Three": 3 - - Configuration variables: ------------------------ - **name** (**Required**, string): The name of the Select. - **address** (**Required**, int): The start address of the first or only register - of the Select. + of the Select (can be decimal or hexadecimal). - **optionsmap** (**Required**, Map[str, int]): Provide a mapping from options (str) of this Select to values (int) of the modbus register and vice versa. All options and all values have to be unique. @@ -48,11 +33,8 @@ Configuration variables: required for uncommon response encodings or to :ref:`optimize modbus communications`. Overrides the defaults determined by ``value_type``. -- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are - updated together. For data points that don't change very frequently updates can be skipped. A - value of 5 would only update this sensor range in every 5th update cycle. Defaults to ``0``. - Note: The modbus_controller merges several registers into groups which are updated together. For - each group the smallest update cycle is used. +- **skip_updates** (*Optional*, int): By default, all sensors of a modbus_controller are updated together. For data points that don't change very frequently, updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle. Note: The modbus_controller groups components by address ranges to reduce number of transactions. All components with the same starting address will be updated in one request. ``skip_updates`` applies for *all* components in the same range. +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. - **force_new_range** (*Optional*, boolean): If possible sensors with sequential addresses are grouped together and requested in one range. Setting this to ``true`` enforces the start of a new range at that address. @@ -75,17 +57,13 @@ Configuration variables: - **write_lambda** (*Optional*, :ref:`lambda `): Lambda to be evaluated on every update of the Sensor, before the new value is written to the modbus registers. -- **use_write_multiple** (*Optional*, boolean): By default the modbus command ``Preset Single Registers`` - (function code 6) is used for setting the holding register if only 1 register is set. If your device only supports *Preset Multiple Registers* (function code 16) set this option to ``true``. Defaults - to ``false``. +- **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``. - **optimistic** (*Optional*, boolean): Whether to operate in optimistic mode - when in this mode, any command sent to the Modbus Select will immediately update the reported state. Defaults to ``false``. - All other options from :ref:`Select `. - - - .. code-block:: yaml # example @@ -136,15 +114,34 @@ Possible return values for the lambda: // ignore update return {}; +Example: +-------- + +.. code-block:: yaml + + # Example configuration entry + select: + - platform: modbus_controller + name: "Modbus Select Register 1000" + address: 1000 + value_type: U_WORD + optionsmap: + "Zero": 0 + "One": 1 + "Two": 2 + "Three": 3 + See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` -- :doc:`/components/switch/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` - :doc:`/components/output/modbus_controller` +- :doc:`/components/switch/modbus_controller` +- :doc:`/components/number/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` - :ref:`automation` - https://www.modbustools.com/modbus.html - :ghedit:`Edit` diff --git a/components/sensor/modbus_controller.rst b/components/sensor/modbus_controller.rst index cd20e6764..a0ae94dae 100644 --- a/components/sensor/modbus_controller.rst +++ b/components/sensor/modbus_controller.rst @@ -15,13 +15,13 @@ Configuration variables: - **name** (**Required**, string): The name of the sensor. - **register_type** (**Required**): type of the modbus register. - - ``coil``: coils are also called discrete output. Coils are 1-bit registers (on/off values) that are used to control discrete outputs. Read and Write access. 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. Read and Write access. Modbus function code 3 (Read Holding Registers) will be used. - - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read. Modbus function code 4 (Read Input Registers) will be used. + - ``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. + - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read. Modbus *Function Code 4 (Read Input Registers)* will be used. -- **address** (**Required**, int): start address of the first register in a range -- **value_type** (**Required**): datatype of the mod_bus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first) +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). +- **value_type** (**Required**): data type of the modbus register data. The default data type for modbus is a 16 bit integer in big endian format (MSB first). - ``U_WORD``: unsigned 16 bit integer from 1 register = 16bit - ``S_WORD``: signed 16 bit integer from 1 register = 16bit @@ -36,19 +36,11 @@ Configuration variables: - ``FP32``: 32 bit IEEE 754 floating point from 2 registers - ``FP32_R``: 32 bit IEEE 754 floating point - same as FP32 but low word first -- **bitmask** (*Optional*, int): some values are packed in a response. The bitmask can be used to extract a value from the response. For example, if the high byte value register 0x9013 contains the minute value of the current time. To only exctract this value use bitmask: 0xFF00. The result will be automatically right shifted by the number of 0 before the first 1 in the bitmask. For 0xFF00 (0b1111111100000000) the result is shifted 8 posistions. More than one sensor can use the same address/offset if the bitmask is different. -- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle - Note: The modbus_controller groups component by address ranges to reduce number of transactions. All compoents with the same address will be updated in one request. skip_updates applies for all components in the same range. -- **register_count** (*Optional*): only required for uncommon response encodings or to :ref:`optimize modbus communications` - The number of registers this data point spans. Overrides the defaults determined by ``value_type``. - If no value for ``register_count`` is provided, it is calculated based on the register type. - - The default size for 1 register is 16 bits (1 Word). Some devices are not adhering to this convention and have registers larger than 16 bits. In this case ``register_count`` and ``response_size`` must be set. For example, if your modbus device uses 1 registers for a FP32 value instead the default of two set ``register_count: 1`` and ``response_size: 4``. +- **bitmask** (*Optional*, int): sometimes multiple values are packed in a single register's response. The bitmask can be used to extract a value from the response. See :ref:`bitmasks`. +- **skip_updates** (*Optional*, int): By default, all sensors of a modbus_controller are updated together. For data points that don't change very frequently, updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle. Note: The modbus_controller groups components by address ranges to reduce number of transactions. All components with the same starting address will be updated in one request. ``skip_updates`` applies for *all* components in the same range. +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. - **response_size** (*Optional*, int): Size of the response for the register in bytes. Defaults to register_count*2. - **force_new_range** (*Optional*, boolean): If possible sensors with sequential addresses are grouped together and requested in one range. Setting ``force_new_range: true`` enforces the start of a new range at that address. -- **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. - custom data must contain all required bytes including the modbus device address. The crc is automatically calculated and appended to the command. - See :ref:`modbus_custom_command` how to use ``custom_command`` - **lambda** (*Optional*, :ref:`lambda `): Lambda to be evaluated every update interval to get the new value of the sensor. @@ -64,23 +56,26 @@ Configuration variables: - ``return ;`` the new value for the sensor. - ``return NAN;`` if the state should be considered invalid to indicate an error (advanced). -- **offset** (*Optional*, int): only required for uncommon response encodings - offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to start address. The component calculates the size of the range based on offset and size of the value type - For coil or discrete input registers offset is the position of the coil/register because these registers encode 8 coils in one byte. +- **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. + Custom data must contain all required bytes including the modbus device address. The CRC is automatically calculated and appended to the command. + See :ref:`modbus_custom_command` how to use ``custom_command`` +- **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 start address. The component calculates the size of the range based on offset and size of the value type. For ``coil`` or ``discrete_input`` registers offset is the position of the coil/register because these registers encode 8 coils in one byte. - All other options from :ref:`Sensor `. +Examples +-------- -This example will send 2 modbus commands (device address 1 assumed) +The example below will send 2 modbus commands (device address 1 assumed): -0x1 0x4 0x31 0x0 0x0 0x02 x7f 0x37 ( read 2 registers starting at 0x3100) +``0x1 0x4 0x31 0x0 0x0 0x02 x7f 0x37`` (read 2 registers starting at 0x3100) -0x1 0x3 0x90 0x1 0x0 0x1 0xf8 0xca ( read 1 holding resister from 0x9001 ) +``0x1 0x3 0x90 0x1 0x0 0x1 0xf8 0xca`` (read 1 holding resister from 0x9001) .. code-block:: yaml - platform: modbus_controller - modbus_controller_id: traceran + modbus_controller_id: modbus1 id: pv_input_voltage name: "PV array input voltage" address: 0x3100 @@ -92,19 +87,7 @@ This example will send 2 modbus commands (device address 1 assumed) - multiply: 0.01 - platform: modbus_controller - modbus_controller_id: traceran - id: pv_input_current - name: "PV array input current" - address: 0x3101 - unit_of_measurement: "A" ## for any other unit the value is returned in minutes - register_type: read - value_type: U_WORD - accuracy_decimals: 2 - filters: - - multiply: 0.01 - - - platform: modbus_controller - modbus_controller_id: traceran + modbus_controller_id: modbus1 name: "Battery Capacity" id: battery_capacity register_type: holding @@ -116,20 +99,20 @@ This example will send 2 modbus commands (device address 1 assumed) The ``modbus`` sensor platform allows you use a lambda that gets called before data is published using :ref:`lambdas `. -This example logs the value as parsed and the raw modbus bytes received for this register range +The example below logs the value as parsed and the raw modbus bytes received for this register range: .. code-block:: yaml # Example configuration entry sensor: - platform: modbus_controller - modbus_controller_id: epever - id: battery_capacity - address: 0x9001 - name: "Battery Capacity" - register_type: holding - value_type: U_WORD - lambda: |- + modbus_controller_id: modbus1 + id: battery_capacity + address: 0x9001 + name: "Battery Capacity" + register_type: holding + value_type: U_WORD + lambda: |- ESP_LOGI("","Lambda incoming value=%f - data array size is %d",x,data.size()); ESP_LOGI("","Sensor properties: adress = 0x%X, offset = 0x%X value type=%d",item->start_address,item->offset,item->sensor_value_type); int i=0 ; @@ -139,137 +122,15 @@ This example logs the value as parsed and the raw modbus bytes received for this } return x ; -.. _modbus_custom_command: - -Using ``custom_command`` ------------------------- - -``custom_command`` can be used to create an arbitrary modbus command. Combined with a lambda any response can be handled. -This example re-implements the command to read the registers 0x156 (Total active energy) and 0x158 Total (reactive energy) from a SDM-120. -SDM-120 returns the values as floats using 32 bits in 2 registers. - - .. code-block:: yaml - - modbus: - send_wait_time: 200ms - uart_id: mod_uart - id: mod_bus - - modbus_controller: - - id: sdm - address: 2 - modbus_id: mod_bus - command_throttle: 100ms - setup_priority: -10 - update_interval: 30s - sensors: - - platform: modbus_controller - modbus_controller_id: sdm - name: "Total active energy" - id: total_energy - # address: 0x156 - # register_type: "read" - ## reimplement using custom_command - # 0x2 : modbus device address - # 0x4 : modbus function code - # 0x1 : high byte of modbus register address - # 0x56: low byte of modbus register address - # 0x00: high byte of total number of registers requested - # 0x02: low byte of total number of registers requested - custom_command: [ 0x2, 0x4, 0x1, 0x56,0x00, 0x02] - value_type: FP32 - unit_of_measurement: kWh - accuracy_decimals: 1 - - - platform: modbus_controller - modbus_controller_id: sdm - name: "Total reactive energy" - # address: 0x158 - # register_type: "read" - custom_command: [0x2, 0x4, 0x1, 0x58, 0x00, 0x02] - ## the command returns an float value using 4 bytes - lambda: |- - ESP_LOGD("Modbus Sensor Lambda","Got new data" ); - union { - float float_value; - uint32_t raw; - } raw_to_float; - if (data.size() < 4 ) { - ESP_LOGE("Modbus Sensor Lambda", "invalid data size %d",data.size()); - return NAN; - } - raw_to_float.raw = data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3]; - ESP_LOGD("Modbus Sensor Lambda", "FP32 = 0x%08X => %f", raw_to_float.raw, raw_to_float.float_value); - return raw_to_float.float_value; - unit_of_measurement: kVArh - accuracy_decimals: 1 - -.. _modbus_register_count: - -.. note:: **Optimize modbus communications** - - ``register_count`` can also be used to skip a register in consecutive range. - - An example is a SDM meter: - - .. code-block:: yaml - - - platform: modbus_controller - name: "Voltage Phase 1" - address: 0 - register_type: "read" - value_type: FP32 - - - platform: modbus_controller - name: "Voltage Phase 2" - address: 2 - register_type: "read" - value_type: FP32 - - - platform: modbus_controller - name: "Voltage Phase 3" - address: 4 - register_type: "read" - value_type: FP32 - - - platform: modbus_controller - name: "Current Phase 1" - address: 6 - register_type: "read" - value_type: FP32 - accuracy_decimals: 1 - - Maybe you don’t care about the Voltage value for Phase 2 and Phase 3 (or you have a SDM-120). - Of course, you can delete the sensors your don’t care about. But then you have a gap in the addresses. The configuration above will generate one modbus command `read multiple registers from 0 to 6`. If you remove the registers at address 2 and 4 then 2 commands will be generated `read register 0` and `read register 6`. - To avoid the generation of multiple commands and reduce the amount of uart communication ``register_count`` can be used to fill the gaps - - .. code-block:: yaml - - - platform: modbus_controller - name: "Voltage Phase 1" - address: 0 - unit_of_measurement: "V" - register_type: "read" - value_type: FP32 - register_count: 6 - - - platform: modbus_controller - name: "Current Phase 1" - address: 6 - register_type: "read" - value_type: FP32 - - Because `register_count: 6` is used for the first register the command “read registers from 0 to 6” can still be used but the values in between are ignored. - **Calculation:** FP32 is a 32 bit value and uses 2 registers. Therefore, to skip the 2 FP32 registers the size of these 2 registers must be added to the default size for the first register. - So we have 2 for address 0, 2 for address 2 and 2 for address 4 then ``register_count`` must be 6. - - See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` -- :doc:`/components/number/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` -- :doc:`/components/text_sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` - :doc:`/components/switch/modbus_controller` +- :doc:`/components/number/modbus_controller` +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` - `EPEVER MPPT Solar Charge Controller (Tracer-AN Series) `__ - :ghedit:`Edit` diff --git a/components/switch/modbus_controller.rst b/components/switch/modbus_controller.rst index 2b5b2541a..5114fd47e 100644 --- a/components/switch/modbus_controller.rst +++ b/components/switch/modbus_controller.rst @@ -13,21 +13,18 @@ Configuration variables: - **id** (*Optional*, :ref:`config-id`): Manually specify the ID used for code generation. - **name** (**Required**, string): The name of the sensor. -- **restore_mode** (*Optional*): See :ref:`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. - **register_type** (**Required**): type of the modbus register. -- **address** (**Required**, int): start address of the first register in a range -- **offset** (*Optional*, int): not required in most cases - offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to 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 the registers 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 -- **bitmask** (*Optional*, int): Some values are packed in a response. The bitmask is used to determined if the result is true or false. -- **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle -- **use_write_multiple** (*Optional*, boolean): By default the modbus command ``Force Single Coil`` (function code 5) is used to send state changes to the device. If your device only supports ``Force Multiple Coils`` (function code 15) set this option to true. -- **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. - custom data must contain all required bytes including the modbus device address. The crc is automatically calculated and appended to the command. - See :ref:`modbus_custom_command` how to use ``custom_command`` + + - ``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. + - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read. Modbus *Function Code 4 (Read Input Registers)* will be used. + +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). +- **skip_updates** (*Optional*, int): By default, all sensors of a modbus_controller are updated together. For data points that don't change very frequently, updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle. Note: The modbus_controller groups components by address ranges to reduce number of transactions. All components with the same starting address will be updated in one request. ``skip_updates`` applies for *all* components in the same range. +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. +- **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 :ref:`bitmasks`. - **lambda** (*Optional*, :ref:`lambda `): Lambda to be evaluated every update interval to read the status of the switch. - **write_lambda** (*Optional*, :ref:`lambda `): Lambda called before send. @@ -46,7 +43,29 @@ Configuration variables: - ``return ; and fill payload with data`` if the payload is added from the lambda then these bytes will be sent. - ``return {};`` in the case the lambda handles the sending of the value itself. -**Example** +- **custom_command** (*Optional*, list of bytes): raw bytes for modbus command. This allows using non-standard commands. If ``custom_command`` is used ``address`` and ``register_type`` can't be used. + Custom data must contain all required bytes including the modbus device address. The CRC is automatically calculated and appended to the command. + See :ref:`modbus_custom_command` how to use ``custom_command`` +- **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 :ref:`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. + + +Examples: +--------- + +.. code-block:: yaml + + switch: + - platform: modbus_controller + modbus_controller_id: epever + id: enable_load_test + register_type: coil + address: 2 + name: "enable load test mode" + bitmask: 1 + .. code-block:: yaml @@ -69,26 +88,12 @@ Configuration variables: return true; - -**Example** - -.. code-block:: yaml - - switch: - - platform: modbus_controller - modbus_controller_id: epever - id: enable_load_test - register_type: coil - address: 2 - name: "enable load test mode" - bitmask: 1 - 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 address 5 (start_address+offset) value 1 or 0 +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. 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`` +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`` .. code-block:: yaml @@ -105,9 +110,13 @@ For holding registers the write command will be write_single_register. Because t See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` +- :doc:`/components/number/modbus_controller` +- :doc:`/components/select/modbus_controller` - :doc:`/components/text_sensor/modbus_controller` - https://www.modbustools.com/modbus.html - :ghedit:`Edit` diff --git a/components/text_sensor/modbus_controller.rst b/components/text_sensor/modbus_controller.rst index 10a49b574..49bc6c321 100644 --- a/components/text_sensor/modbus_controller.rst +++ b/components/text_sensor/modbus_controller.rst @@ -14,16 +14,16 @@ Configuration variables: - **name** (**Required**, string): The name of the sensor. - **register_type** (**Required**): type of the modbus register. - - ``coil``: coils are also called discrete outout. Coils are 1-bit registers (on/off values) that are used to control discrete outputs. Read and Write access - - ``discrete_input``: discrete input register (read only coil) are similar to coils but can only be read. - - ``holding``: Holding Registers - Holding registers are the most universal 16-bit register. Read and Write access - - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read + - ``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. + - ``read``: Read Input Registers - registers are 16-bit registers used for input, and may only be read. Modbus *Function Code 4 (Read Input Registers)* will be used. -- **address** (**Required**, int): start address of the first register in a range +- **address** (**Required**, int): start address of the first register in a range (can be decimal or hexadecimal). - **skip_updates** (*Optional*, int): By default all sensors of a modbus_controller are updated together. For data points that don't change very frequently updates can be skipped. A value of 5 would only update this sensor range in every 5th update cycle -- **register_count** (*Optional*): The number of registers this data point spans. Default is 1 -- **response_size** (**Required**): Number of bytes of the response -- **raw_encode** (*Optional*, enum): If the response is binary it can't be published directly. Since a text sensor only publishes strings the binary data can be encoded +- **register_count** (*Optional*, int): The number of consecutive registers this read request should span or skip in a single command. Default is 1. See :ref:`modbus_register_count` for more details. +- **response_size** (**Required**): Number of bytes of the response. +- **raw_encode** (*Optional*, enum): If the response is binary it can't be published directly. Since a text sensor only publishes strings the binary data can be encoded: - ``NONE``: Don't encode data. - ``HEXBYTES``: 2 byte hex string. 0x2011 will be sent as "2011". @@ -48,12 +48,11 @@ Configuration variables: - ``return ;`` the new value for the sensor. - ``return {};`` uses the parsed value for the state (same as ``return x;``). -- **offset** (*Optional*, int): not required in most cases - offset from start address in bytes. If more than one register is read a modbus read registers command this value is used to find the start of this datapoint relative to start address. The component calculates the size of the range based on offset and size of the value type +- **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 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. - All options from :ref:`Text Sensor `. -**Example** - +Example: +-------- .. code-block:: yaml @@ -79,9 +78,14 @@ Configuration variables: See Also -------- +- :doc:`/components/modbus` - :doc:`/components/modbus_controller` - :doc:`/components/sensor/modbus_controller` - :doc:`/components/binary_sensor/modbus_controller` +- :doc:`/components/output/modbus_controller` - :doc:`/components/switch/modbus_controller` +- :doc:`/components/number/modbus_controller` +- :doc:`/components/select/modbus_controller` +- :doc:`/components/text_sensor/modbus_controller` - https://www.modbustools.com/modbus.html - :ghedit:`Edit`