Modbus Integer Write Out

Oh by the way…writing to registers over 4 modbus hubs now…life’s good!

Thanks for sorting this out for the community.

I know that topic is a bit old :slight_smile:
Anyhow. Is here any modbus experienced able to advice how to write in a single bit of register?
I know that I can use a switch, nonetheless I do not know how address a specific bit.

I am reading from:

  - platform: modbus
    registers:
    - name: relays mb #  MBF_MEASURE_RELAYS_STATE
      hub: pool
      slave: 1
      register: 258
      register_type: holding
      data_type: uint

There are 5 bits for 5 relays state (bit 1,2,3,4,5) and I need to be able to change bit 3 from 0 to 1

Any advice?

@kajmaj I have not had the need to do this myself however I am pretty sure there have been posts about this type of thing so maybe you need to do some specific searching for your answer.

From memory of one discussion I believe you just have to construct an ‘array’

There is documentation here which includes this below:

Service Description
write_register Write register. Requires hub, unit, address and value fields. value can be either single value or an array

I hope that helps a little and I will continue to search myself a little to see what I can find. Try searching “write_register bit” OR “write_register array”

Perhaps if you find the answer please post back here to add your solution? I will as well.

UPDATE: Discussion and a solution here: SOLVED:How to add sensor state in an array inside an automation and send it with modbus.write_register service

NOTE: The automation is how you write the value to your register.

@wellsy Great thank you :+1:
If I understood well.
Register of my interest
RELAYS_STATE
If I needed to change bit 4 from 0 to 1 I will use following (I have slave 1, but using MODBUS TCP it should be 255)

  action:
    service: modbus.write_register
    data:
      hub: pool
      unit: 255
      address: 270
    data_template:
      value: [10,1]

If I need to change bit 6 from 1 to 0:

    data_template:
      value: [40,0]

At the end I need to change bit 2 and 6 back to original state. How to organize an array in this case?

Or it should be in a different way?

One thing is you may need to fill in all the array but I am not certain? As I understand these things there are 6 bits in the array?

So like:
value: [0,0,0.0,0,0]

You then set the fourth bit:
value: [0,0,0.40,0,0]

The key to understanding what will be sent is to test your data template in the yourIP:8123/developer-tools/template editor. Read down to the bottom of that link I posted above and you will see how all involved in that thread worked out how to get it right for a number of different requirements.

You look like you just want to write specific values right?
If you want to rewrite those values then just construct a second automation may work for your purposes?

You can also setup an input_number to provide the values and trigger the automation on state change of the input_number if that works for you?

Here is an example automation using an input_number…disregard if of no use:

- id: IrrigationPoolSetpoint
  alias: Irrigation Pool Setpoint
  trigger:
    platform: state
    entity_id: input_number.slider4
  action:
    service: modbus.write_register
    data_template:
      hub: hub3
      unit: 3
      address: 3080
      value: "{{ states.input_number.slider4.state|int }}"

Remember you just need to modify to include your array within the value template.

There are 7 bits in array. All bits are under address 270
In the chart they are using 0 bit but in relity it is a first bit.
Now I understand an array writes. It is a big progress. :slight_smile:
I’ll try to explain what I am interested in:
Technically each bit is controlling one relay in the swimming pool controller.
And I need to control each bit separately, leaving rest of them as they were before.
For exmple bit 4 is light, bit 6 filtration, 3 heating etc. So I would like to be able to control each system separately, without influence to remaining.
Respectivelly even more bits in one shot but rest bits shall remain as they are.
To make it easier, each bit has value 1 or 0.
One time for example switch light, ie. bit 4 to one while 6 and 4 are altready 1, next time change bit 3 to 0, while 6 is 1 and 3 is 0.
As I understood, hassio modbus is supporting both single bit and multiple bits write.
My question is how to address let say bit 4 and write value 1 in it and leave rest of bits untouched?

Okie…this is where I am at the limit of my knowledge sorry. Not up on how it all works as I said thats just how ‘I’ understood it and that could be very wrong.

There is also this in the documentation: See the “value” of an Array

Service Data Attributes

Attribute Description
hub Hub name (defaults to ‘default’ when omitted)
unit Slave address (set to 255 you talk to Modbus via TCP)
address Address of the Register (e.g., 138)
value A single value or an array of 16-bit values. Single value will call modbus function code 6. Array will call modbus function code 16. Array might need reverse ordering. E.g., to set 0x0004 you might need to set [4,0]

Glad to have helped a little but my understanding of 16-bit values is limited.

You are a far better to me.
Now we are approaching the edge. :slight_smile:

[4,0] means fourth bit, value set to 0?
If in array, for bit 4 to value 1 and bit 6 to value 0 it should be [4,1,6,0] or it simply must be two separated services for each of bits?

Edited: Otherwise - I need to specify bit and its value

Yes I agree after reading more carefully after I posted that bit from the docs.

It seems that is how you address each bit BUT then it also talks about you may need to reverse order…Yeah right!

Thanks for precise help. :+1:
I will test it ASAP.
Reverse order is for array I believe.

1 Like

@kajmaj glad I could at least point you in the right direction but I feel it was only a nudge. Post again when you have your full solution or if you get stuck along the way.

@wellsy
Tested.
address one specific bit still remains a big question. As and address of the bit is used number of bit (i.e. 0,1,2,3,4,5,6) or mask (in my case 1, 2, 4, 8, 10, 20, 40)
If I continue with 4th bit, non of [4,1] or [1,4] work while tried to write 1 to this 4th bit.
Array seems to be working somehow, at least when I needed to clear the whole register. [0,0,0,0,0,0,0] worked but for example but [1,0,0,0,0,0,0] or [0,0,0,0,0,0,1] not (resulted value of the whole register should be 64)
I must do sthg wrong but what?

