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: