Add write-value service to modbus inegration

A pymodbus maintainer suggested I request adding a “write_value” service to the modbus integration in order to address an issue I am having.

Background:
I have a modbus temp controller from which I can read values, however, I cannot write any value unless I format the value field of write-register as (ex: value: [0x4296, 0x0000] to write the value 75). And the issue with that is there is no straightforward way to do that conversion in HA.

From the controller doc, I see it only supports the use of modbus function 0x10 to update floating point registers. The modbus integration uses 0x6 to write single values and 0x10 for multi-values so I have to specify multiple values even though I only need to write one.
The working example writes floating point number in IEEE 754 format. Since I cannot make that translation in yaml, I’m at a bit of a stop.

Works:

service: modbus.write_register
data:
  hub: modbus_hub
  address: 0
  value: [value: [0x4296, 0x0000]
  unit: 1 

Doesn’t work:

service: modbus.write_register
data:
  hub: modbus_hub
  address: 0
  value: 75
  unit: 1 

Write_register uses 0x10 when you call it with a scalar (int etc), writing a single register, it uses 0x06 when you call it with an array and writes multiple consecutive registers (one for each value in the array).

A register in the modbus definition defined with a fixed size: 16 bits.

So a 32 bit float register do not exist, you actually want to write 2 registers which represent 1 value (your float).

Write_register works as defined by the modbus standard. A new method write_value could have a more relaxed look, and automatically split the value into the needed registers (e.g. float in 2 registers, a string typically needs 4 or more registers).

I am not the creator of pymodbus, but an active maintainer…and it so happens that I also maintain the modbus integration.

Please see this as a correction to the above. I have no opinion on whether or not to add a write_value method.

1 Like

The modbus integration docs say this:

(write_register) A single value or an array of 16-bit values. Single value will call modbus function code 0x06. Array will call modbus function code 0x10.

This appears contrary to what you are saying. If it is in error, I’ll be glad to do a PR to fix the wording.

What is confusing me is I don’t even want to write a float. I’m OK with an int, but I cannot make that work.

BTW, fixed my reference to “creator”.