Modbus issues

I agree with @wellsy, maybe must be tested a little more, but should be consider it as core function.I read many post related this issue. Maybe modbus is not so common in home automation but there are few improvements that is possible to add in order to improve compatibility with modbus devices

1 Like

Good on you @Bard It would be more common if the modbus integration was more complete…there are potentially thousands of modbus devices out there with things that just dont quite fit atm I reckon.

Try the following patch generated with git-diff

It needs testing with a wider range of devices before it can be considered for a PR. Even then, it may not make it as it doesn’t use a public (modbus library) function to alter the framer delay.

diff --git a/homeassistant/components/modbus/sensor.py b/homeassistant/components/modbus/sensor.py
index 10e11a9..078f1f1 100644
--- a/homeassistant/components/modbus/sensor.py
+++ b/homeassistant/components/modbus/sensor.py
@@ -23,6 +23,7 @@ CONF_REGISTER_TYPE = 'register_type'
 CONF_REGISTERS = 'registers'
 CONF_REVERSE_ORDER = 'reverse_order'
 CONF_SCALE = 'scale'
+CONF_FRAMER_DELAY = "framer_delay"
 
 DATA_TYPE_CUSTOM = 'custom'
 DATA_TYPE_FLOAT = 'float'
@@ -52,6 +53,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
         vol.Optional(CONF_SLAVE): cv.positive_int,
         vol.Optional(CONF_STRUCTURE): cv.string,
         vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
+        vol.Optional(CONF_FRAMER_DELAY, default=0): vol.All(
+            vol.Coerce(float), vol.Range(min=0, max=0.75))
     }]
 })
 
@@ -98,7 +101,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
             register.get(CONF_UNIT_OF_MEASUREMENT), register.get(CONF_COUNT),
             register.get(CONF_REVERSE_ORDER), register.get(CONF_SCALE),
             register.get(CONF_OFFSET), structure,
-            register.get(CONF_PRECISION)))
+            register.get(CONF_PRECISION), register.get(CONF_FRAMER_DELAY)))
 
     if not sensors:
         return False
@@ -110,7 +113,7 @@ class ModbusRegisterSensor(RestoreEntity):
 
     def __init__(self, hub, name, slave, register, register_type,
                  unit_of_measurement, count, reverse_order, scale, offset,
-                 structure, precision):
+                 structure, precision, delay):
         """Initialize the modbus register sensor."""
         self._hub = hub
         self._name = name
@@ -126,6 +129,9 @@ class ModbusRegisterSensor(RestoreEntity):
         self._structure = structure
         self._value = None
 
+        if delay > self._hub._client.silent_interval:
+            self._hub._client.silent_interval = delay
+
     async def async_added_to_hass(self):
         """Handle entity which will be added."""
         state = await self.async_get_last_state()

Hi @paul_c5x4 and @wellsy due to the unhappy “covid” period, I have more time for work on my home assistant configuration.
How can I load your patch? In the last days I found a possible solution to use the Addon appdaemon that create a python “sandbox” but i don’t know if is the best solution, could you kindly post some documentation reference?
thanks
Matteo

following some post, I created a custom_components/modbus and I added the patched sensor.py and a copy of the init and switch file as shown in the picture below
image

but HA seems using the standard sensor components, do you know if I missed some step?

@Bard Look Here and read well…follow the instructions completely. The gist of what you are doing is you are creating a full ‘custom component’ replacement for the ‘standard’ modbus component.

OK, just got to test this as well. It works great. I can finally delete my second instance of Hass and run all in a single container. Thanks @PtP.

Here is the corrected custom_component modbus1.py:

Base your changes on existing current modbus code using @paul_c5x4 mods.

Understood? NOTE: The component is called custom_component modbus1.py

PS: Stay safe in these troubled times!

Hi @wellsy,
I’ll read today, in order to recap: I don’t need to keep the same name of the standard component, I can use a different one.
Then for use it is enough call in my config.yaml or I need to add some declaration?

Yep you got it…It MUST be a different name…it becomes a total replacement for the standard modbus integration.That means you will need to setup ALL your devices to be using that by reworking your config…fair bit of work but if done correctly it works perfectly (well it did for me in the case of the multiple hub modification).
Good luck and let us know how you go with it.

I read the post, my HA is running with hassio and I cannot figure out where get the file:

/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/modbus.py

Sorry @Bard but that is not a platform I have that sort of experience with…try searching the forum for that advice. Back when I was tinkering I messed with hassio…moved on to HA (core now?) for a few reasons.

Maybe this one could be a reason for install it in a different way :sweat_smile:, I’ll try to search a solution for an hassio config :+1:

If this is an urgent/important need then you may need to consider moving to ‘core’ as it is far more easily customizable. With a learning curve as well though.

well is not urgent, since the system is working with this issue from a long time, but my idea is to try to resolve it. Good thing of hassio that is simple to keep updated and install addon

Stick with that then…keep an eye on any posts by @paul_c5x4 for any possibility this may become a part of HA modbus. Good luck!

Hi Wellsy, I migrated my solution on HA core, I checked the procedure, but today the layout of the files is different, There is a modbus folder without any modbus.py do you know how it changed the filosofy?

Bugger…sorry since multiple hubs were introduced I have not needed to poke around so thats news to me as well. Have you looked at the modbus github repo? https://github.com/home-assistant/core/tree/dev/homeassistant/components/modbus Looks like the files I am used to seeing there mate?

modbus.py = init.py

Ok I’ll try to check again the procedure after work

1 Like

Ok I spent few days on it without a good result,
I found a different solution:
I moved all the modbus code in the node-red addon and then I transfer data using MQTT, the good thing is that with only one read, I can get all required register and then I unpacked them with a specific function

@Bard I only just noticed that you had posted this reply…good to see you have a solution you are happy with. Do you normally run Node-Red OR just for this solution?

I have used it in the past but in the interests of overall simplicity I backed away from using it.