esphome / issues

Issue Tracker for ESPHome
https://esphome.io/
290 stars 34 forks source link

Modbus controller text sensor: response_size has no effect #5468

Closed wowtor closed 2 months ago

wowtor commented 7 months ago

The problem

Modbus controller text sensor has a response_size attribute, but it seems to be unused. With response_size set to 2 and raw_encode to HEXBYTES, I expect a four character string, but I receive 8 characters, such as "00000000". If appreciated, I don't mind to create a pull request to fix this; see proposed change below.

Which version of ESPHome has the issue?

2023.12.9

What type of installation are you using?

Home Assistant Add-on

Which version of Home Assistant has the issue?

2024.1.6

What platform are you using?

ESP32

Board

esp-wrover-kit

Component causing the issue

modbus controller

Example YAML snippet

text_sensor:
  - platform: modbus_controller
    modbus_controller_id: "omnia_modbus"
    name: "Omnia current HVAC mode"
    id: "omnia_current_hvac_mode"
    register_type: holding
    register_count: 1
    response_size: 2
    address: 101
    entity_category: DIAGNOSTIC
    raw_encode: HEXBYTES
    filters:
      - map:
          - 0000 -> off
          - 0002 -> cool
          - 0003 -> heat

Anything in the logs that might be useful for us?

[09:01:59][D][text_sensor:064]: 'Omnia current HVAC mode': Sending state '00000000'

Additional information

Looking at the source code, it seems that the value for response_size is never used. The relevant snippet seems to be this:

  uint8_t max_items = this->response_bytes;
  uint8_t index = this->offset;
  char buffer[4];
  while ((max_items != 0) && index < data.size()) {
    uint8_t b = data[index];
    switch (this->encode_) {
      case RawEncoding::HEXBYTES:
        sprintf(buffer, "%02x", b);
        output << buffer;
        break;
      case RawEncoding::COMMA:
        sprintf(buffer, index != this->offset ? ",%d" : "%d", b);
        output << buffer;
        break;
      // Anything else no encoding
      case RawEncoding::NONE:
      default:
        output << (char) b;
        break;
    }

    index++;
  }

I expect the value max_items to be decreased, but in fact it never changes. If this is correct, I don't mind to create a pull request, to add a max_items--; at the end of the loop.

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

wowtor commented 2 months ago

How can I re-open this issue?