I’m experiencing the same problem. After some debugging I’ve noticed that the service modbus.write_register sends a modbus function code 16 (presetting multiple registers). This would be desirable if an array of values is passed, but it also does this if you only pass a single integer as value.
The modbus function code 16 is often not supported by the modbus slave, and for writing a single value function code 6 would be a better option, and this is supported by most modbus slaves.
Anyone who know how to force the service modbus.write_register to use modbus function code 6 for writing a single value? Or is this a bug in the modbus component?
Not yet at the moment, I’ve drilled down in the modbus.py code and found a where it goes wrong (that way always an array is passed to pymodbus which results in FC 16).
I did a pull request (https://github.com/home-assistant/home-assistant/pull/18425) but I had to do a PR for the docs and also alter services.yaml. Had no time to look into it, but I might resume it in the future. I’m now running my own custom modbus.py (based on my change in the PR) which works perfectly.
now my line 63 reads: vol.Required(ATTR_VALUE): vol.Any(cv.positive_int)
was: vol.Required(ATTR_VALUE): vol.All(cv.ensure_list, [cv.positive_int])
I can also confirm that this also works if you have followed the workaround for having more than one modbus device/hub (IE: modbus1.py and modbus2.py etc). see here Ability to add multiple modbus hubs
I started with my most urgent device (using modbus2.py) to make sure this was going to work and it does so I assume all other mods of the modbus.py will behave the same. I will report back if this is not so.
You just need to remember which register you are writing to belongs to what modbus.py.
Thanks again to the whole community here and I would encourage developers to recognise that both these issues (IE: writing to registers and multiple modbus hubs) should be included into the official version at some point in the near future and that the documentation also needs to be updated.
I’ve found some spare time and retried a PR for this issue. Hopefully it gets accepted now and is merged in the new build. In the meantime I have also been running the modbus component as a custom component and it works perfect. It is indeed a wrong implementation of pymodbus, which is already for a very long time residing in home assistant.
Great that you have been able to get onto this gertdb!
For my own (and others) clarity could you confirm…there are two extra lines in modbus.py (a total of three have been edited) due to the line length restrictions? Doing it that way will allow both options of a single register or multiple registers right?
Also, does the services.yaml (@ homeassistant/components/modbus/services.yaml) require this edit to allow the two options to work? And if so will that also work for multiple modbus hubs do you think?
The multiple hubs solution (here Ability to add multiple modbus hubs which works seamlessly for me needs to be looked at and a better solution devised in my humble opinion.
Why?
Imagine if you had to have multiple MQTT Hubs for every new device?
I’m not a developer so I wouldn’t know if it’s possible but surely there is a way to code this to allow for multiple modbus devices within the one modbus.py system? Is there anyone working on that do you know?
Cheers for your great work again!
Regards Steve Wells
The single values and arrays have been officially supported for a long time, but not working because of the coding bug in the voluptuous data validation. I hope my PR gets merged, because then this is also finally resolved.
The reason that the change consists now of multiple lines of code is indeed because of the line length restrictions.
The edit in services.yaml is not necessary for the change to work, it only provides a description of the possibilities of the service. (for example shonw on the /dev-service page in Home Assistant).
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.
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.
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!
I know that topic is a bit old
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.
@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.
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:
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.
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.