Modbus Integer Write Out

Hi gertdb and thanks for the full answers…appreciate that!

From what I read and in particular this below:

@balloob balloob referenced this pull request 16 days ago

Merged

0.88.0 #21238

0.88.0 is the version with multiple hubs fixed yes?
Then your PR needs to be merged as well right?

For me I will stick with what is working…incredibly well…for now as I have 0.81.6 installed on a QNAP DOCKER and it makes me very nervous upgrading especially when I am reading so many things broken with recent updates.

For me the modbus multiple hubs MUST work as I have recently ditched a flawed SCADA solution in favour of a homeassistant solution which runs some reasonably important (to me) infrastructure I have here including, water treatment, irrigation, energy management and a number of other lesser systems. I’m still completing full control and monitoring of my 5 PLC’s at this stage.
Now that I can write to registers effectively things will become easier but I haven’t even begun work on historical data which is quite important for what I’m trying to achieve.

I think I will investigate running another docker installation with the latest version to test for a while.

Thanks again for your help and feedback!

Hi Steve, yes indeed, the multiple hub solution was merged since 0.88.0. My small code change for the single/array input support will hopefully be merged in an upcoming release.

If you want some more comfort in upgrading/downgrading I can recommend to run Hass.io on a Raspberry PI. If you “invest” in a good PSU and good quality MicroSD card (e.g. Samsung EVO Pro) it’s a really reliable solution, and the snapshot feature gives you ease to test upgrades and quickly roll back when needed.

I can second that Home Assistant, even it’s still under development is already very superior to a lot of out-of-the-box (commercial) solutions.

Yes I hope your code makes it soon as well!

Regarding your suggestion to do testing on raspi. I have in the past used a good quality setup like you described above but found that the time it took for restarts of the pi and of homeassistant or hasio took a little too long for my liking…after researching I realised that a more powerful platform may be the best wat to go.

I had already realised the eliminating my defective scada system was possible with homeassistant so I have chosen a QNAP NAS after looking at a number of other options.

I will give you an update when I setup the latest version HA (unfortunately you cannot install Hassio in a docker container) after the full solution for my modbus needs is available (with your code included) and found to be stable.

Cheers again all for this wonderful community effort here!

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.