Resetting sensor value to unknown in case Modbus no response in ESPHome

Hi!

Get successfully running ESPHome with Modbus, that connected to solar inverter. All sensors settings was transferred from existing configuration of Modbus RTU from HA (there was 3 USB-232 adapters in use, and due another issue with instability, I have decided to try to switch on ESPHome).

No problem with configuration and reading, but I can’t find any configuration option to recognize that there is no Modbus response. For example, If I switch power off from ESP device, then after some time I see Unknown value of sensors in HA, but if I keep ESP power on but brake Modbus connection, ESP keep sending to HA last read value for each sensor until power on. In ESP log will be errors - “Modbus command to device=1 … no response received - removed from send queue”. But if device lost due some problem, and ESP keep sending last values to HA - it’s not possible to understood that something wrong with device + energy statistics will collect wrong data.

In case of MQTT settings, there is “expire_after:” for sensors that. In original HA Modbus configuration also “retries:” option + and if reply’s are no received - sensors will be set as unknown.
But is it something like that for Modbus in ESPHome?

Thanks you!

1 Like

Would putting a delta filter on the sensor(s) help?

If no change then don’t send value?

Thank you! Will check that!

But looks like its something different a bit, for example those variables in web portal

also update when values are coming from Modbus and keeps when no new values are coming, NA in case if those Modbus registers are missing - that logical. But if once values was read and then Modbus link was broken, old values stays there despite log show MB read error. In this case, predictably receive NA again as Sensor value.

Maybe you could create your own time based “stale data” binary sensor tracker.

on_value: of a sensor you publish an “on” state to a template binary sensor (call it is_my_sensor_data_recent etc). Then X seconds later publish off. X is stale data timeout.

on_release of the binary sensor, data is stale, so publish NAN to your sensor.

I don’t know modbus at all so don’t know about native solutions.