@kajmaj I can only refer back again to what was discussed and was worked out in the example I posted initially. @Bard do you mind assisting in this post?

For sending an array of static values worked by using the below syntax

Now I am curious to know what the actual values of your register bits are at present?

You said:

What then happens if you send [0,0,0,0,1,0,0]

It seems that would/may fulfiill what you want to set ie: bit 4=1 and bit 6=0

@wellsy Regarding the array write there is a 3 steps process in my MODBUS device, where at at least 2 steps require write to a single bit od different registers.
If written to improper bit the device could be bricked.
I do not want to risk it before I know how to write single bit.
All my related registers are 8 bits.
one is currently: < 0100000> (decimal 32) and could be of any combination of 0 and 1 (well maybe 6 or 7 combinations). Confirmation write must be performed within 500msec, otherwise it reverts back without any change.
one of ‘confirmation’ registers is currently <1010010> and I need to change 3rd bit from the left from 0 to 1. If I do it in another bit, device could be bricked.
I have no problem with automation to do it but I am still not sure, how to hit the right bit :slight_smile:

What I know, each bit can contain 1 or 0 only. Based on it how to interpret [1,2,2] or [“1”,“2”,“2”] ? How to distinguish particular bit and its value?

Maybe my questins are silly, but rather silly up front than sorry afterwards with bricked device as the result.

I did a few more tests regarding addressing :slight_smile:
In my case:
bit address is 1,2,4,8,16,32,64 for bits 0,1,2,3,4,5,6
Value of bit is not necessary, it simply change the state.
Array of 2 or more bits (i.e. [32,16,1] ) does not work, only first bit address from left changes state(value)
So small progress, must verify array issue now

@kajmaj Do you have access to a manual for your device? Can you post a link?
Also how are you reading the values presently?

@wellsy Huh, it is a quite complex question.
As I mentioned here 21 in this topic the device is swimming pool control device Hidrolife
It can work off line or on-line using manufacturer cloud via wifi module.
Wifi module communicates with the device via MODBUS. There is 1 spare MODBUS connector available in the device
My intention is to fully cut off cloud. Simply want to have it under the control. :slight_smile:
So far I use for settings cloud (that is why I am interested in modbus write) and reading data via MODBUS TCP converter.
User manual for Hidrolife is useless regarding MODBUS.
There is available MODBUS registers description here but there are missing sequencies how to write in it.
There is a blog dealing with the integration of Hidrolife to Loxone. Fom them I am trying to gain last drops of info on write sequencies and confirmation bits combination.
That is why I know about a risk of bricked device if wrong bit is changed and trying to avoid risky experiments.
So far - MODBUS reading - fine, single bit addressing - I hope that I know (with your help :+1:), array - cannot confirm.

Ok I looked at the Modbus-registers.pdf and what I would ask of you now is a list of the registers you want to be able to read from or write to please? Interesting how it randomly jumps from english to spanish…challenging…lol!

At the moment reading from (HEX) 0x0102, 0x0106, 0x0107, 0x0411, 0x0413, 0x0416, 0x0421, 0x0504, 0x0505 and 0x010E
Need to write to 0x0411, 0x0413, 0x010E and 0x02F0 (final confirmation).There are 2 more confirmation bits somewhere in registers I do not know yet. Hopefully will know soon.
Register 0x010E is one of registers where is neccessary to write either to particular bit or in array. Two more are those “unknown” yet. Rest of registers to write it is 1 bit at a time.
Any write has to be confirmed in two more registers (sequence within defined time)

The pH level: That is read only I imagine so you just set that up as a sensor.

The water temperature sensor: That is read only I imagine so you just set that up as a sensor.

The pH control module status: That is read only I imagine so you just set that up as a sensor.

The filtering mode of the equipment: That is read only I imagine so you just set that up as a sensor.

Filtering status in manual mode (on = 1; off = 0): Read only I imagine and you should be able to read it as a binary sensor.

Heating set point temperature: So you should just be able to write the value you want to set. And you should be able to read it as a sensor.

Filtration function state. This register is read only and exposes the current state of
the filtration procedure: 0 is off and 1 is on and should just be setup as a binary sensor.

This register contains the higher limit of the pH regulation system: So you should just be able to write the value you want to set. And you should be able to read it as a sensor.
The value set in this register is multiplied by 100. This means that if we want to set
a value of 7.5, the numerical content that we must write in this register is 750.
This register must be always higher than MBF_PAR_PH2.
 To make the modification of this register persistent, execute the EEPROM
storage procedure described in global register MBF_SAVE_TO_EEPROM.

NOTE: “The value set in this register is multiplied by 100. This means that if we want to set a value of 7.5, the numerical content that we must write in this register is 750” I think there may be a typo here: So register is multiplied by 100 may be register is divided by 100? But that needs to be clarified?

This register contains the lower limit of the pH regulation system: So you should just be able to write the value you want to set. And you should be able to read it as a sensor.
The value set in this register is multiplied by 100. This means that if we want to set
a value of 7.0, the numerical content that we must write in this register is 700.
This register must be always lower than MBF_PAR_PH1.
 To make the modification of this register persistent, execute the EEPROM
storage procedure described in global register MBF_SAVE_TO_EEPROM.

NOTE: “The value set in this register is multiplied by 100. This means that if we want to set a value of 7, the numerical content that we must write in this register is 700” I think there may be a typo here: So register is multiplied by 100 may be register is divided by 100? But that needs to be clarified?

This register contains the status of each and every one of the configurable relays: This register is read only I imagine and should be setup as a sensor. Extracting the value of the individual relays in individual sensors is something I am not too sure about but we should be able to figure that out. So you should see 0000000 if all relays are ‘off’ and 1111111 if they are all ‘on’.

Lets stop there and await your reply