Read Modbus registers as HEX and combine

Some help here please from anyone in the know:

I need to read the value of two Modbus holding registers in HEX and combine them before converting the combined total to Base 10
Example:

Address 8 Bytes 1 & 0 are 1BB3
Address 9 Bytes 3 & 2 are 0002

Combine the two to get 00021BB3
Convert to Base 10 to give 138663

Issues then -

  1. How do I read them and store them as HEX? I cannot seem to find a structure format that will do that…
  2. How do I combine them to a new value that can then be read and converted?

I am extracting the data in Int format fine using this:

      - name: TOTAL_POWER_1
        unique_id: CB_8
        scan_interval: 10
        address: 8
        slave: 1
        input_type: holding
        unit_of_measurement: kWh
        device_class: power
        state_class: measurement
        data_type: uint16
        
      - name: TOTAL_POWER_2
        unique_id: CB_9
        scan_interval: 10
        address: 9
        slave: 1
        input_type: holding
        unit_of_measurement: kWh
        device_class: power
        state_class: measurement
        data_type: uint16

I have tried custom structures and none seem to read it as HEX.

Any pointers please???

      - name: TOTAL_POWER
        unique_id: CB89
        scan_interval: 10
        address: 8
        slave: 1
        count: 2
        data_type: uint32
        swap: word_byte
        input_type: holding
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing 

Thank you Nikito7

What that returns in the entity is two decimal values:

e.g. I get: 8935,2

Hex behind it is 22E7 0002
That needs to be combined in hex as a single word 000222E7
Converted to DEC this is then 140007 which is the correct reading I need to show on the entity.

- name: TOTAL_POWER
        unique_id: CB89
        scan_interval: 10
        address: 8
        slave: 1
        count: 2
        data_type: uint32
        swap: word ### this
        input_type: holding
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing

That just returns 2 in the entity

Change address to 7 or 9.

What you say above is word swap

AABB CCDD > CCDD AABB

byte swap

AABB CCDD > BBAA DDCC

word byte swap

AABB CCDD > DDCC BBAA

Ok - we are good with this:

      - name: TOTAL_POWER_1
        unique_id: CB89
        scan_interval: 10
        address: 8
        slave: 1
        input_type: holding
        unit_of_measurement: kWh
        data_type: uint32
        swap: word
        count: 2
        device_class: energy
        state_class: total_increasing

Thank you very much! :grinning:

1 Like