ESPhome controlled SDM320C over MODBUS relay toggle automatically to OFF after several minutes in HA dashboard

Hi, my first post. I am really out of ideas…

The actual relay remains in the position I manually toggled it to from the HA dashboard but visually the toggle will switch to off after several minutes. If the relay had been turned off manually in the HA dashboard it will remain visually off and no off triggers will be shown in the HA or ESP logs.
I have no automations or any scripts running.

In the ESPhome logs I receive the following message a few minutes after I toggle on the relay:

[19:55:20][D][switch:037]: 'enable load test mode': Sending state OFF

In the home assistant switch logs it simply says:

Turned off

this is the part of the ESPhome config:

uart:
  id: mod_uart
  rx_pin: 17
  tx_pin: 16
  baud_rate: 9600
  stop_bits: 1
  


modbus:
  send_wait_time: 200ms
  uart_id: mod_uart
  id: mod_bus
  
modbus_controller:
  - id: mod_con
    address: 2
    modbus_id: mod_bus
    # command_throttle: 100ms
    # setup_priority: -10
    # update_interval: 300s

switch:
- platform: modbus_controller
  # use_write_multiple: False
  modbus_controller_id: mod_con
  id: toggle_SDM_on_off
  register_type: coil
  address: 2
  name: "enable load test mode"
  # bitmask: 1
  # force_new_range: true


sensor:
  - platform: sdm_meter
    address: 0x2
    phase_a:
      current:
        name: "SDM220M Current"
      voltage:
        name: "SDM220M Voltage"
      active_power:
        name: "SDM220M Power"
      power_factor:
        name: "SDM220M Power Factor"
      apparent_power:
        name: "SDM220M Apparent Power"
      reactive_power:
        name: "SDM220M Reactive Power"
      phase_angle:
        name: "SDM220M Phase Angle"
    frequency:
      name: "SDM220M Frequency"
    total_power:
      name: "SDM220M Total Power"
    import_active_energy:
      name: "SDM220M Import Active Energy"
    export_active_energy:
      name: "SDM220M Export Active Energy"
    import_reactive_energy:
      name: "SDM220M Import Reactive Energy"
    export_reactive_energy:
      name: "SDM220M Export Reactive Energy"
    update_interval: 2000s

It looks like that string is used in their example as a name for a switch. I cant help you otherwise but i think maybe thats a bit of a red herring

I gave it that name (confusing I know). Giving it a short one word name did not fix my issue though…
thanks for the input!

oh right I see you have done that in the original code, welp im useless beyond that sorry.
It looks like you are saying esphome is the one who is sending the “OFF” state to HA after a few minutes, is that what seems to change the HA UI element to OFF? In that case I would say the problem is inside ESPhome, why is it sending a state that is not true, why does the relay stay on?

Sometimes a switch is a physical device that can be toggled IRL, and then that state will propagate back into your smart home system, right? So like if your relay was a real switch and someone physically moved it to OFF, it would have to change state on the GUI like this, so is it possible your relay device is acting strangely and somehow reporting that it was turned off physically? even though that is not possible?

I do not know if the off signal is coming from esphome or from HA. all I know is that the unwanted off signal is logged both in HA and in esphome. In HA it does not tell me who or what is calling this ‘off’ action and apart from toggling the toggle to off in HA and it also being logged in esphome, the physical rely is not switched off.
I have tried using a different identical relay/energy meter (SDM320C) and it shows the same behavior.