Modbus meter values to NA when offline not working as expected

Hi everyone,

I have an Eastron SDM120M modbus meter for my 1 of my solar setups.
Have some troubles with it and sometimes my RCD trips.
The Eastron SDM120 is after the RCD and cannot be read anymore.

However my ESP32 connected with modbus to the Eastron SDM120M keeps showing the latest value and shows up with this values in Home Assistant.

I want to stop sending values to Home Assistant and set the sensors to “Unavailable” when the Eastron SDM120 stops responding.

I have tried it with this code:

modbus_controller:
  modbus_id: modbus1
  id: sdm120
  update_interval: 5s
  address: 1
  command_throttle: 50ms
  setup_priority: -10
  on_offline:
    - logger.log: "SDM120M offline! Sensoren op unavailable zetten."
    - delay: 10s
    - lambda: |-
        id(modbus_status).publish_state(false);
    - sensor.template.publish:
        id: voltage
        state: !lambda "return NAN;"
    - sensor.template.publish:
        id: frequency
        state: !lambda "return NAN;"
    - sensor.template.publish:
        id: current
        state: !lambda "return NAN;"
    - sensor.template.publish:
        id: power
        state: !lambda "return NAN;"
    - sensor.template.publish:
        id: exported
        state: !lambda "return NAN;"
    - homeassistant.event:
        event: esphome.kwh_meter_offline
        data:
          message: "De SDM120M kWh-meter is offline!"

When i try this, i see that the values are “unavailable” for a few seconds. Howvery, after 10-15 seconds, the old, latest values are back and keeps sending this old values to Home Assistant.

Any ideas how to fix this?

Doesn’t match with my experience with modbus controller.
Post your logs where you see modbus sensors while slave is down.
Maybe you have problem on HA side?

Esp with slave disconnected:

Did you have something specific configured on your sensor to show “0” when disconnected?

My sensor configuration is as follow:

uart:
  id: mod_bus
  baud_rate: 2400
  stop_bits: 1
  parity: none
  tx_pin: GPIO6
  rx_pin: GPIO5
  
modbus:
  id: modbus1
  send_wait_time: 200ms
  
modbus_controller:
  modbus_id: modbus1
  id: sdm120
  update_interval: 5s
  address: 1
  command_throttle: 50ms
  setup_priority: -10

sensor:
  - platform: uptime
    name: "ESPHome - Uptime"
    
  - platform: wifi_signal
    name: "ESPHome - WiFi Signal dB"
    id: wifi_signal_db
    update_interval: 60s
    entity_category: "diagnostic"

  - id: voltage
    platform: modbus_controller
    name: "SDM120M Solar System 1 - Voltage"
    address: 0x0000
    unit_of_measurement: "V"
    register_type: read
    value_type: FP32
    accuracy_decimals: 2
    device_class: voltage

  - id: frequency
    platform: modbus_controller
    name: "SDM120M Solar System 1 - Frequency"
    address: 0x0046    
    unit_of_measurement: "Hz"
    register_type: read
    value_type: FP32
    accuracy_decimals: 2
    device_class: frequency

  - id: current
    platform: modbus_controller
    name: "SDM120M Solar System 1 - Current"
    address: 0x0006
    unit_of_measurement: "A"
    register_type: read
    value_type: FP32
    accuracy_decimals: 2
    device_class: current

  - id: power
    platform: modbus_controller
    name: "SDM120M Solar System 1 - Power"
    address: 0x000C
    unit_of_measurement: "W"
    register_type: read
    value_type: FP32
    accuracy_decimals: 0
    device_class: power

  - id: exported
    platform: modbus_controller
    name: "SDM120M Solar System 1 - Exported Energy"
    address: 0x004A
    unit_of_measurement: "kWh"
    register_type: read
    value_type: FP32
    accuracy_decimals: 3
    device_class: energy
    state_class: total_increasing
    
button:
  - platform: restart
    name: "ESPHome - Restart"

text_sensor:
  - platform: version
    name: "ESPHome - Version"
    hide_timestamp: true
  - platform: debug
    reset_reason:
      name: "ESPHome - Reset reason"

No I didn’t.
What you get on esphome log when you disconnect slave?

As you can see, it shows all the old values (before the device goes offline) and keeps showing it while offline. When the device is back online it will updates values again.

I assume it’s HA problem.
If you show your esphome log or even better esphome web-server output, I can understand better…

See my last post with a screenshot of ESPHome webserver. Log shows device is jot responding and it shows the latest values and keeps showing them.