esphome / esphome/issues

Modbus controller text sensor: response_size has no effect

Open
#5,970 1 comment 0 reactions 0 assignees View on GitHub
stale
Dominant language
No language data
Stars
313
Forks
40
PR merge metrics
No merged PRs in 30d

Description

### 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

```yaml
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?

```txt
[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:
```cpp
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.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.