Closed vanackej closed 2 months ago
In Home assistant, the switch state appears as OFF, but the OFF command have never been actually sent.
I am able to reproduce this behavior with my 24ch modbus relay card.
@martgras @ssieb
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Keep open, the problem is confirmed and has no fix yet
I don't see why this is the default behavior, if there are multiple commands to the same node, it should just issue, eventually log, but it should not just discard the command.
@martgras Do you think that adding a "Allow Duplicate Cmd" to the ModbusController would be an option?
I can't have these commands discarded.
Ps.: I'm able to implement an PR for this.
I think something like allow_duplicate_commands
would be useful. There are indeed such cases.
The problem
When quickly sending 2 consecutive commands to a modbus device (in my situation, an 8ch modbus relay, but the issue is generic I think), the modbus_controller first looks in the command queue if another command is already queued for this device. If any existing command is found, it's payload is updated with the payload of the new command, and the new command is ignored (here in source code)
If the first command has not yet been sent, this can be ok. But when it happens when the first command has already been sent, the payload of the second command replaces the one from the first command, but is never sent. This happens because the last sent command stays on the command_queue while waiting for a response. For exemple, the first ON command is sent, but the OFF command is ignored. In Home assistant, the switch state appears as OFF, but the OFF command have never been actually sent.
Potential fix : always remove the current command from the command_queue when sending the command, and store it in another variable for response processing. On send error, the command could be placed on the command_queue again. Doing so, we won't discard the second OFF command when the ON command has been sent and is stilll waiting for a reply.
A side effect of this fix would be that it would allow other commands to be executed in between attempts to send the same command again in case of a failure. This could maybe be good in case of a failed device that would slow down every other command to other devices., don't you think ?
Which version of ESPHome has the issue?
2022.11.4
What type of installation are you using?
Home Assistant Add-on
Which version of Home Assistant has the issue?
2022.11.5
What platform are you using?
ESP32
Board
No response
Component causing the issue
modbus_controller
Example YAML snippet
Anything in the logs that might be useful for us?
Additional information
No response