How to in HA graph preserve last value during e.g. restart or switch of ESP32

How to preserve the value in the graph when Home Assistant or ESP32 device fails or restarts (i.e. the graph will not contain 0 during shutdown but the last known value, which will be overwritten by the new value after restart)?
Thanks.